简体   繁体   中英

How to handle Recyclerview list item click event

I am working with recyclerview but I am trying to intent to next activity but there is not setonlistitem method can any one tell how to redirect to next activity on click of list item,following is my snippet code can any one help me with that

public class SubcategoryActivity extends AppCompatActivity{

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subcatgory_listviews);


        mToolbar = (Toolbar) findViewById(R.id.toolbarsubcategory_list);
        setSupportActionBar(mToolbar);
        setTitle(getString(R.string.app_name));
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerViewsubcategory);


        ItemData itemsData[] = { new ItemData("Help",R.drawable.logo),
                new ItemData("Delete",R.drawable.logo),
                new ItemData("Cloud",R.drawable.logo),
                new ItemData("Favorite",R.drawable.logo),
                new ItemData("Like",R.drawable.logo),
                new ItemData("Rating",R.drawable.logo)};


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        MyAdapter mAdapter = new MyAdapter(itemsData);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());




    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}

You need implement RecyclerView as below example . Then in your holder class, implement OnClickListener . Then use getAdapterPosition() to get current position of clicked Item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM