为了账号安全,请及时绑定邮箱和手机立即绑定

如何在没有ListView的ArrayList中添加OnitemClickListener

如何在没有ListView的ArrayList中添加OnitemClickListener

繁星coding 2023-03-31 09:23:26
我想在 android studio 中制作一个仪表板。我使用两个 xml 来制作仪表板 main_activity 和 list_item 活动。在编码会话中,我使用了模型类适配器和主类。public class MainActivity extends AppCompatActivity {ArrayList<DashModel> dashModelArrayList;DashAdapter dashAdapter;private RecyclerView recyclerView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    recyclerView = findViewById(R.id.rv1);    dashModelArrayList = new ArrayList<>();    String heads[] = {"Jobs", "My Profile", "Messages", "Applied Jobs", "Resume", "Settings"};    String subs[] = {"12 new jobs found", "75% complete", "2 new messages", "3 applies jobs", "Edit resume", "Set preferences"};    int images[] = {R.drawable.find_jobs, R.drawable.profile, R.drawable.messages, R.drawable.applied_jobs,            R.drawable.resume, R.drawable.settings};    for (int count = 0; count < heads.length; count++) {        DashModel dashModel = new DashModel();        dashModel.setHead(heads[count]);        dashModel.setSub(subs[count]);        dashModel.setImage(images[count]);        dashModelArrayList.add(dashModel);        //this should be retrieved in our adapter    }    recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));    dashAdapter = new DashAdapter(dashModelArrayList);    recyclerView.setHasFixedSize(true);    recyclerView.setAdapter(dashAdapter);}}如何在此处添加 onItemclickListener?我希望并希望在图标中添加 OnItemclickListener,但我不能也不知道如何添加。
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

要为 RecyclerView 创建一个,onClickListener你必须创建一个类来实现RecyclerView.OnItemTouchListener


import android.support.v7.widget.*;

import android.view.*;

import android.content.*;


public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {


    private GestureDetector gestureDetector;

    private ClickListener clickListener;


    public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {

        this.clickListener = clickListener;

        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

            @Override

            public boolean onSingleTapUp(MotionEvent e) {

                return true;

            }


            @Override

            public void onLongPress(MotionEvent e) {

                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());

                if (child != null && clickListener != null) {

                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));

                }

            }

        });

    }


    @Override

    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {


        View child = rv.findChildViewUnder(e.getX(), e.getY());

        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {

            clickListener.onClick(child, rv.getChildPosition(child));

        }

        return false;

    }


    @Override

    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }


    @Override

    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {


    }


    public interface ClickListener{


        public void onClick(View view,int position);

        public void onLongClick(View view,int position);

    }

}

之后,您可以将其附加到您的设备上RecyclerView以收听点击。


recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getActivity(), recyclerView, new RecyclerViewTouchListener.ClickListener(){


        @Override

        public void onClick(View view, int position)

        {

            // TODO: Implement this method



        }


        @Override

        public void onLongClick(View view, int position)

        {

            // TODO: Implement this method

        }                                           

    }));


查看完整回答
反对 回复 2023-03-31
  • 1 回答
  • 0 关注
  • 129 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信