列表视图项选择的背景已更改,它也会更改其他项

时间:2016-01-12 11:01:33

标签: java android listview

我是列表视图中的新功能,我希望项目点击列表视图它改变背景颜色,它改变但我滚动后它也改变了其他项目背景视图。我无法找出我错在哪里?

代码段: 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>

2 个答案:

答案 0 :(得分:1)

这是ListView滚动后点击位置不同的问题,您可以通过Onclick listnere选择对象来记住对象,从而轻松解决此问题

假设这是您提供给list的{​​{1}}类型的类

Adapter

class ProductInfo{ // here you already have your instance variables boolean isSelected; } 项目OnclickProductInfo ProductInfo时,创建此实例变量并更改true中相应项目的背景您必须从AdapterArrayAdapter延长。

当您在BaseAdapter适配器中填充列表时,您可以检查ListView's是否为真,而不是选择相应项目的背景。否则不要

通过这样做,您将独立于项目的位置以及相应的滚动。因此,isSelected将保留实际选择的背景更改。

  

更新,

我仍然是对的所以我写了一个带有输出的代码片段,你可以检查, 为了简单起见,我保持代码尽可能简单并在单个文件中编写代码,代码也可以在github上获得,您可以正确地检查代码

ProductInfo
  

activity_custom_list_view2.xml

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;
        }
    }
}
  

single_item_custom_one.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>
  

输出

output_image

答案 1 :(得分:0)

更新代码

if (arrayListProf.get(position).isOngoing() && mselected ==position) {
        convertView.setBackgroundColor(Color.parseColor("#ff0000"));
         convertView.setTag(vh);
  }