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

我想在 android studio 中制作一个仪表板。我使用两个 xml 来制作仪表板 main_activity 和 list_item 活动。在编码会话中,我使用了模型类适配器和主类。


public class MainActivity extends AppCompatActivity {


ArrayList<DashModel> dashModelArrayList;

DashAdapter dashAdapter;

private RecyclerView recyclerView;


@Override

protected 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,但我不能也不知道如何添加。


繁星coding
浏览 130回答 1
1回答

心有法竹

要为 RecyclerView 创建一个,onClickListener你必须创建一个类来实现RecyclerView.OnItemTouchListenerimport android.support.v7.widget.*;import android.view.*;import android.content.*;public class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {&nbsp; &nbsp; private GestureDetector gestureDetector;&nbsp; &nbsp; private ClickListener clickListener;&nbsp; &nbsp; public RecyclerViewTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {&nbsp; &nbsp; &nbsp; &nbsp; this.clickListener = clickListener;&nbsp; &nbsp; &nbsp; &nbsp; gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onSingleTapUp(MotionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onLongPress(MotionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View child = recyclerView.findChildViewUnder(e.getX(), e.getY());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (child != null && clickListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickListener.onLongClick(child, recyclerView.getChildPosition(child));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; View child = rv.findChildViewUnder(e.getX(), e.getY());&nbsp; &nbsp; &nbsp; &nbsp; if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clickListener.onClick(child, rv.getChildPosition(child));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTouchEvent(RecyclerView rv, MotionEvent e) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {&nbsp; &nbsp; }&nbsp; &nbsp; public interface ClickListener{&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view,int position);&nbsp; &nbsp; &nbsp; &nbsp; public void onLongClick(View view,int position);&nbsp; &nbsp; }}之后,您可以将其附加到您的设备上RecyclerView以收听点击。recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getActivity(), recyclerView, new RecyclerViewTouchListener.ClickListener(){&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view, int position)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Implement this method&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onLongClick(View view, int position)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: Implement this method&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java