李堇 2022-06-07 18:08 采纳率: 48.7%
浏览 31
已结题

ListView怎么实现每个列中的控件点击事件

当fragment实现OnItemClickListener时只能知道某一行进行了点击,但是无法知道具体是那个控件触发点击,代码如下:

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        SubjectPainPointDetailFragment subjectPainPointDetailFragment = new SubjectPainPointDetailFragment(fManager);
        LogUtils.e("=1=","list.size :" + alllist.size());
        LogUtils.e("=1=","position :" + position);
        Bundle bd = new Bundle();
        bd.putString("id", alllist.get(position-1).getId());
        bd.putString("fileUrl",alllist.get(position-1).getFileUrl());
        LogUtils.e("=1=","onItemClick id:" + bd.get("id"));
        LogUtils.e("=1=","onItemClick fileUrl:" + bd.get("fileUrl"));
        subjectPainPointDetailFragment.setArguments(bd);
        //加上Fragment替换动画
        fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit);
        fTransaction.replace(R.id.centerFragment, subjectPainPointDetailFragment);
        //调用addToBackStack将Fragment添加到栈中
        fTransaction.addToBackStack(null);
        fTransaction.commit();
    }

当在Adapter的getView中找到控件绑定OnClickListener时可以获取控件的触发,但是因为是内部类,参数无法传递,item数据拿不到,导致无法将item中的参数传递到新fragment,代码如下:

img

请问怎处理?

  • 写回答

2条回答 默认 最新

  • 李堇 2022-06-09 15:02
    关注

    已实现,具体做法是在adapter中定义一个实现了View.OnClickListener类,通过构造方法传递对应的参数或者位置之类的信息,然后重写onClick方法,在adapter的getView中去找到对应的view去绑定OnClickListener事件即可,代码如下:

    public class AiErrorQuestionCollectAdapter extends  BaseCommonAdapter<TRAIContactBook>{
    
        private FragmentManager fManager;
    
        public AiErrorQuestionCollectAdapter(Context context, List<TRAIContactBook> list,FragmentManager fManager) {
            super(context, list);
            this.fManager = fManager;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = new ViewHolder(mContext, parent, R.layout.item_list_ai_error_question_collect, position);
    
            holder.setText(R.id.pTime,DateUtils.formatDate(mDatas.get(position).getUpdateTime()));
            holder.setText(R.id.rName,mDatas.get(position).getContactBookName());
            holder.setText(R.id.df,mDatas.get(position).getDifficultyName());
            holder.setText(R.id.grade,mDatas.get(position).getGradeName());
            holder.setText(R.id.course,mDatas.get(position).getCourseName());
            holder.setText(R.id.isPrint,mDatas.get(position).getPrintStatusName());
            holder.setText(R.id.status,mDatas.get(position).getStatusName());
    
            View qBtn = holder.getView(R.id.qBtn);
            qBtn.setOnClickListener(new myOnClickListener(mDatas.get(position),"0001"));
            View aBtn = holder.getView(R.id.aBtn);
            aBtn.setOnClickListener(new myOnClickListener(mDatas.get(position),"0002"));
            return holder.getConvertView();
        }
    
    
        class myOnClickListener implements View.OnClickListener {
            private TRAIContactBook itemData;
            private String type;
            public myOnClickListener(TRAIContactBook info,String type) {
                this.itemData = info;
                this.type = type;
            }
            @Override
            public void onClick(View view) {
                LogUtils.e("=1=","onItemClick id:" + itemData.getId());
                LogUtils.e("=1=","onItemClick fileUrl:" + itemData.getQuestionPdfUrl());
                LogUtils.e("=1=","onItemClick fileUrl:" + itemData.getAnswerPdfUrl());
    
                FragmentTransaction fTransaction = fManager.beginTransaction();
                Bundle bd = new Bundle();
                if("0001".equals(type)) {
                    AiErrorQuestionCollectQpdfFragment aiErrorQuestionCollectQpdfFragment = new AiErrorQuestionCollectQpdfFragment(fManager);
    
                    bd.putString("id", itemData.getId());
                    bd.putString("fileUrl",itemData.getQuestionPdfUrl());
                    LogUtils.e("=1=","onItemClick id:" + bd.get("id"));
                    LogUtils.e("=1=","onItemClick fileUrl:" + bd.get("fileUrl"));
                    aiErrorQuestionCollectQpdfFragment.setArguments(bd);
                    fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit);
                    fTransaction.replace(R.id.centerFragment, aiErrorQuestionCollectQpdfFragment);
                } else if("0002".equals(type)) {
                    AiErrorQuestionCollectApdfFragment aiErrorQuestionCollectApdfFragment = new AiErrorQuestionCollectApdfFragment(fManager);
                    bd.putString("id", itemData.getId());
                    bd.putString("fileUrl",itemData.getAnswerPdfUrl());
                    LogUtils.e("=1=","onItemClick id:" + bd.get("id"));
                    LogUtils.e("=1=","onItemClick fileUrl:" + bd.get("fileUrl"));
                    aiErrorQuestionCollectApdfFragment.setArguments(bd);
                    fTransaction.replace(R.id.centerFragment, aiErrorQuestionCollectApdfFragment);
                } else if("0003".equals(type)) {
    
                }
                //加上Fragment替换动画
                fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit);
                //调用addToBackStack将Fragment添加到栈中
                fTransaction.addToBackStack(null);
                fTransaction.commit();
            }
        }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月17日
  • 已采纳回答 6月9日
  • 创建了问题 6月7日

悬赏问题

  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。