q649916440 2014-08-08 01:52 采纳率: 0%
浏览 4096

Android开发listView中的Button点击事件被拦截

根据http://blog.csdn.net/boylinux/article/details/8860443 这位大神的帖子,我的代码如下,现在的问题是Listview中的Button和imageview和textview控件点击之后不会响应,而在滑动一下Listview后,之前的点击事件才会响应,导致这个问题的原因不明,但是如果不用HashMap这个MAP的话,就不会有这个问题,请问大神们这是为什么呢?代码如下:
public class MyTwoAdapter extends BaseAdapter {
Context context;
private List data;
FollowInfo followInfo;
Bitmap bitmap;
FinalBitmap finalBitmap;
HashMap lmap = new HashMap();
private LayoutInflater mInflater=null;

public void setData(List<FollowInfo> data) {
    this.data = data;
}

public MyTwoAdapter(Context context) {
    this.context = context;
    mInflater = LayoutInflater.from(context);
    finalBitmap = FinalBitmap.create(context);//初始化FinalBitmap模块
    finalBitmap.configLoadingImage(R.drawable.ic_launcher);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public FollowInfo getItem(int position) {
    return data.get(position);
}

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

@SuppressLint("NewApi") @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (lmap.get(position)==null) {

// if (convertView == null) {
convertView = mInflater.inflate(R.layout.mytwo_item, null);
viewHolder = new ViewHolder();
viewHolder.user_logo = (ImageView) convertView.findViewById(R.id.user_logo);
viewHolder.user_name = (TextView) convertView.findViewById(R.id.user_name);
viewHolder.add_time = (TextView) convertView.findViewById(R.id.add_time);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
viewHolder.sign1 = (TextView) convertView.findViewById(R.id.sign1);
viewHolder.sign2 = (TextView) convertView.findViewById(R.id.sign2);
viewHolder.sign3 = (TextView) convertView.findViewById(R.id.sign3);
viewHolder.up = (Button) convertView.findViewById(R.id.yes_up);
viewHolder.no_up = (Button) convertView.findViewById(R.id.no_up);
viewHolder.up_num = (TextView) convertView.findViewById(R.id.up_num);
viewHolder.say_sth = (ImageButton) convertView.findViewById(R.id.say_sth);
viewHolder.share = (ImageButton) convertView.findViewById(R.id.share);

viewHolder.fl = (FrameLayout) convertView.findViewById(R.id.fl);
viewHolder.describe = (TextView) convertView.findViewById(R.id.describe);
viewHolder.iv.setOnClickListener(clickListener);
lmap.put(position, convertView);

convertView.setTag(viewHolder);

        followInfo = data.get(position);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.fl.getLayoutParams();
        layoutParams.width = AndroidUtil.SCREEN_WIDTH; //图片宽度填满屏幕
        layoutParams.height = (int) (AndroidUtil.SCREEN_WIDTH * 1.5);
        viewHolder.fl.setLayoutParams(layoutParams);
        viewHolder.user_logo.setImageResource(R.drawable.ic_launcher);
        viewHolder.iv.setImageResource(R.drawable.ic_launcher);
        viewHolder.user_name.setText(followInfo.getUser_name());
        viewHolder.up_num.setText(followInfo.getHot() + "");
        viewHolder.add_time.setText(UpdateTime.getUpdateDate(new Date().getTime(), followInfo.getUpload_date(), context));
        if (!followInfo.getDescribe().equals("")){
            viewHolder.describe.setVisibility(0);
            viewHolder.describe.setText(followInfo.getDescribe());              
        }
        if (followInfo.getLikeStatus() == 1){
            viewHolder.up.setVisibility(View.VISIBLE);
            viewHolder.no_up.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.up.setVisibility(View.INVISIBLE);
            viewHolder.no_up.setVisibility(View.VISIBLE);
        }
        if (!followInfo.getCountry3().equals("")){
            viewHolder.sign1.setVisibility(View.VISIBLE);
            viewHolder.sign1.setText("#" + followInfo.getCountry3() + " ");
        }
        if (!followInfo.getFashion().equals("")){
                viewHolder.sign2.setVisibility(View.VISIBLE);
                viewHolder.sign2.setText("#" + followInfo.getFashion() + " ");
        }
        if (!followInfo.getScene().equals("")){
                viewHolder.sign3.setVisibility(View.VISIBLE);
                viewHolder.sign3.setText("#" + followInfo.getScene() + " ");
        }

        //图片设置
        final String user_image = followInfo.getImage_id();

        finalBitmap.display(viewHolder.iv,AndroidUtil.SERVER_PATH+  //finalBitmap加载图片
                "imageLoad/load_image_buff/"+ user_image +"/00/0");

         //头像设置图片   
        final String user_logoUrl = followInfo.getUser_id();
        finalBitmap.display(viewHolder.user_logo,AndroidUtil.SERVER_PATH    
                +"imageLoad/load_user_image_buff/" + user_logoUrl);

        if (followInfo.getTags() != null && followInfo.getTags().size() != 0){ //动态添加标签                             
            for (int i = 0; i <  followInfo.getTags().size(); i++){ //TAG数
                float tranX;
                float tranY;
                View tag_layout;

                final Tags tag = followInfo.getTags().get(i); 

                if (tag.getPercentX() == null || tag.getPercentX().equals("") || tag.getPercentX().equals("null")){
                    tranX = Float.parseFloat(tag.getTagX()) / 480 * layoutParams.width;
                    tranY = Float.parseFloat(tag.getTagY()) / 720 * layoutParams.height - AndroidUtil.dip2px(context, 8);
                } else {
                    tranX = Float.parseFloat(tag.getPercentX()) * layoutParams.width;
                    tranY = Float.parseFloat(tag.getPercentY()) * layoutParams.height - AndroidUtil.dip2px(context, 8);
                }

// Log.e("tranX", tranX + "");
// Log.e("tranY", tranY + "");
//链接图片是否占空间
int brandURLLength = 0;
boolean isBrandUrl = false;
if (tag.getBrandURL() != null && !tag.getBrandURL().equals("") && !tag.getBrandURL().equals("null")){
isBrandUrl = true;
brandURLLength = AndroidUtil.dip2px(context, 25);
}
//圆点及箭头及文字空白区域的长度和
int pointArrowLength = AndroidUtil.dip2px(context, 35);
//文字的长度
int textLength = AndroidUtil.getLineLength(tag.getBrand(), 28);
// Log.e("position", position + "");
TextView tag_text;
ImageView tag_brand;
// 判断左边显示还是右边显示
if (layoutParams.width - tranX >= brandURLLength + pointArrowLength + textLength){ //右边显示
tag_layout = LayoutInflater.from(context).inflate(R.layout.right_tag, null);
tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);
tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);
tag_layout.setTranslationX(tranX);
tag_layout.setTranslationY(tranY);
} else if (tranX < brandURLLength + pointArrowLength + textLength){ //显示不全
if (tranX * 2 < layoutParams.width){ //右边显示.但显示不全

tag_layout = LayoutInflater.from(context).inflate(R.layout.right_tag, null);
tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);
tag_text.setWidth((int)(layoutParams.width
- tranX - brandURLLength - pointArrowLength + AndroidUtil.dip2px(context, 10)));
tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);
tag_layout.setTranslationX(tranX);
tag_layout.setTranslationY(tranY);
} else { //左边显示 不全
tag_layout = LayoutInflater.from(context).inflate(R.layout.left_tag, null);
tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);
tag_text.setWidth((int)(tranX - brandURLLength - AndroidUtil.dip2px(context, 15)));
tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);
tag_layout.setTranslationY(tranY);
}
} else { //左边显示
tag_layout = LayoutInflater.from(context).inflate(R.layout.left_tag, null);
tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);
tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);
tag_layout.setTranslationX(tranX - AndroidUtil.dip2px(context, 25) - textLength - brandURLLength);
tag_layout.setTranslationY(tranY);
}
tag_text.setText(tag.getBrand());
if (isBrandUrl){
tag_brand.setVisibility(View.VISIBLE);

}
viewHolder.fl.addView(tag_layout);

                tag_text.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(context, SearchResultActivity.class);
                        ArrayList<String> list = new ArrayList<String>();
                        list.add(tag.getBrand());
                        intent.putExtra("sign_cont", list);
                        intent.putExtra("from_flag", "&brand=");
                        context.startActivity(intent);
                    }
                });
                tag_brand.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {                                               
                        if (tag.getBrandURL() != null && !tag.getBrandURL().equals("")){
                            Uri uri = Uri.parse(tag.getBrandURL());
                            Intent intent = new Intent();
                            intent.setData(uri);
                            intent.setAction(Intent.ACTION_VIEW);
                            try{
                                context.startActivity(intent);                                  
                            } catch (Exception e){
                                Toast toast = Toast.makeText(context,
                                        context.getResources().getString(R.string.err_uri), Toast.LENGTH_SHORT);
                                toast.setGravity(Gravity.CENTER, 0, 300);                               
                                toast.show();
                            }                           
                        }
                    }
                });
            }
        }
        addListener(convertView, followInfo, followInfo.getImage_id(), position);
    }
    else {
        convertView = lmap.get(position);  
        viewHolder = (ViewHolder) convertView.getTag();
    }
    return convertView;
}

public class ViewHolder{
    ImageView user_logo;
    TextView user_name;
    TextView add_time;
    ImageView iv;
    TextView sign1;
    TextView sign2;
    TextView sign3;
    Button up;
    Button no_up;
    TextView up_num;
    ImageButton say_sth;
    ImageButton share;
    FrameLayout fl;
    TextView describe;
}
public void addListener(final View convertView,final FollowInfo followInfo,
        final String image_uri, final int position) {      
    final Button no_up = (Button) convertView.findViewById(R.id.no_up);
    final Button yes_up = (Button) convertView.findViewById(R.id.yes_up);
    final TextView up_num = (TextView) convertView.findViewById(R.id.up_num);
    final ImageButton share = (ImageButton) convertView.findViewById(R.id.share);
    final TextView sign1 = (TextView) convertView.findViewById(R.id.sign1);
    final TextView sign2 = (TextView) convertView.findViewById(R.id.sign2);
    final TextView sign3 = (TextView) convertView.findViewById(R.id.sign3);
    final ImageButton say_sth = (ImageButton) convertView.findViewById(R.id.say_sth);
    final ImageView user_logo = (ImageView) convertView.findViewById(R.id.user_logo);
    final TextView user_name = (TextView) convertView.findViewById(R.id.user_name);
    no_up.setOnClickListener(new OnClickListener() {            
        public void onClick(View v) {
            no_up.setVisibility(View.INVISIBLE);
            yes_up.setVisibility(View.VISIBLE);
            up_num.setText(Integer.parseInt(up_num.getText().toString()) + 1 + "");
            new AsyncUpLoad(followInfo.getImage_id(), followInfo.getUser_id());
        }
    }); 
    yes_up.setOnClickListener(new OnClickListener() {           
        public void onClick(View v) {
            no_up.setVisibility(View.VISIBLE);
            yes_up.setVisibility(View.INVISIBLE);
            up_num.setText(Integer.parseInt(up_num.getText().toString()) - 1 + "");
            new AsyncUpLoad(followInfo.getImage_id(), followInfo.getUser_id());
        }
    }); 

}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 10:53
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题可能是因为在addListener方法中,您尝试在同一个OnClickListener中处理多个按钮(例如,no_upyes_up),这可能导致在某个时间点上这些按钮已经处于不可用状态。为了解决这个问题,您可以考虑将每个按钮分别绑定到不同的OnClickListener

    以下是一个修改后的版本:

    public class MyTwoAdapter extends BaseAdapter {
    
        Context context;
    
        private List data;
    
        FollowInfo followInfo;
    
        Bitmap bitmap;
    
        FinalBitmap finalBitmap;
    
        HashMap lmap = new HashMap(); 
    
        private LayoutInflater mInflater=null;
    
        @Override
        public int getCount() {
            return data.size();
        }
    
        @Override
        public FollowInfo getItem(int position) {
            return data.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (lmap.get(position) == null) {
                convertView = mInflater.inflate(R.layout.mytwo_item, null);
    
                viewHolder = new ViewHolder();
    
                viewHolder.user_logo = (ImageView) convertView.findViewById(R.id.user_logo);
    
                viewHolder.user_name = (TextView) convertView.findViewById(R.id.user_name);
    
                viewHolder.add_time = (TextView) convertView.findViewById(R.id.add_time);
    
                viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
    
                viewHolder.sign1 = (TextView) convertView.findViewById(R.id.sign1);
    
                viewHolder.sign2 = (TextView) convertView.findViewById(R.id.sign2);
    
                viewHolder.sign3 = (TextView) convertView.findViewById(R.id.sign3);
    
                viewHolder.up = (Button) convertView.findViewById(R.id.yes_up);
    
                viewHolder.no_up = (Button) convertView.findViewById(R.id.no_up);
    
                viewHolder.up_num = (TextView) convertView.findViewById(R.id.up_num);
    
                viewHolder.say_sth = (ImageButton) convertView.findViewById(R.id.say_sth);
    
                viewHolder.share = (ImageButton) convertView.findViewById(R.id.share);
    
                viewHolder.fl = (FrameLayout) convertView.findViewById(R.id.fl);
    
                viewHolder.describe = (TextView) convertView.findViewById(R.id.describe);
    
                viewHolder.iv.setOnClickListener(clickListener);
    
                lmap.put(position, convertView);
    
                convertView.setTag(viewHolder);
    
                followInfo = data.get(position);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.fl.getLayoutParams();
                layoutParams.width = AndroidUtil.SCREEN_WIDTH; //图片宽度填满屏幕
                layoutParams.height = (int) (AndroidUtil.SCREEN_WIDTH * 1.5);
    
                viewHolder.fl.setLayoutParams(layoutParams);
    
                viewHolder.user_logo.setImageResource(R.drawable.ic_launcher);
                viewHolder.iv.setImageResource(R.drawable.ic_launcher);
                viewHolder.user_name.setText(followInfo.getUser_name());
                viewHolder.up_num.setText(followInfo.getHot() + "");
                viewHolder.add_time.setText(UpdateTime.getUpdateDate(new Date().getTime(), followInfo.getUpload_date(), context));
    
                if (!followInfo.getDescribe().equals("")) {
                    viewHolder.describe.setVisibility(0);
                    viewHolder.describe.setText(followInfo.getDescribe());
                }
    
                if (followInfo.getLikeStatus() == 1) {
                    viewHolder.up.setVisibility(View.VISIBLE);
                    viewHolder.no_up.setVisibility(View.INVISIBLE);
                } else {
                    viewHolder.up.setVisibility(View.INVISIBLE);
                    viewHolder.no_up.setVisibility(View.VISIBLE);
                }
    
                if (!followInfo.getCountry3().equals("")) {
                    viewHolder.sign1.setVisibility(View.VISIBLE);
                    viewHolder.sign1.setText("#" + followInfo.getCountry3() + " ");
                }
    
                if (!followInfo.getFashion().equals("")) {
                    viewHolder.sign2.setVisibility(View.VISIBLE);
                    viewHolder.sign2.setText("#" + followInfo.getFashion() + " ");
                }
    
                if (!followInfo.getScene().equals("")) {
                    viewHolder.sign3.setVisibility(View.VISIBLE);
                    viewHolder.sign3.setText("#" + followInfo.getScene() + " ");
                }
    
                //图片设置
                final String user_image = followInfo.getImage_id();
    
                finalBitmap.display(viewHolder.iv, AndroidUtil.SERVER_PATH + //finalBitmap加载图片
                        "imageLoad/load_image_buff/" + user_image + "/00/0");
    
                //头像设置图片   
                final String user_logoUrl = followInfo.getUser_id();
                finalBitmap.display(viewHolder.user_logo, AndroidUtil.SERVER_PATH    
                        +"imageLoad/load_user_image_buff/" + user_logoUrl);
    
                if (followInfo.getTags() != null && followInfo.getTags().size() != 0) { //动态添加标签
                    for (int i = 0; i < followInfo.getTags().size(); i++) { //TAG数
                        float tranX;
                        float tranY;
                        View tag_layout;
    
                        final Tags tag = followInfo.getTags().get(i); 
    
                        if (tag.getPercentX() == null || tag.getPercentX().equals("") || tag.getPercentX().equals("null")) {
                            tranX = Float.parseFloat(tag.getTagX()) / 480 * layoutParams.width;
                            tranY = Float.parseFloat(tag.getTagY()) / 720 * layoutParams.height - AndroidUtil.dip2px(context, 8);
                        } else {
                            tranX = Float.parseFloat(tag.getPercentX()) * layoutParams.width;
                            tranY = Float.parseFloat(tag.getPercentY()) * layoutParams.height - AndroidUtil.dip2px(context, 8);
                        }
    

    // Log.e("tranX", tranX + "");

    // Log.e("tranY", tranY + "");

    //链接图片是否占空间

    int brandURLLength = 0;

    boolean isBrandUrl = false;

    if (tag.getBrandURL() != null && !tag.getBrandURL().equals("") && !tag.getBrandURL().equals("null")){

    isBrandUrl = true;

    brandURLLength = AndroidUtil.dip2px(context, 25);

    }

    //圆点及箭头及文字空白区域的长度和

    int pointArrowLength = AndroidUtil.dip2px(context, 35);

    //文字的长度

    int textLength = AndroidUtil.getLineLength(tag.getBrand(), 28);

    // Log.e("position", position + "");

    TextView tag_text;

    ImageView tag_brand;

    // 判断左边显示还是右边显示

    if (layoutParams.width - tranX >= brandURLLength + pointArrowLength + textLength){ //右边显示

    tag_layout = LayoutInflater.from(context).inflate(R.layout.right_tag, null);

    tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);

    tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);

    tag_layout.setTranslationX(tranX);

    tag_layout.setTranslationY(tranY);

    } else if (tranX < brandURLLength + pointArrowLength + textLength){ //显示不全

    if (tranX * 2 < layoutParams.width){ //右边显示.但显示不全

    tag_layout = LayoutInflater.from(context).inflate(R.layout.right_tag, null);

    tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);

    tag_text.setWidth((int)(layoutParams.width

    • tranX - brandURLLength - pointArrowLength + AndroidUtil.dip2px(context, 10)));

    tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);

    tag_layout.setTranslationX(tranX);

    tag_layout.setTranslationY(tranY);

    } else { //左边显示 不全

    tag_layout = LayoutInflater.from(context).inflate(R.layout.left_tag, null);

    tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);

    tag_text.setWidth((int)(tranX - brandURLLength - AndroidUtil.dip2px(context, 15)));

    tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);

    tag_layout.setTranslationY(tranY);

    }

    } else { //左边显示

    tag_layout = LayoutInflater.from(context).inflate(R.layout.left_tag, null);

    tag_text = (TextView) tag_layout.findViewById(R.id.tag_text);

    tag_brand = (ImageView) tag_layout.findViewById(R.id.tag_brand);

    tag_layout.setTranslationX(tranX - AndroidUtil.dip2px(context, 25) - textLength - brandURLLength);

    tag_layout.setTranslationY(tranY);

    }

    tag_text.setText(tag.getBrand());

    if (isBrandUrl){

    tag_brand.setVisibility(View.VISIBLE);

    }

    viewHolder.fl.addView(tag_layout);

    return convertView; } else { convertView = lmap.get(position);
    viewHolder = (ViewHolder) convertView.getTag(); return convertView; } } public class ViewHolder{ ImageView user_logo; TextView user_name; TextView add_time; ImageView iv; TextView sign1; TextView sign2; TextView sign3; Button up; Button no_up; TextView up_num; ImageButton say_sth; ImageButton share; FrameLayout fl; TextView describe; } public void addListener(final View convertView,final FollowInfo followInfo, final String image_uri, final int position) {
    final Button no_up = (Button) convertView.findViewById(R.id.no_up); final Button yes_up = (Button) convertView.findViewById(R.id.yes_up); final TextView up_num = (TextView) convertView.findViewById(R.id.up_num); final ImageButton share = (ImageButton) convertView.findViewById(R.id.share); final TextView sign1 = (TextView) convertView.findViewById(R.id.sign1); final TextView sign2 = (TextView) convertView.findViewById(R.id.sign2); final TextView sign3 = (TextView) convertView.findViewById(R.id.sign3); final ImageButton say_sth = (ImageButton) convertView.findViewById(R.id.say_sth); final ImageView user_logo = (ImageView) convertView.findViewById(R.id.user_logo); final TextView user_name = (TextView) convertView.findViewById(R.id.user_name); no_up.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            no_up.setVisibility(View.INVISIBLE);
            yes_up.setVisibility(View.VISIBLE);
            up_num.setText(Integer.parseInt(up_num.getText().toString()) + 1 + "");
            new AsyncUpLoad(followInfo.getImage_id(), followInfo.getUser_id());
        }
    });
    
    yes_up.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            no_up.setVisibility(View.VISIBLE);
            yes_up.setVisibility(View.INVISIBLE);
            up_num.setText(Integer.parseInt(up_num.getText().toString()) - 1 + "");
            new AsyncUpLoad(followInfo.getImage_id(), followInfo.getUser_id());
        }
    });
    

    }

    
    这样应该解决了您的问题,如果仍然存在问题,请提供更多的代码或详细信息以便进一步分析和解决。
    
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备