Ich benutze eine listview, die einige Elemente binden und beim Klicken ich wollte Ansicht von listview aktualisieren, aber wenn ich es scrollen, wird dieser Effekt auf andere Ansichten angewendet, die zufällig Position ändert, wenn mehr Zeit blättern.Listview view update zufällig auf scroll
Dies ist mein Code.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/twvTest"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
ListView twvTest;
ArrayList<DataModel> dataModels;
NameListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
twvTest = (ListView) findViewById(R.id.twvTest);
dataModels = new ArrayList<>();
adapter = new NameListAdapter(MainActivity.this, dataModels);
twvTest.setAdapter(adapter);
twvTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < twvTest.getCount(); i++) {
TextView txtName = (TextView) view.findViewById(R.id.txtName);
if (i == position) {
txtName.setTextColor(Color.GREEN);
txtName.setBackgroundColor(Color.YELLOW);
} else {
txtName.setTextColor(Color.YELLOW);
txtName.setBackgroundColor(Color.GREEN);
}
}
}
});
fillList();
}
private void fillList() {
for (int i = 0; i < 30; i++) {
DataModel dataModel = new DataModel();
dataModel.setName("Name : " + i);
dataModels.add(dataModel);
}
adapter.notifyDataSetChanged();
}
}
NameListAdapter.java
public class NameListAdapter extends BaseAdapter {
Context context;
ArrayList<DataModel> dataModels;
LayoutInflater inflater;
public NameListAdapter(Context context, ArrayList<DataModel> dataModels) {
this.context = context;
this.dataModels = dataModels;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return dataModels.size();
}
@Override
public Object getItem(int position) {
return dataModels.get(position);
}
@Override
public long getItemId(int position) {
return dataModels.indexOf(getItem(position));
}
private class Holder {
TextView txtName;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.test_adapter_raw, parent, false);
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.txtName.setText(dataModels.get(position).getName());
return convertView;
}
}
test_adapter_raw.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#949494"
android:gravity="center"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
DataModel.java
public class DataModel {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Hallo Techsky folgt. Da die Ansicht wiederverwendet wird, können Sie ihre Farbe nicht direkt in einem onClickListener ändern ... Sie müssen Ihren Adapter aktualisieren und dann wird der Adapter die Farben der Ansicht steuern ... – W0rmH0le