RecyclerView适配器
public class SongsAdapter extends RecyclerView.Adapter<SongsAdapter.MyViewHolder>{
Context context;
ArrayList<HashMap<String, String>> songList;
public SongsAdapter(ArrayList<HashMap<String, String>> songList,Context context) {
this.songList = songList;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(view,context);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(songList.get(position).get("file_name"));
}
@Override
public int getItemCount() {
return songList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
Context context;
public MyViewHolder(View itemView, Context context){
super(itemView);
this.context = context;
textView=(TextView)itemView.findViewById(R.id.listitemtextview);
}
@Override
public void onClick(View v) {
int position=getAdapterPosition();
Log.d(TAG, "onClick: ");
Toast.makeText(context,"Hi",Toast.LENGTH_LONG).show();
}
}
}
LIST_ITEM
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:backgroundTint="@color/tan_background"
android:padding="8dp"
android:layout_margin="20dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textStyle="bold"
android:id="@+id/listitemtextview"/>
</LinearLayout>
</android.support.v7.widget.CardView>
点击recyclerview无效。我想点击recyclerview进入下一个活动。请帮忙。我尝试了一切,但没有任何作用。我经历了stackoverflow的所有相关主题的问题。我的CardView布局中没有任何onclick = true
答案 0 :(得分:0)
在itemView.setOnClickListner(this)
public MyViewHolder(View itemView, Context context)
答案 1 :(得分:0)
根据android点击监听器你需要先用监听器注册然后你可以在onClick()中获取事件所以首先用clickeble对象注册监听器然后你可以在onClick()中获取事件或者你可以直接注册使用匿名类,如下面的方式
itemView.setOnClickListener(this)
或
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});