zhangcainiao007
zhf_827
采纳率75%
2016-07-05 10:09

listview 中,滑动liseview会导致其他没有点击情况的item也会出现图片的改变

15

public class CallAdapter extends BaseAdapter{
private int table_sum = 6;
private Context mContext;
private LayoutInflater layoutInflater;

public CallAdapter(Context context, int cur_pos){
    this.mContext = context;
    this.layoutInflater = LayoutInflater.from(this.mContext);

}

public int getCount() {
    return table_sum;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     GameListHolder gameListHolder = null;

    if (convertView == null) {
        convertView = this.layoutInflater.inflate(
                R.layout.lianhuantouzhu_list_item, null);
        gameListHolder = new GameListHolder();

        gameListHolder.rl_list_item_1 = (RelativeLayout) convertView
                .findViewById(R.id.rl_list_item_1);
        gameListHolder.rl_list_item_4 = (RelativeLayout) convertView
                .findViewById(R.id.rl_list_item_4);
        gameListHolder.rl_list_item_2 = (RelativeLayout) convertView
                .findViewById(R.id.rl_list_item_2);
        gameListHolder.rl_list_item_5 = (RelativeLayout) convertView
                .findViewById(R.id.rl_list_item_5);

        gameListHolder.rl_call = (RelativeLayout) convertView
                .findViewById(R.id.rl_call);

        gameListHolder.ll_line_blue = (TextView) convertView
                .findViewById(R.id.ll_line_blue);
        gameListHolder.ll_line_green = (TextView) convertView
                .findViewById(R.id.ll_line_green);
        gameListHolder.ll_line_red = (TextView) convertView
                .findViewById(R.id.ll_line_red);

        gameListHolder.iv_small_chip1 = (ImageView)convertView.
                 findViewById(R.id.iv_small_chip1);
        gameListHolder.iv_small_chip2 = (ImageView)
                convertView.findViewById(R.id.iv_small_chip2);
        gameListHolder.iv_small_chip3 = (ImageView)
                convertView.findViewById(R.id.iv_small_chip3);
        gameListHolder.iv_small_chip4 = (ImageView)
                convertView.findViewById(R.id.iv_small_chip4);
        gameListHolder.iv_small_chip5 = (ImageView)
                convertView.findViewById(R.id.iv_small_chip5);

        gameListHolder.rl_desktop_1 = (RelativeLayout) convertView
                .findViewById(R.id.rl_desktop_1);

        gameListHolder.rl_desktop_2 = (RelativeLayout) convertView
                .findViewById(R.id.rl_desktop_2);
        gameListHolder.rl_desktop_3 = (RelativeLayout) convertView
                .findViewById(R.id.rl_desktop_3);
        gameListHolder.rl_desktop_4 = (RelativeLayout) convertView
                .findViewById(R.id.rl_desktop_4);
        gameListHolder.rl_desktop_5 = (RelativeLayout) convertView
                .findViewById(R.id.rl_desktop_5);


        convertView.setTag(gameListHolder);
    } 
    else {
        gameListHolder = (GameListHolder) convertView.getTag();
    }

    //添加事件
        gameListHolder.rl_desktop_1.setOnClickListener(new DesktopClickListener(gameListHolder.iv_small_chip1));
        gameListHolder.rl_desktop_2.setOnClickListener(new DesktopClickListener(gameListHolder.iv_small_chip2));
        gameListHolder.rl_desktop_3.setOnClickListener(new DesktopClickListener(gameListHolder.iv_small_chip3));
        gameListHolder.rl_desktop_4.setOnClickListener(new DesktopClickListener(gameListHolder.iv_small_chip4));
        gameListHolder.rl_desktop_5.setOnClickListener(new DesktopClickListener(gameListHolder.iv_small_chip5));

    return convertView;

}

/**

 * @remark 点击事件
 *
 */
class DesktopClickListener implements OnClickListener {
    ImageView iv;
    DesktopClickListener( final ImageView iv) {
        this.iv = iv;
    }
    @Override
    public void onClick(View v) {
        if (GameActivity.cur_pos == -1) {
        } else {
            this.iv.setImageResource(SysConstant.chip_smallID[GameActivity.cur_pos]);

        }

        listview 中,如我上面的代码中有6个item,每个item中都有5个relativelayout,当我点击其中一个item的任意一个relativelayout时(每个relativelayout都包含一个imageview控件),会改变对应这个相对布局中imageview的资源,此时当我滑动liseview会出现其他item中(没有点击情况)的imageview也会出现图片资源的改变



        上面是我的baseadapte的完整代码,我网上查了,由于复用导致的这个问题的,就是说滑动listview的时候,可见的item会复用不可见item里的数据,需要进行绑定状态才能OK,但是我试了下,还是不能达到效果。求哪位大侠帮指点下,根据我上面的代码如何才能时点击任意一个item中改变对应的图片后,滑动item ,其他的item保证状态不变,求大虾们能指出在哪里添加状态,详细点最好,我把我剩余的c币都给大家*(也不多)。求大虾们帮帮忙!
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • daweibalang717 daweibalang717 5年前

    适配器里面getView的时候进行图片设置,在onclick的时候进行数据标记。举个栗子:点击 onclick的时候标记一个ImageView的要设置图片A,
    然后调用 adapter类的 notifydatasetchanged 方法去更新数据, 在getView 方法中设置 你标记的图片是哪一张 setImageResource( A)。
    你当前是在 onclick 中直接进行数据设置了。

    点赞 评论 复制链接分享
  • qq_28026283 大白大白大白 5年前

    if (GameActivity.cur_pos == -1) {
    } else {
    this.iv.setImageResource(SysConstant.chip_smallID[GameActivity.cur_pos]);

        }这个判断-1是?
    
    点赞 评论 复制链接分享
  • qq_35485029 唐之风采 5年前

    你把添加事件那部分代码放到 if (convertView == null) {}else{} 后面 return convertView之前 ,放错位置了。

    点赞 评论 复制链接分享
  • daweibalang717 daweibalang717 5年前

    看不懂你的描述,但是你这么写会有问题,你把添加事件那部分代码放到 if (convertView == null) {}else{} 后面 return convertView之前 ,添加事件 放错位置了。

    点赞 评论 复制链接分享

相关推荐