ListView 选择器问题:选择内容未被删除

发布于 2024-09-24 19:59:49 字数 1681 浏览 7 评论 0原文

我有一个列表视图。当我单击 ListItem 时,我将 ListItem (它的视图)的背景设​​置为另一种颜色:

  listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    setupDetailView(position);
                    setupChartView(position);
                    setupARView(position);
                    emptyView.setVisibility(View.INVISIBLE);

                    quotesAdapter.isSelected = true;
                    //v.setBackgroundResource(R.drawable.stocks_selector);
                }
            });

这是我的适配器:

private class QuoteAdapter extends ArrayAdapter<Quote> {

        private ArrayList<Quote> items;
        public boolean isSelected = false;

        public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.mainrow, null);

                if(isSelected)
                    v.setBackgroundResource(R.drawable.red);
                else
                    v.setBackgroundResource(R.drawable.transparent_background);
            }

问题是,如果我选择多行,则多行具有彩色背景。我只希望单击的项目具有彩色背景。因此,如果我单击第 2 行,我希望它变为红色,然后如果我单击第 1 行,我希望第 2 行恢复正常,第 1 行变为红色。

我该怎么做?

I have a ListView. When I click on a ListItem, I set the background of the ListItem (it's view) to another color:

  listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    setupDetailView(position);
                    setupChartView(position);
                    setupARView(position);
                    emptyView.setVisibility(View.INVISIBLE);

                    quotesAdapter.isSelected = true;
                    //v.setBackgroundResource(R.drawable.stocks_selector);
                }
            });

here is my adapter:

private class QuoteAdapter extends ArrayAdapter<Quote> {

        private ArrayList<Quote> items;
        public boolean isSelected = false;

        public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.mainrow, null);

                if(isSelected)
                    v.setBackgroundResource(R.drawable.red);
                else
                    v.setBackgroundResource(R.drawable.transparent_background);
            }

The problem is, if I select multiple rows, then multiple rows have a colored background. I only want the clicked item to have a colored background. So if I click on row 2, I want it to turn red, then if I click row 1, I want row 2 to go back to normal, and row 1 to turn red.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-10-01 19:59:49

将列表选择器放入 ListView 中,

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:listSelector="@drawable/stocks_selector" />

stocks_selector.xml 应该类似于

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    type="rectangle"

>
    <gradient
        android:startColor="@color/start_gradient"
        android:endColor="@color/end_gradient" 
        android:angle="270" />
/>
</shape>

Put your list selector in your ListView

 <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:listSelector="@drawable/stocks_selector" />

your stocks_selector.xml should look something like

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    type="rectangle"

>
    <gradient
        android:startColor="@color/start_gradient"
        android:endColor="@color/end_gradient" 
        android:angle="270" />
/>
</shape>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文