如何在 recyclerView 项目上打开不同的活动 onclick [英] How to open a different activity on recyclerView item onclick

查看:14
本文介绍了如何在 recyclerView 项目上打开不同的活动 onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 recyclerViewnavigation drawer 中显示我的 listitems.我已经实现了 onclickListener但是当项目点击时,我一直被困在如何打开一个不同的activity.根据代码,我的所有项目现在都在项目点击时显示 toastitem 位置.

i am using a recyclerView to show my listitems in the navigation drawer.I have implemented the onclickListener but i have been stuck on how to open a different activity when items are clicked. All my items do on item click as of now as per the code is to display a toast with the item position.

非常感谢您的帮助.

AdapterClass.java

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    private LayoutInflater inflater;
    private Context context;
List<Information>data= Collections.emptyList();
    public AdapterClass(Context context,List<Information>data){
        this.context=context;

        inflater= LayoutInflater.from(context);
        this.data=data;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view= inflater.inflate(R.layout.custom_row,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);
        holder.icon.setImageResource(current.iconId);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView title;
        ImageView icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.listText);
           icon=(ImageView)itemView.findViewById(R.id.listIcon);
            itemView.setClickable(true);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            Toast.makeText(context,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();
        }
    };
}

Log  cat error after implementing Konrad's solution

    02-27 15:24:52.833: D/AndroidRuntime(1630): --------- beginning of crash
02-27 15:24:52.834: E/AndroidRuntime(1630): FATAL EXCEPTION: main
02-27 15:24:52.834: E/AndroidRuntime(1630): Process: com.snappy.stevekamau.snappy, PID: 1630
02-27 15:24:52.834: E/AndroidRuntime(1630): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snappy.stevekamau.snappy/com.snappy.stevekamau.snappy.YourActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.os.Looper.loop(Looper.java:135)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at java.lang.reflect.Method.invoke(Native Method)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at java.lang.reflect.Method.invoke(Method.java:372)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-27 15:24:52.834: E/AndroidRuntime(1630): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:95)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:88)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:84)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.app.ActionBarActivityDelegateBase.setSupportActionBar(ActionBarActivityDelegateBase.java:175)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.support.v7.app.ActionBarActivity.setSupportActionBar(ActionBarActivity.java:92)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at com.snappy.stevekamau.snappy.YourActivity.onCreate(YourActivity.java:18)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.Activity.performCreate(Activity.java:5933)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-27 15:24:52.834: E/AndroidRuntime(1630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-27 15:24:52.834: E/AndroidRuntime(1630):     ... 10 more
02-27 15:24:52.839: W/ActivityManager(464):   Force finishing activity com.snappy.stevekamau.snappy/.YourActivity
02-27 15:24:52.841: W/ActivityManager(464):   Force finishing activity com.snappy.stevekamau.snappy/.MainActivity

推荐答案

您可以(但不需要,因为 ViewHolder 类不是静态的)创建字段上下文,如下所示:

You can (but don't need to because the ViewHolder class is not static) create field context as is shown below:

private final Context context;

public MyViewHolder(View itemView) {
    super(itemView);
    context = itemView.getContext();
    ...
}

并在您的 onClick 方法上调用如下所示:

and on your onClick method just call sth like below:

@Override
public void onClick(View v) {          

    final Intent intent;
    switch (getAdapterPostion()){
        case 0:
           intent =  new Intent(context, FirstActivity.class);
           break;

        case 1:
            intent =  new Intent(context, SecondActivity.class);
            break;
           ...
        default:
           intent =  new Intent(context, DefaultActivity.class);
           break;
     }
    context.startActivity(intent);
}

@Override
public void onClick(View v) {          

    final Intent intent;
    if (getAdapterPosition() == sth){
       intent =  new Intent(context, OneActivity.class);
    } else if (getPosition() == sth2){
       intent =  new Intent(context, SecondActivity.class);
    } else {
       intent =  new Intent(context, DifferentActivity.class);
    }
    context.startActivity(intent);
}

这篇关于如何在 recyclerView 项目上打开不同的活动 onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆