简体   繁体   中英

How to open new activity from clicking an item from recyclerview

how do you open a new empty activity when I click my List view item?

I got the codes from another source, I can do it with a button, but confused to do it on a custom recycler view?

This is my Mainactivity code

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Model, ViewHolder>(
                    Model.class,
                    R.layout.row,
                    ViewHolder.class,
                    mDbRef
            ) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {

                    viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getImage());

You have to follow these steps:

1 - Pass the Context to your adapter using the Constructor

2 - In the onBindViewHolder function start your activity like that :

    holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              Intent intent =  new Intent(context, ActivityToStart.class);
              context.startActivity(intent);
        }
    });

For clicking event, you need to implement an onClickListener as below:

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

(If you take advantage of 'Cntl+ENTER'. This work is much easier.)

And for the changing the screen(or Activity).

You can do it like this:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

I hope this is helpful.

You can set onClickListener in the onBindViewHolder function in your adapter.And use intent to navigate to another activity Like below example.

@Override
public void onBindViewHolder(final LeaderBoardAdapter.MyViewHolder holder, final int 
position) {

    //holder.newsUrl.setText(newsItemList.get(position).getUrl());
    holder.newsDescription.setText(newsItemList.get(position).getDescription());

    holder.newsUrl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext,ReadMoreActivity.class);
            intent.putExtra("title",newsItemList.get(position).getTitle());

            mContext.startActivity(intent);

       }
    });


}

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