Android列表视图项中的按钮颜色更改无法正常工作

时间:2016-02-19 00:10:16

标签: android listview

在我的列表视图中,每个项目列表都有一个按钮。当我点击按钮时,按钮颜色从绿色变为红色。但是当我向下滚动时,我看到的第一个项目的颜色按钮也是红色的。为什么列表视图中的先前状态视图未被清除? 这是我的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;
    }

1 个答案:

答案 0 :(得分: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);
            }
        }
    });
}