RecyclerView set Click Listener,点击后没有任何反应

时间:2015-08-22 02:44:44

标签: android android-recyclerview android-viewholder

我正在尝试将Click Listener设置为我的RecyclerView,但是当我点击它时,没有任何反应。

以下是我的适配器代码。我所做的是在我的适配器中的ViewHolder中附加点击事件,

我在这里找不到错误:

public class HomeSetsViewAdapter extends RecyclerView.Adapter<HomeSetsViewAdapter.ViewHolder> {
    private Context context;

    // Pass in the context and posts array into the constructor
    public HomeSetsViewAdapter(Context context, ArrayList<Post> posts) {
        this.posts = posts;
        this.context = context;
    }

    // Store a member variable for the users
    private ArrayList<Post> posts;

    @Override
    public HomeSetsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        // to inflate the item layout and create the holder
        // Inflate the custom layout
        View itemView = LayoutInflater.from(context).inflate(R.layout.layout_viewholderset, parent, false);
        return new HomeSetsViewAdapter.ViewHolder(context, itemView);
    }

    // to set the view attributes based on the data
    // Get the data model based on position
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Post post = posts.get(position);
        if (position % 2 == 0) {
            holder.getmainPic().setImageResource(R.drawable.pic3);
        } else
            holder.getmainPic().setImageResource(R.drawable.pic4);
        // Set item views based on the data model

    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // Your holder should contain a member variable
        // for any view that will be set as you render a row

        private ImageView main_pic;
        private Context context;

        // constructor
        public ViewHolder(View itemView) {
            super(itemView);
            this.main_pic = (ImageView) itemView.findViewById(R.id.main_pic);
            // Attach a click listener to the entire row view
            itemView.setOnClickListener(this);
        }

        // constructor
        public ViewHolder(Context context, View itemView) {
            super(itemView);
            this.main_pic = (ImageView) itemView.findViewById(R.id.main_pic);
            this.context = context;
            // Attach a click listener to the entire row view
            itemView.setOnClickListener(this);
        }

        public ImageView getmainPic() {
            return main_pic;
        }

        public void setmainPic(ImageView main_pic) {
            this.main_pic = main_pic;
        }

        // Handles the row being being clicked
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, HomePostPagerActivity.class);
            context.startActivity(intent);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我没有看到你的项目布局文件(layout_viewholderset.xml),但是你应该设置它的可视和可点击的根视图,如下所示:

android:focusable="true"
android:clickable="true"