私は、テキスト、テキストと画像のいずれかを表示するリストビュー、または単に画像を有しています。私が過去に画像をスクロールすると、テキストだけを含む次の行の残りの部分は、画像が含まれているかのようにサイズが変更されます。 screenshotリセットリストビューの行の高さ
アダプターコード:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Confession> confessions;
private ImageLoader imageLoader;
public CustomListAdapter(Activity activity, List<Confession> confessions) {
this.activity = activity;
this.confessions = confessions;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder mHolder;
// getting data for the row
final Confession c = confessions.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.confessions_layout, parent, false);
if (imageLoader == null) {
imageLoader = AppController.getInstance().getImageLoader();
}
mHolder = new ViewHolder();
mHolder.networkImageView = convertView
.findViewById(R.id.confession_image);
mHolder.confession = convertView.findViewById(R.id.confession);
mHolder.date = convertView.findViewById(R.id.date);
mHolder.likeButton = convertView.findViewById(R.id.like_button);
mHolder.deleteButton = convertView.findViewById(R.id.delete_button);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.networkImageView.setImageUrl(c.getUrl(), imageLoader);
mHolder.confession.setText(c.getConfession());
mHolder.date.setText(c.getDate());
return convertView;
}
private class ViewHolder {
private TextView confession, date;
private NetworkImageView networkImageView;
private LikeButton likeButton, deleteButton;
}
は、それを移植:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SQLiteHandler db = new SQLiteHandler(getContext());
listView = getActivity().findViewById(R.id.confessions);
adapter = new CustomListAdapter(getActivity(), confessions);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getContext());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
user = new HashMap<>();
user = db.getUserDetails();
username = user.get("username");
Map<String, String> params = new HashMap();
params.put("username", username);
String tag_array_req = "req_profile";
Request profilereq;
JSONObject parameters = new JSONObject(params);
profilereq = Volley.newRequestQueue(getActivity())
.add(new JsonRequest<JSONArray>(Request.Method.POST,
url, parameters.toString(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
JSONArray ja = response;
// Parsing json
for (int i = 0; i < ja.length(); i++) {
try {
JSONObject obj = ja.getJSONObject(i);
Confession confession = new Confession();
confession.setConfession(obj.getString("confession"));
String url = obj.getString("image");
if (url != null) {
url = url.replace("..", "");
confession.setUrl(URL_MAIN + url);
}
confession.setDate(obj.getString("date"));
confession.setId(obj.getString("id"));
confession.setFavorite(obj.getString("favorite"));
confessions.add(confession);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Json error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Profile Error: " + error.getMessage());
Toast.makeText(getContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hidePDialog();
}
}) {
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = new String(networkResponse.data,
HttpHeaderParser
.parseCharset(networkResponse.headers));
return Response.success(new JSONArray(jsonString),
HttpHeaderParser
.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(profilereq, tag_array_req);
}
任意の助けをいただければ幸いです!私は何とか高さをリセットしなければならないと思いますか?そうしないと私は考えてすべての行を一の固定サイズ..
EDIT作ってるんだ:adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/confession_layout"
android:background="@color/colorPrimary">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/confession_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<LinearLayout
android:id="@+id/confessions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/confession_image"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/confession"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="20sp"
android:padding="20dp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/confessions">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"/>
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="30dp"
android:id="@+id/like_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circle_start_color="@color/white"
app:circle_end_color="@color/colorAccent"
app:dots_primary_color="@color/colorAccent"
app:dots_secondary_color="@color/colorPrimary"
app:liked="false"
android:layout_below="@id/date"
android:layout_marginTop="15dp"
/>
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="32dp"
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circle_start_color="@color/white"
app:circle_end_color="@color/colorAccent"
app:dots_primary_color="@color/colorAccent"
app:dots_secondary_color="@color/colorPrimary"
app:liked="false"
android:layout_toEndOf="@+id/like_button"
android:layout_below="@id/date"
android:layout_marginStart="10dp"
app:like_drawable="@drawable/trash_on"
app:unlike_drawable="@drawable/trash_off"
android:layout_marginTop="13dp"
/>
</RelativeLayout>
</RelativeLayout>
EDIT2:私は試してみました事を。イメージが空であれば、removeviewとsetvisibilityを試しました。私が最後にやったのは、以下のようなネットワークイメージビューのない別のレイアウトを使用することです。これは同じ問題です。 getView
で
if (convertView == null) {
mHolder = new ViewHolder();
if (!hasImage(c)) {
convertView = inflater.inflate(R.layout.text_layout, parent, false);
mHolder.confession = convertView.findViewById(R.id.confession2);
mHolder.date = convertView.findViewById(R.id.date2);
mHolder.likeButton = convertView.findViewById(R.id.like_button2);
mHolder.deleteButton = convertView.findViewById(R.id.delete_button2);
} else {
convertView = inflater.inflate(R.layout.confessions_layout, parent, false);
mHolder.networkImageView = convertView
.findViewById(R.id.confession_image);
mHolder.confession = convertView.findViewById(R.id.confession);
mHolder.date = convertView.findViewById(R.id.date);
mHolder.likeButton = convertView.findViewById(R.id.like_button);
mHolder.deleteButton = convertView.findViewById(R.id.delete_button);
}
アダプターxmlを追加します。 –
@MuthukrishnanRajendran done – Yousef
参照:https://stackoverflow.com/questions/4777272/android-listview-with-different-layouts-for-each-row – ditkin