Ich habe ListView in meinem Fragment, wo ich versuche, einige Elemente von LongClick auswählen. Auf meinem Bildschirm kann ich 8 Elemente sehen (auf einem anderen Smartphone kann ich 6 Elemente sehen.) Wenn ich viele Elemente in meiner ListView (zum Beispiel 23) und ich LongClick auf ein erstes Element als Ergebnis habe, sehe ich das geänderte Bild, aber Wenn ich nach unten scrolle, kann ich sehen, dass 10 und 19 Elemente auch das Bild geändert haben (wie sie überprüft wurden). Auf dem anderen Smartphone nach LongClick den ersten Eintrag sehe ich auch 8 und 16 und etc). Wie Sie sehen können, bekomme ich Änderungen an den Bildern von ListView-Elementen, nachdem die Anzahl der Elemente, die mein Smartphone darstellen kann, gleich ist. Der tatsächliche Status von "zusätzlichen" Elementen ändert sich nicht, nur ihre Bildansicht. Es ist das seltsame Verhalten von ListView, das sich ändernde Bild der Artikelansicht in jeder Gruppe verdoppelt (Gruppe bedeutet die Anzahl ListView-Elemente ein Vielfaches der Anzahl der Elemente, die das Smartphone gleichzeitig anzeigen kann)Fragment ListView seltsames Verhalten
Was ist falsch in meinem Code oder wie zu vermeiden Dieses unerwartete Verhalten von ListView? Thanx
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import ru.someCode.R;
public class ListItems extends ListFragment {
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list, null);
lv = getListView();
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view,
int position, long id) {
Log.d("MY", "Checked");
ImageView imageView = ((ImageView) view.findViewById(R.id.operImg));
ImageView imageViewCheck = ((ImageView) view.findViewById(R.id.operImgCheck));
if (lv.isItemChecked(position)) {
lv.setItemChecked(position, false);
imageView.setVisibility(View.VISIBLE);
imageViewCheck.setVisibility(View.GONE);
} else {
lv.setItemChecked(position, true);
imageView.setVisibility(View.INVISIBLE);
imageViewCheck.setVisibility(View.VISIBLE);
}
return true;
}
});
}
}
---- ---- fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listContainer">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dip"
android:divider="@color/listDev"
android:footerDividersEnabled="true"/>
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:gravity="center" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/progressContainer"
android:layout_centerInParent="true"
android:gravity="center"
android:visibility="gone">
<ProgressBar style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
---- Adapter Codeteil ---
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.history_list_group, null);
holder = findViewsById(convertView);
convertView.setTag(holder);
if (lv == null) {
lv = (ListView) parent;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
SetDataView(holder, position);
return convertView;
}
---- history_list_group. xml -
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:id="@+id/operImgLayout"
android:layout_gravity="center_vertical"
android:layout_margin="6dp">
<ru.phone4pay.phone4pay.extlib.MLRoundedImageView
android:id="@+id/operImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_action_1"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/operImgCheck"
android:src="@drawable/ic_action_tick"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
Scheint, dass Ihr getView nicht richtig codiert ist. –
Zeigen Sie den vollständigen Adaptercode und den setDataView-Code an –
Sie müssen diese Frage nicht ablehnen. @ user5546244 Sie haben Problem mit der Aufrechterhaltung der Position können Sie ähnliche Frage überprüfen und das wird auch helfen, http://stackoverflow.com/questions/11190390/checking-a-checkbox-in-listview-makes-other-random-checkboxes-checked -tooo .. Überprüfen Sie gegebenen Link und versuchen und verstehen Sie die Antwort und passen Sie es mit Ihrem Code, wenn Sie festgestellt, dass Sie das gleiche Problem als, lösen Sie mit und löschen Sie Ihre eigene Frage nach erfolgreich lösen – MKJParekh