[英]list view item selected background changed , it changes other item also
我在列表視圖中是新的,我想在列表視圖上單擊它會更改背景顏色,它會更改,但是在滾動后我也會更改其他項目背景視圖。 我找不到我錯了嗎?
代碼段:ListColorChange.java
package com.example.spinnerfilter;
public class ListColorChange extends Activity {
private ListView phproductinfo;
ArrayList<ProductInfo> arrayListProf;
ChangeColorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
init();
}
int mselected = -1;
public void init() {
phproductinfo = (ListView) findViewById(R.id.phlist);
arraylistInit();
adapter = new ChangeColorAdapter(ListColorChange.this, arrayListProf);
phproductinfo.setAdapter(adapter);
phproductinfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
mselected = position;
ProductInfo info = arrayListProf.get(position);
info.setOngoing(true);
adapter.notifyDataSetChanged();
}
});
}
private void arraylistInit() {
String name[] = { "India", "Austrai", "Xyxz", "Pask", "New", "India",
"Austrai", "Xyxz", "Pask", "New", "India", "Austrai", "Xyxz",
"Pask", "New", "India", "Austrai", "Xyxz", "Pask", "New",
"India", "India", "Austrai", "Xyxz", "Pask", "New", "India",
"India", "Austrai", "Xyxz", "Pask", "New", "India", "India",
"Austrai", "Xyxz", "Pask", "New", "India" };
arrayListProf = new ArrayList<ProductInfo>();
for (int i = 0; i < name.length; i++) {
ProductInfo info = new ProductInfo();
info.setDisplay_name(name[i]);
info.setOngoing(false);
arrayListProf.add(info);
}
}
public class ChangeColorAdapter extends BaseAdapter
{
public ChangeColorAdapter(ListColorChange listColorChange,
ArrayList<ProductInfo> arrayListProf) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayListProf.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arrayListProf.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder vh;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater
.from(ListColorChange.this);
convertView = inflater.inflate(R.layout.productlist, null);
TextView product_name = (TextView) convertView
.findViewById(R.id.prod_name);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
if (arrayListProf != null && arrayListProf.size() > 0) {
ProductInfo pi = arrayListProf.get(position);
if (pi.getDisplay_name() != null) {
vh.product_name.setText(pi.getDisplay_name());
}
if (arrayListProf.get(position).isOngoing() && mselected == position) {
convertView.setBackgroundColor(Color.parseColor("#ff0000"));
}
}
return convertView;
}
class ViewHolder {
TextView product_name;
TextView qty;
TextView unit;
int position;
boolean ispu = false;
public ViewHolder(View view) {
// TODO Auto-generated constructor stub
product_name = (TextView) view.findViewById(R.id.prod_name);
qty = (TextView) view.findViewById(R.id.qty);
unit = (TextView) view.findViewById(R.id.unit);
}
}
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<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/phlist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
這是由於在ListView
滾動后單擊位置與單擊位置不同而導致的問題,您可以通過讓對象記住是否由Onclick
listnere選擇了對象來輕松解決此問題。
假設這是您提供給Adapter
的list
類型的類
class ProductInfo{
// here you already have your instance variables
boolean isSelected;
}
當你Onclick
上ProductInfo
項目作出相應的這個實例變量ProductInfo
, true
和改變你的各個項目的背景Adapter
,你必須從擴展ArrayAdapter
或BaseAdapter
。
當您在ListView's
適配器中填充List時,可以檢查isSelected
是否為true,而不是選擇相應項目的背景。 否則不要
這樣,您將與滾動條無關地獨立於項目的位置。 因此,您的ProductInfo
將保留實際選擇的背景更改。
更新,
我還是對的,所以我寫了一個代碼片段,您可以檢查輸出,我將代碼保持了盡可能的簡單,並為簡單起見在單個文件中編寫了代碼,並且該代碼也可以在github上找到 ,您可以在其中正確地檢查代碼
public class CustomListViewActivity extends Activity {
private ListView listView;
private ArrayList<ProductInfo> productInfos;
private ArrayAdapter<ProductInfo> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_list_view2);
initializeUI();
}
private void initializeUI() {
listView = (ListView)findViewById(R.id.CustomListViewActivity_listView_two);
productInfos = new ArrayList<>();
for (int i = 0; i < 50; i++) {
ProductInfo productInfo = new ProductInfo();
productInfo.setText("Product_1_"+i);
productInfos.add(productInfo);
}
adapter = new MyAdapter(getApplicationContext(), R.layout.single_item_custom_one, productInfos);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ProductInfo productInfo = (ProductInfo) listView.getItemAtPosition(position);
productInfo.setSelected(true);
adapter.notifyDataSetChanged();
}
});
}
private class MyAdapter extends ArrayAdapter {
private ArrayList<ProductInfo> a_productInfos;
private Context a_context;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<ProductInfo> a_productInfos) {
super(context, resource, a_productInfos);
this.a_productInfos = a_productInfos;
this.a_context = context;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_custom_one, parent, false);
holder = new ViewHolder();
holder.product_name = (TextView) row.findViewById(R.id.single_item_custom_one_textView);
holder.item_LinearLayout = (LinearLayout) row.findViewById(R.id.single_item_custom_one_linearLayout);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final ProductInfo productInfo = a_productInfos.get(position);
holder.product_name.setText(""+productInfo.getText());
if (productInfo.isSelected) {
holder.item_LinearLayout.setBackgroundColor(Color.parseColor("#ff44ff"));
}else {
holder.item_LinearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
}
return row;
}
class ViewHolder {
TextView product_name;
LinearLayout item_LinearLayout;
}
@Override
public int getCount() {
return super.getCount();
}
}
private class ProductInfo {
private String text;
private boolean isSelected;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
}
activity_custom_list_view2.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/CustomListViewActivity_listView_two"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
single_item_custom_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/single_item_custom_one_linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/single_item_custom_one_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
android:paddingRight="?android:attr/listPreferredItemPaddingRight"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#000" />
</LinearLayout>
產量
更新代碼
if (arrayListProf.get(position).isOngoing() && mselected ==position) {
convertView.setBackgroundColor(Color.parseColor("#ff0000"));
convertView.setTag(vh);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.