如何获取RecyclerView项目的ViewHolder

时间:2016-03-30 09:34:38

标签: android android-recyclerview material-design

我正在使用RecyclerView,我想在卡片上为我的图像添加视差效果。如果我使用ParallaxEveryWhere库它可以正常工作,但我想手动创建它。

现在问题是我想从我的适配器中获取所有项目的ViewHolder。我已经尝试了所有的RecyclerView方法来获取项目的Viewholder,但所有这些方法都返回了null。

我正在寻找一种解决方案,以便从它的适配器中获取物品的ViewHolder

2 个答案:

答案 0 :(得分:1)

这是RecyclerView的骨架

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {

    private Context context;
    public YourAdapter( Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.card_item, viewGroup, false);
        ViewHolder viewHolder;
        viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

         viewHolder.textView.setText("Text");
         viewHolder.imgView.setImageResource(R.drawable.image);

    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView textView;
        public ImageView imgView;
        public ViewHolder(View itemView) {
            super(itemView);

            textView = (TextView) itemView.findViewById(R.id.textView);
            imgView = (ImageView) itemView.findViewById(R.id.imgView);

        }

    }

}

例如,如果在ViewHolder中将TextView和ImageView作为项目,则可以在onBindViewHolder()方法

中访问这些项目

答案 1 :(得分:0)

在上面的代码中有返回0,因此它将返回0, 在那里放一些值,所以适配器返回一些值。

public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {

        private Context context;
        public YourAdapter( Context context) {
            this.context = context;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.card_item, viewGroup, false);
            ViewHolder viewHolder;
            viewHolder = new ViewHolder(v);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder viewHolder, final int i) {

             viewHolder.textView.setText("Text");
             viewHolder.imgView.setImageResource(R.drawable.image);

        }

        @Override
        public int getItemCount() {
            return imgView.length;
        }

        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

            public TextView textView;
            public ImageView imgView;
            public ViewHolder(View itemView) {
                super(itemView);

                textView = (TextView) itemView.findViewById(R.id.textView);
                imgView = (ImageView) itemView.findViewById(R.id.imgView);

            }

        }

    }

我希望你的问题会随之而来......