我正在使用recyclerView
在listitems
中显示我的navigation drawer
。我已经实施了onclickListener
,但我一直坚持如何打开另一个activity
1}} 项为clicked
时。截至目前,根据代码,我的所有项目都会在项目点击时显示,toast
显示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();
}
};
}
&#13;
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
答案 0 :(得分:78)
您可以(但不需要,因为ViewHolder
类不是静态的)创建字段上下文,如下所示:
private final Context context;
public MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
...
}
并在您的onClick
方法上调用如下所示:
@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);
}
答案 1 :(得分:3)
您可以实现适配器的onClickListener:
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder>implements View.OnClickListener
并使用其中的方法接口
public interface mClickListener {
public void mClick(View v, int position);
}
并在你的onClick方法中调用界面中的方法并将其传递给视图和位置
在您的主要活动中实现该界面
public class MainActivity extends ActionBarActivity implements AdapterClass.mClickListener
并覆盖该方法
@Override
public void onCommentsClick(View v, int position) {
final Intent intent = new Intent(this, OtherActivity.class);
}
最好通过活动而不是其他类来管理您的活动转换
答案 2 :(得分:2)
很久以前就问过这个问题,无论如何,我会写一个简单的答案,可能对某人有帮助。
将clickListener附加到RecyclerView项目的最简单方法是在Adapter中,如下所示:
public class MyAdapter extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
....
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), YourActivity.Class);
startActivity(intent);
}
});
}
答案 3 :(得分:1)
简单地说,您可以轻松完成......您只需在此处查看您的活动背景。
//Create intent getting the context of your View and the class where you want to go
Intent intent = new Intent(view.getContext(), YourClass.class);
//start the activity from the view/context
view.getContext().startActivity(intent); //If you are inside activity, otherwise pass context to this funtion
请记住,您需要修改AndroidManifest.xml并放置活动...
<activity
android:name=".YourClass"
android:label="Label for your activity"></activity>
答案 4 :(得分:1)
示例,在 MyRecyclerViewAdapter 中添加方法:
public interface ItemClickListener {
void onItemClick(View view, int position);
}
在 MainActivity.java
中@Override
public void onItemClick(View view, int position) {
Context context=view.getContext();
Intent intent=new Intent();
switch (position){
case 0:
intent = new Intent(context, ChildActivity.class);
context.startActivity(intent);
break;
}
}
答案 5 :(得分:1)
在将Glide用于ImageView时或在recyclerview中将intent用于项目onClick时,在声明上下文时发生问题。我发现这对我有用,可以帮助我声明要在Glide,Intent或Toast中使用的上下文。
public class NoteAdapter extends FirestoreRecyclerAdapter<Note,NoteAdapter.NoteHolder> {
Context context;
public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) {
holder.r_tv.setText(model.getTitle());
Glide.with(CategoryActivity.context).load(model.getImage()).into(holder.r_iv);
context = holder.itemView.getContext();
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, SuggestActivity.class);
context.startActivity(i);
}
});
}
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category,parent,false);
return new NoteHolder(v);
}
public static class NoteHolder extends RecyclerView.ViewHolder
{
TextView r_tv;
ImageView r_iv;
public NoteHolder(@NonNull View itemView) {
super(itemView);
r_tv = itemView.findViewById(R.id.r_tv);
r_iv = itemView.findViewById(R.id.r_iv);
}
}
}
答案 6 :(得分:0)
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();
}
};
}
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();
}
};
}
答案 7 :(得分:0)
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<Android> android;
Context context;
private ImageView img;
public DataAdapter(Context contextN, ArrayList<Android> android) {
this.android = android;
this.context=contextN;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_list, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(android.get(i).getOffer());
viewHolder.tv_version.setText(android.get(i).getOfferType());
Picasso.with(context).load(android.get(i).getImg()).transform(new CircleTransform()).into(img);
}
@Override
public int getItemCount() {
return android.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
img = (ImageView) view.findViewById(R.id.img);
context = itemView.getContext();
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int itemPosition = getLayoutPosition();
Toast.makeText(context, "" + itemPosition, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,Show.class);
intent.putExtra("name",""+android.get(itemPosition).getOffer());
intent.putExtra("img",""+android.get(itemPosition).getImg());
context.startActivity(intent);
}
});
}
}
}
答案 8 :(得分:0)
这个问题很早以前就被问到了,但是尽管Milad Moosavi`s answer非常接近,但以上答案都没有帮助我。 要从“回收者”视图上的特定位置打开新活动,以下代码可能会有所帮助:
@Override
public void onBindViewHolder(@NonNull TripViewHolder holder, int position) {
Trip currentTrip = trips.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), EditTrip.class);
v.getContext().startActivity(intent);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent(v.getContext(), ReadTripActivity.class);
v.getContext().startActivity(intent);
return false;
}
});
}
答案 9 :(得分:-2)
iconView =(ImageView)itemLayoutView .findViewById(R.id.iconId);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), SecondPage.class);
v.getContext().startActivity(intent);
Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
}
});