【问题标题】:Button Color Change in Android Listview Item not working properlyAndroid Listview 项目中的按钮颜色更改无法正常工作
【发布时间】:2016-02-19 00:10:16
【问题描述】:

在我的列表视图中,每个项目列表都有一个按钮。当我单击按钮时,按钮颜色从绿色变为红色。但是当我向下滚动时,我看到的第一个项目的颜色按钮也是红色的。为什么我的列表视图中以前的状态视图没有被清除? 这是我的 getView(...) 代码

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) act
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.list_user, null);
        }

        TextView txtHeader = (TextView) convertView.findViewById(R.id.firstLine);
        TextView txtFooter = (TextView) convertView.findViewById(R.id.secondLine);
        Button btnAction = (Button) convertView.findViewById(R.id.btn_action);

        txtHeader.setText(mDataset.get(position).getUsername());

        txtFooter.setText(mDataset.get(position).getId());
        btnAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button btnAction = (Button)v;

                String text = btnAction.getTag().toString();
                if(text.equals("start")){
                    btnAction.setTag("finish");
                    btnAction.setText("finish");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnAction.setBackground(act.getResources().getDrawable(R.drawable.rounded_corner_error));
                    }
                }
            }
        });

        return convertView;
    }

【问题讨论】:

    标签: android listview


    【解决方案1】:

    如您所知,getView() 将重用一些视图。

    因此,这些视图可能会使用最后使用的颜色。这样,您总是必须根据位置设置文本和颜色。

    您的方法 getView() 为每个 convertView 设置文本。但是,您永远不会再次更改按钮颜色/文本。一旦设置,你就不会再改变了。

    另外,我会尝试改变逻辑。您正在为每个按钮设置一个 OnClickListener。但是,ListView 可以有一个 OnItemClickListener...

    所以,我将对您的代码进行以下更改:

    注意

    我创建了一个布尔数组来跟踪所有被点击的按钮。这只是一个分享想法的例子。

    你应该让事情变得更聪明。

    您的适配器:

    public class MyListAdapter extends BaseAdapter {
    
        // With same size of Adaptar Size - as returned by getCount().
        // Initialize it as soon as the adapter is created
        private boolean [] wasClicked;
    
        public boolean wasClicked(int position) {
            return wasClicked[position];
        }
    
        public void setClicked(int position, boolean clicked) {
            wasClicked[position] = clicked;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater mInflater = (LayoutInflater) act
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                    convertView = mInflater.inflate(R.layout.list_user, null);
            }
    
            TextView txtHeader = (TextView) convertView.findViewById(R.id.firstLine);
            TextView txtFooter = (TextView) convertView.findViewById(R.id.secondLine);
            Button btnAction = (Button) convertView.findViewById(R.id.btn_action);
    
            txtHeader.setText(mDataset.get(position).getUsername());
    
            txtFooter.setText(mDataset.get(position).getId());
    
            if(!wasClicked(position)) {
                btnAction.setTag("start");
                btnAction.setText("start");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    btnAction.setBackground(/* SET DEFAUL COLOR */);
                }            
            } else {
                btnAction.setTag("finish");
                btnAction.setText("finish");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    btnAction.setBackground(act.getResources().getDrawable(R.drawable.rounded_corner_error));
                }
            }
            return convertView;
        }
    }
    

    在你的活动中:

    public class Activity extends Activity {
        list.setAdapter(adapter);
        list.setClickable(true);
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Button btnAction = (Button) view.findViewById(R.id.btn_action);
                if(adapter.wasClicked(position)){
                    btnAction.setTag("finish");
                    btnAction.setText("finish");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnAction.setBackground(act.getResources().getDrawable(R.drawable.rounded_corner_error));
                    }
                    adapter.setClicked(position, true);
                }
            }
        });
    }
    

    【讨论】:

    • 它的工作就像魅力一样。非常感谢。我现在将探索和思考聪明。
    • 很高兴听到海军。我认为您可以使用 ArrayList 或 SparcableBooleanArray 来跟踪按钮...这些只是替换布尔数组的一些示例
    猜你喜欢
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2023-02-09
    • 2021-02-19
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    相关资源
    最近更新 更多