简体   繁体   English

如何通过单击来自recyclerview的项目来打开新活动

[英]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 这是我的Mainactivity代码

@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 1-使用构造函数将上下文传递给适配器

2 - In the onBindViewHolder function start your activity like that : 2-在onBindViewHolder函数中,像这样开始活动:

    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: 对于click事件,您需要实现以下onClickListener:

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

        }
    });

(If you take advantage of 'Cntl+ENTER'. This work is much easier.) (如果您利用“ Cntl + ENTER”。这项工作会容易得多。)

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. 您可以在适配器的onBindViewHolder函数中设置onClickListener,并使用intent导航到另一个活动,如下面的示例所示。

@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);

       }
    });


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android RecyclerView如何通过单击项目打开新活动 - Android RecyclerView how to open new activity from clicking the items 如何从 recyclerview 项目打开活动? - How to open an activity from a recyclerview item? 在 RecyclerView 的新活动中打开项目(来自 json 的数据) - Open item in new activity from RecyclerView (data from json) 如何将数据从recyclerview中的单击项传递到ViewModel并打开新活动? - How to pass data from clicked item in recyclerview to viewmodel and open new activity? 如何打开新活动点击列表视图中的项目? - How open new activity clicking an item in listview? 从包含卡片的Recyclerview中打开新活动 - Open new activity from Recyclerview containing cards 从 RecyclerView 打开一个项目作为新活动并显示来自 JSON 的某些数据 - Open an item as new activity from the RecyclerView and display certain data from JSON 如何根据RecyclerView中的单击项在新活动中打开特定片段 - How to open specific Fragment in new Activity depending on clicked item in RecyclerView 在Firebase RecyclerView中单击项目时如何打开新活动 - How to open a new activity when an item is clicked in Firebase RecyclerView 如何在RecyclerView中使用不同的数据和照片在项目单击上打开新的活动 - How to open new Activity on item click with different data and photo in RecyclerView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM