简体   繁体   English

Recycler View中的意图?

[英]Intents inside a Recycler View?

Could anyone help me out! 任何人都可以帮助我! Cant get the normal way of writing intent to launch a new activity inside my recycler view to work! 无法正常编写意图,以便在我的回收站视图中启动新活动! Is there a new way you must do it? 有必要这样做的新方法吗? Any help would be hugely appreciated! 任何帮助将非常感谢!

Here is my View Holder Code; 这是我的View Holder Code;

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView, TextView textView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listImage);
        itemView.setOnClickListener(this);
        title = textView;
    }

    @Override
    public void onClick(View v) {
        if (getPosition() == 0) {
            Toast.makeText(v.getContext(), "Actvity 1", Toast.LENGTH_SHORT).show();
        }
        if (getPosition() == 1) {
            Toast.makeText(v.getContext(), "Actvity 2", Toast.LENGTH_SHORT).show();
        }
    }
  }
}

Initial click listeners in your viewHolder. 在viewHolder中初始单击侦听器。

/**
 * see {@link <a href="https://youtu.be/imsr8NrIAMs?t=2163">Official Video</a>}
 */
public static class MyViewHolder extends RecyclerView.ViewHolder {

  public MyViewHolder(View itemView) {
    super(itemView);
    //TODO: findViewById or ButterKnife
    ....
    //set item view listener
    itemView.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View itemView) {
        if (getAdapterPosition() != RecyclerView.NO_POSITION) {
          Context c = itemView.getContext();
          //TODO: startActivity
        }
      }
    });
    //TODO: set other child views' listener
    ....
  }
}

In your custom adapter create your own constructor. 在自定义适配器中创建自己的构造函数。 for Example: 例如:

  private Context context;
    public MyRecyclerViewAdapter(Context context,ArrayList<String> yourList){
            this.context=context;
            // further code
    }

and while calling your adapter just pass the reference of your context and now you can able to call intent via 并且在调用适配器时只需传递上下文的引用,现在您可以通过调用intent

context.startActivity() & all other methods. context.startActivity()和所有其他方法。

The ViewHolder is just a container for the inner View objects within your specific layout for the item. ViewHolder只是项目特定布局中内部View对象的容器。 Neither it or the RecyclerView.Adapter for your implementation have direct access to the Context object needed to send an Intent . 它或您的实现的RecyclerView.Adapter都不能直接访问发送Intent所需的Context对象。 Rather than set your OnClickListener here, put it in your adapter's onBindViewHolder() . 而不是在这里设置OnClickListener ,而是将它放在适配器的onBindViewHolder() Your adapter can be notified when its owning RecyclerView is attached to it, so you can save a reference to it in a private field. 您的适配器可以在其拥有的RecyclerView附加到适配器时得到通知,因此您可以在私有字段中保存对它的引用。 From that view you can call getContext() so you could then create an Intent and send it via the usual means. 从该视图中,您可以调用getContext()这样您就可以创建一个Intent并通过常规方式发送它。

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

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