李堇 2022-06-07 18:08 采纳率: 52.8%
浏览 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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝