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

java-列表视图项的选定背景已更改,它也会更改其他项,第1张

概述我是列表视图中的新手,我想在列表视图上单击它会更改背景颜色,它会更改,但是在滚动后我也会更改其他项目背景视图.我找不到我错了吗?代码段:ListColorChange.javapackagecom.example.spinnerfilter;publicclassListColorChangeextendsActivity{privateListViewph

我是列表视图中的新手,我想在列表视图上单击它会更改背景颜色,它会更改,但是在滚动后我也会更改其他项目背景视图.我找不到我错了吗?

代码段:
ListcolorChange.java

package com.example.spinnerfilter; public class ListcolorChange extends Activity { private ListVIEw PHProductinfo; ArrayList<ProductInfo> arrayListProf;ChangecolorAdapter adapter;@OverrIDeprotected 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选择了对象来轻松解决此问题.

假设这是您提供给适配器的列表类型的类

class ProductInfo{  // here you already have your instance variables   boolean isSelected;}

当您在ProductInfo项上单击时,将其对应的ProductInfo的实例变量设为true,并更改您必须已从ArrayAdapter或BaseAdapter扩展的Adapter中的相应项的背景.

当您在ListVIEw的适配器中填充List时,可以检查isSelected是否为true,而不是选择相应项目的背景.否则不要

这样,您将与滚动条无关地独立于项目的位置.因此,您的ProductInfo将保留实际选择的背景更改.

Update,

我还是对的,因此我写了一个代码段,其中包含您可以检查的输出,
为了使代码更加简洁,我将代码保持了尽可能的简单,并且在单个文件中编写了代码,并且代码也可以在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>

Output

总结

以上是内存溢出为你收集整理的java-列表视图项的选定背景已更改,它也会更改其他项全部内容,希望文章能够帮你解决java-列表视图项的选定背景已更改,它也会更改其他项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1093583.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存