我正在使用listview綁定一些項目,並點擊我想更新listview視圖,但是當我滾動它的效果應用於其他視圖隨機更改位置,如果滾動更多的時間。列表視圖更新隨機滾動
這是我的代碼。
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;
}
}
嗨Techsky使用標識爲您listItems中的根佈局。由於視圖被重用,你不能直接在onClickListener中修改它的顏色......你必須更新你的適配器,然後,你的適配器將控制視圖的顏色... – W0rmH0le