如何为此RecyclerView适配器添加页脚

时间:2016-04-10 06:19:39

标签: android android-recyclerview recycler-adapter

我正在使用此问题Is there an addHeaderView equivalent for RecyclerView?中接受的答案将页脚添加到我的RecyclerView适配器,但它似乎无法正常工作。

我在RecyclerView中有十个项目,但页脚仅出现在第一个项目之后,并替换RecyclerView的剩余九个项目,即出现一个RecyclerView项目,剩余的九个项目被替换为九个页脚项目。但它应该是十个RecyclerView项目下面的一个页脚项目。希望你能帮到我?

我的适配器

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

    private ImageLoader imageLoader;
    private Context mContext;

    //List of videos
    private List<VideoItems> mVideoItems;

    private final int VIEW_ITEM = 0;
    private final int VIEW_PROG = 1;

   public VideoAdapter(List<VideoItems> videoItems, Context context) {
       super();

       //Getting all videos
       this.mVideoItems = videoItems;
       this.mContext = context;
   }

    @Override
    public int getItemViewType(int position) {
        if (isPositionItem(position))
            return VIEW_ITEM;
        return VIEW_PROG;
    }

    private boolean isPositionItem(int position) {
       return position == 0;
    }



    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder (ViewGroup parent, int viewType) {
        if (viewType == VIEW_ITEM) {
            View v =  LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.video_summ, parent, false);
            return new VideoViewHolder(v);
        } else if (viewType == VIEW_PROG){
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.recyclerfooter, parent, false);
            return new ProgressViewHolder(v);
        }

        return null;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoViewHolder) {
            VideoItems videoList = mVideoItems.get(position);
            imageLoader = VolleyRequest.getInstance(mContext).getImageLoader();
            imageLoader.get(videoList.getVideo_image(), ImageLoader.getImageListener(((VideoViewHolder) holder).videoImage, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

            ((VideoViewHolder) holder).videoImage.setImageUrl(videoList.getVideo_image(), imageLoader);
            ((VideoViewHolder) holder).videoTitle.setText(videoList.getVideo_title());
            ((VideoViewHolder) holder).videoBody.setText(videoList.getVideo_body());
            ((VideoViewHolder) holder).videoAuthor.setText(videoList.getVideo_author());
            ((VideoViewHolder) holder).videoDate.setText(videoList.getVideo_date());

        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
            ((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
        }


    }


    @Override
    public int getItemCount(){
        //Return the number of items in the data set
        return mVideoItems.size();
    }



    public static class VideoViewHolder extends RecyclerView.ViewHolder{
        public NetworkImageView videoImage;
        public TextView videoTitle, videoAuthor, videoDate, videoBody;
        public ImageButton imageButton;

        VideoViewHolder (View videoView) {
            super(videoView);
            videoImage = (NetworkImageView) videoView.findViewById(R.id.video_image);
            videoTitle = (TextView) videoView.findViewById(R.id.video_title);
            videoBody = (TextView) videoView.findViewById(R.id.video_body);
            videoAuthor = (TextView) videoView.findViewById(R.id.video_author);
            videoDate = (TextView) videoView.findViewById(R.id.video_date);
            imageButton = (ImageButton) videoView.findViewById(R.id.arrow_button);



        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        Button loadButton;
        ProgressBar progressBar;

        public ProgressViewHolder(View footerView){
            super(footerView);
            loadButton = (Button) footerView.findViewById(R.id.reload_button);
            progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);
        }
    }


}

任何想法我出错了以及如何解决它?

注意我使用排球从JSON获取我的项目。

1 个答案:

答案 0 :(得分:2)

private boolean isPositionItem(int position) {
       return position == 0;
    }

此方法仅对第一个项目返回true。因此,只有所有其他项目都是您的回收站视图中的页脚。因为对于标题视图,他们会选择第一个将视图更改为标题的位置。对于页脚视图,您必须更改上一个位置的视图。

将上述方法更改为

private boolean isPositionItem(int position) {
           return position != getItemCount()-1; // last position
        }

现在,您只能在最后一个位置获得一个页脚视图。