如何使用基本适配器更改点击视图android的背景颜色

时间:2014-03-31 12:07:56

标签: android baseadapter

我想设置TextView中包含的点击LinearLayout的背景颜色。但我无法设置相应的背景。我没有面对 清除之前点击的背景。如果我点击全部然后设置所有背景颜色。 你能建议我吗?  如何设置TextView背景中包含的可点击LinearLayout

以下是我的示例代码:

holder.txtName = (TextView) convertView.findViewById(R.id.row_cell_text_dummy_multilevel);
    holder.l_select = (LinearLayout) convertView.findViewById(R.id.linear_select);

 holder.txtName.setTag(position);

    holder.txtName.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                select_pos=(Integer) holder.txtName.getTag();
                    if (position==select_pos) {
                        holder.l_select.setTextColor(Color.RED);
                    }else {
                        holder.l_select.setTextColor(Color.WHITE);
                    }
                }
            });

1 个答案:

答案 0 :(得分:1)

我设法完成了这么多工作,但你必须做一些功课,并找出如何自己反映这些变化。目前,仅在滚动视图时才会反映更改。

但我希望它对你有帮助。

1.declare一个静态变量,用于设置点击

的位置
private static int selectedPostion;

2.在构造函数

中将selectedPosition的值设置为-1

3. getView中的onclickListener方法执行此操作:

 int value = (Integer)((TextView)v).getTag();

Log.e("tag","(TextView)v).getTag() : " + value);
Log.e("tag", "position : " + position);

if(value == position) {
selectedPostion = position;
}else {
    selectedPostion = -1;
}

4.在返回视图之前完全在onClick代码下写下:

if(selectedPostion == position) {
    view.setBackgroundColor(mContext.getResources().getColor(R.color.even_color));
    // or  holder.l_select.setTextColor(Color.RED);
}else {
view.setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
    // or holder.l_select.setTextColor(Color.WHITE);
}

希望它有所帮助!