Recyclerview
项目点击并不总是有效,有时可以工作,但有更多点击(不知道点击了多少)。如果只点击一次无法点击。
注意前几天我尝试点击一次就可以了,但是现在当我尝试使用AS再次运行时它不起作用,即使我没有修改该文件
我的代码
public class RecyclerViewAdapterRiwayat extends RecyclerView.Adapter<RecyclerViewAdapterRiwayat.ViewHolder> {
Context context;
private static final String TAG = RecyclerViewAdapterRiwayat.class.getSimpleName();
List<GetDataAdapterRiwayat> getDataAdapter;
ImageLoader imageLoader1;
String FIXURL = "http://192.168.1.101/AndroidFileUpload/";
String url = FIXURL + "uploads/";
String StatusRiwayat;
public RecyclerViewAdapterRiwayat(List<GetDataAdapterRiwayat> getDataAdapter, Context context) {
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items_riwayat, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
final GetDataAdapterRiwayat getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(url+getDataAdapter1.getFotoSetelahRiwayat(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.networkImageView.setImageUrl(url+getDataAdapter1.getFotoSetelahRiwayat(), imageLoader1);
Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());
Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView;
public NetworkImageView networkImageView;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item_riwayat);
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView2);
}
}
}
答案 0 :(得分:3)
如果您在滚动/推送RecyclerView后只遇到问题,那么它是SupportLibrary中的一个已知错误
用户滚动后,他们无法点击RecyclerView中的项目。 (AOSP问题66996774)
Issuetracker ID 66996774以及69823266因为它仍未在27.0.1中修复。即使Google声明it has fixed it。
最重要的是:这里的解决方案(来自Chris Banes,Googler)非常有效:https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2
答案 1 :(得分:0)
试试这个:
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView;
public NetworkImageView networkImageView;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item_riwayat);
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getLayoutPosition(); // use this to get item from list
Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());
Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
context.startActivity(intent);
}
});
}
答案 2 :(得分:0)
您的代码
Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());
Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
context.startActivity(intent);
}
});
每次在itemView上创建新的侦听器,而是在分配新的侦听器之前实现OnClickListener或传递 null
Viewholder.itemView.setOnClickListener(null);
Viewholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());
Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
context.startActivity(intent);
}
});
或在ViewHolder
onCreateViewHolder()
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewHolder.getAdapterPosition(); // use this to get item from list
Log.d(TAG, "coba nih5 : "+getDataAdapter1.getNamaJalanRiwayat());
Intent intent = new Intent(v.getContext(), DetailRiwayatActivity.class);
context.startActivity(intent);
}
});
答案 3 :(得分:0)
将这些添加到R.layout.recyclerview_items_riwayat
的父元素中android:clickable="false"
android:focusable="false"
答案 4 :(得分:-1)
这可能是由于父级的onTouch
方法拦截了触摸事件。您需要检查是否有像SwipeLayout
这样的父视图。