安卓:在ListView控件的onClick行更改按钮背景 [英] Android: Change Button background in ListView Row with onClick

查看:99
本文介绍了安卓:在ListView控件的onClick行更改按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我行包含有自己的点击监听器,我的适配器的getView设置一个按钮。我能采用了android我的按钮点击和实际行项的点击次数来区分:在该行的母公司descendantFocusability =blocksDescendants

My rows contain a button that has its own click listener set in my adapter's getView. I'm able to distinguish between my button clicks and the actual row item clicks using android:descendantFocusability="blocksDescendants" in the row's parent.

当我点击一个按钮,它正确地设置按钮的背景下,我的问题是,我通过列表中的设置它不同行,以及滚动。我认为他们的问题的地方,享有回收。

When I click on a button it sets the button background properly, my problem is as I scroll through the list its setting it for different rows as well. I assume theirs an issue somewhere with views recycling.

下面是我的code:

@Override
public View getView(int position, View convertView, ViewGroup parent){

    if(convertView == null){

        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.todays_sales_favorite_row, null);
        holder.favCatBtn = (Button)convertView.findViewById(R.id.favCatBtn);            

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder)convertView.getTag();
    }

        holder.favCatBtn.setTag(position);
        holder.favCatBtn.setOnClickListener(this);

    return convertView;
 }

@Override
public void onClick(View v) {
    int pos = (Integer) v.getTag();
    Log.d(TAG, "Button row pos click: " + pos);
    RelativeLayout rl = (RelativeLayout)v.getParent();
    holder.favCatBtn = (Button)rl.getChildAt(0);
    holder.favCatBtn.setBackgroundResource(R.drawable.icon_yellow_star_large);

}

所以,如果我在行位置1按钮背景更改,请单击该按钮,因为它应该。但后来当我向下滚动列表随机其他按键越来越设置为好。然后,有时,当我向后滚动到位置1按钮背景又恢复到原来的。

So if i click on the button at row position 1 the button background changes as it should. But then as i scroll down the list random other buttons are getting set as well. Then sometimes when I scroll back up to position 1 the button background reverts back to the original again.

我缺少的是在这里吗?我知道,我就在那里它只是次要的东西我不这样做。

What am I missing here? I know I'm right there its just something minor I'm not doing.

推荐答案

是的,你说得对,意见被回收。您将需要跟踪位置都被点击它并更新你的 getView 法的背景资源。例如,我的扩展code以添加背景的转换:

Yes, you are right, the views are recycled. You will need to track which positions have been clicked on and update the background resource in your getView method. For example, I extended your code to add background toggling:

private final boolean[] mHighlightedPositions = new boolean[NUM_OF_ITEMS];

@Override
public View getView(int position, View convertView, ViewGroup parent){

    if(convertView == null){
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.todays_sales_favorite_row, null);
        holder.favCatBtn = (Button)convertView.findViewById(R.id.favCatBtn);
        holder.favCatBtn.setOnClickListener(this);
        convertView.setTag(holder);
    }else {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.favCatBtn.setTag(position);

    if(mHighlightedPositions[position]) {
        holder.favCatBtn.setBackgroundResource(R.drawable.icon_yellow_star_large);
    }else {
        holder.favCatBtn.setBackgroundResource(0);
    }

    return convertView;
}

@Override
public void onClick(View view) {
    int position = (Integer)view.getTag();
    Log.d(TAG, "Button row pos click: " + position);

    // Toggle background resource
    RelativeLayout layout = (RelativeLayout)view.getParent();
    Button button = (Button)layout.getChildAt(0);
    if(mHighlightedPositions[position]) {
        button.setBackgroundResource(0);
        mHighlightedPositions[position] = false;
    }else {
        button.setBackgroundResource(R.drawable.icon_yellow_star_large);
        mHighlightedPositions[position] = true;
    }
}

这篇关于安卓:在ListView控件的onClick行更改按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆