我在适配器中的Recyclerview中有以下代码:
public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {
private Context context;
Cursor dataCursor;
public MyMovieAdapter(Context context, Cursor cursor,) {
this.context = context;
this.dataCursor = cursor;
}
@Override
public MyMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_movies_listrow, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
dataCursor.moveToPosition(i);
viewHolder.my_movie_delete_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.e("AAAA", "It doesn't print that log on a button click");
}
});
viewHolder.tv_original_title.setText(dataCursor.getString(1));
Picasso.with(context).load(dataCursor.getString(9)).resize(150,225).into(viewHolder.img_my_movie);
}
@Override
public int getItemCount() {
return dataCursor.getCount();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tv_original_title;
ImageView img_my_movie;
Button my_movie_delete_button;
public ViewHolder(View view) {
super(view);
tv_original_title = (TextView)view.findViewById(R.id.tv_original_title);
img_my_movie = (ImageView)view.findViewById(R.id.img_my_movie);
my_movie_delete_button = (Button)view.findViewById(R.id.my_movies_delete_button);
my_movie_delete_button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Log.e("AAAA", "Neither like that");
}
}
public Cursor swapCursor(Cursor cursor) {
if (dataCursor == cursor) {
return null;
}
Cursor oldCursor = dataCursor;
this.dataCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}
}
我列表中一行的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<RelativeLayout
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="225dp"
android:id="@+id/img_my_movie"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"/>
<TextView
android:layout_marginTop="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_original_title"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/img_my_movie" />
<Button
android:id="@+id/my_movies_delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
android:layout_alignBaseline="@+id/tv_original_title"
android:layout_alignBottom="@+id/tv_original_title"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
我已经尝试在按钮上打印一个日志点击两种不同的方式,如上所示,没有运气。可能是什么问题?
答案 0 :(得分:0)
将此设置为行布局android的父级:descendantFocusability =&#34; blocksDescendants&#34; 和 用以下代码替换您的代码,
public class MyMovieAdapter extends RecyclerView.Adapter<MyMovieAdapter.ViewHolder> {
private Context context;
Cursor dataCursor;
public MyMovieAdapter(Context context, Cursor cursor,) {
this.context = context;
this.dataCursor = cursor;
}
@Override
public MyMovieAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_movies_listrow, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
dataCursor.moveToPosition(i);
viewHolder.my_movie_delete_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.e("AAAA", "It doesn't print that log on a button click");
}
});
viewHolder.tv_original_title.setText(dataCursor.getString(1));
Picasso.with(context).load(dataCursor.getString(9)).resize(150,225).into(viewHolder.img_my_movie);
}
@Override
public int getItemCount() {
return dataCursor.getCount();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tv_original_title;
ImageView img_my_movie;
Button my_movie_delete_button;
public ViewHolder(View view) {
super(view);
tv_original_title = (TextView)view.findViewById(R.id.tv_original_title);
img_my_movie = (ImageView)view.findViewById(R.id.img_my_movie);
my_movie_delete_button = (Button)view.findViewById(R.id.my_movies_delete_button);
}
}
public Cursor swapCursor(Cursor cursor) {
if (dataCursor == cursor) {
return null;
}
Cursor oldCursor = dataCursor;
this.dataCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}
}
答案 1 :(得分:0)
许多人在上面的评论中说,问题在于重启应用程序。首先,我在logcat中访问监视器并关闭正在运行的应用程序,然后再次运行应用程序。重新启动应用程序后仍未显示日志。然后我从我的设备上卸载了应用程序,然后再次运行。该应用程序在之后工作。
感谢您的帮助和时间来帮助解决我的问题。