【问题标题】:Why is my onClicklistener unreachable?为什么我的 onClicklistener 无法访问?
【发布时间】:2016-09-28 11:46:20
【问题描述】:

我在回收站视图中的适配器中有以下代码:

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>

我尝试在按钮单击上打印日志,如您在上面看到的两种不同的方式,但没有运气。可能是什么问题?

【问题讨论】:

  • 你得到什么样的错误?
  • 有时日志不会被打印退出工作室并重新启动并检查它是否会工作但不要重新启动它只需退出并重新启动
  • @0xDEADC0DE 它不会在buttonclick上打印日志。
  • 你能简单解释一下你面临什么样的问题
  • @MohitTrivedi 我试过没有运气,我也试过调试,但看起来这个 onClick 方法从未访问过。

标签: android android-recyclerview onclicklistener


【解决方案1】:

将此设置为行布局的父级 android:descendantFocusability="blocksDescendants" 和 用下面的代码替换你的代码,

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;
    }
 }

【讨论】:

  • @Gabor Hartyandi 试试 rajesh 的代码,希望它对你有用
  • 尝试描述问题所在,而不是只发布解决方案。它将帮助该人将来避免此类问题。
  • 将此设置为行布局的父级 android:descendantFocusability="blocksDescendants"
  • @Rajesh 是的,视图出现了,它应该是怎样的。
  • 将此设置为行布局的父级 android:descendantFocusability="blocksDescendants"
【解决方案2】:

正如上面 cmets 中的许多人所说,问题在于重新启动应用程序。首先,我进入 logcat 中的 Monitors 并关闭正在运行的应用程序,然后再次运行该应用程序。重新启动应用程序后仍然没有出现日志。然后我从我的设备上卸载了该应用程序,然后再次运行。该应用程序在之后运行。

感谢您的帮助和您帮助解决我的问题的时间。

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多