Android 기본 어댑터 아래쪽 및 헤드 레이아웃 추가

8334 단어
            getItemViewType ,getItemCount,onCreateViewHolder  (getItemCount size+1          )
@Override
public int getItemViewType(int position) {   position          
    if (position == resultData.size()) {
        return FOOTER_TYPE;
    } else {
        return ITEM_TYPE;
    }
}
@Override
public int getItemCount() {
    return resultData.size() > 0 ? resultData.size() + 1 : 0;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    if (viewType == FOOTER_TYPE) {
        return new ScrollViewHolder(inflater.inflate(R.layout.item_scroll_layout2, parent, false));
    } else {
        return new FooterViewHolder(inflater.inflate(R.layout.comment_footer, parent, false));
    }
}

 

package com.guwu.varysandroid.ui.data.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.guwu.varysandroid.R;
import com.guwu.varysandroid.bean.GetInformationAccountBean;
import com.guwu.varysandroid.utils.SynScrollerLayout;

import java.util.List;

public class DataAnalysisAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private SynScrollerLayout mSynScrollerview;
    private String constType;
    private final int FOOTER_TYPE = 1;
    private final int ITEM_TYPE = 2;
    private final int TOP_TYPE = 3;
    private LayoutInflater inflater;
    private List resultData;

    public DataAnalysisAdapter(Context context, List resultData, SynScrollerLayout mSynScrollerview, String type) {
        this.mContext = context;
        this.mSynScrollerview = mSynScrollerview;
        this.constType = type;
        this.resultData = resultData;
        this.inflater = LayoutInflater.from(context);
    }

    public void setNewData(List resultData) {
        this.resultData = resultData;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == FOOTER_TYPE) {
            return new ScrollViewHolder(inflater.inflate(R.layout.item_scroll_layout2, parent, false));
        } else {
            return new FooterViewHolder(inflater.inflate(R.layout.comment_footer, parent, false));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ScrollViewHolder) {
            ScrollViewHolder itemHolder = (ScrollViewHolder) holder;
            if (resultData.size() > 0) {
                itemHolder.mView.setText(String.valueOf(resultData.get(position).getDate()));
                if (TextUtils.equals("user_type", constType)) {
                    itemHolder.tvPlatform.setText(resultData.get(position).getPlatName());
                    itemHolder.tv_fans.setText(String.valueOf(resultData.get(position).getFansTotalCnt()));
                    itemHolder.tv_fans_num.setText(String.valueOf(resultData.get(position).getFansCnt()));
                    itemHolder.tv_video.setText("");
                } else if (TextUtils.equals("platform_type", constType)) {
                    itemHolder.tvPlatform.setText(resultData.get(position).getPlatName());
                    itemHolder.tv_fans.setText(String.valueOf(resultData.get(position).getArticleCnt()));
                    itemHolder.tv_fans_num.setText(String.valueOf(resultData.get(position).getArticleCnt()));
                    itemHolder.tv_video.setText(String.valueOf(resultData.get(position).getVideoCnt()));
                    itemHolder.tv_read.setText(String.valueOf(resultData.get(position).getReadCnt()));
                    itemHolder.tv_play.setText(String.valueOf(resultData.get(position).getPlayCnt()));
                    itemHolder.tv_like.setText(String.valueOf(resultData.get(position).getLikeCnt()));
                    itemHolder.tv_share.setText(String.valueOf(resultData.get(position).getShareCnt()));
                    itemHolder.tv_collect.setText(String.valueOf(resultData.get(position).getCollectCnt()));
                    itemHolder.tv_comments.setText(String.valueOf(resultData.get(position).getCommentCnt()));
                    itemHolder.tv_recommend.setText(String.valueOf(resultData.get(position).getRecommendCnt()));
                }
                if (position % 2 == 0) {
                    itemHolder.ll_layout_item.setBackground(mContext.getDrawable(R.drawable.user_analyze_top));
                } else {
                    itemHolder.ll_layout_item.setBackground(mContext.getDrawable(R.drawable.user_analyze_top_while));
                }
                mSynScrollerview.setOnScrollListener((l, t, oldl, oldt) -> itemHolder.mSynScrollerLayout.smoothScrollTo(l, 0));//      RecyclerView        
            }
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerHolder = (FooterViewHolder) holder;
            footerHolder.mTVFooter.setText("      ");
        }
    }


    @Override
    public int getItemCount() {
        return resultData.size() > 0 ? resultData.size() + 1 : 0;
    }

    @Override
    public int getItemViewType(int position) { //  position          
        if (position == resultData.size()) {
            return FOOTER_TYPE;
        }else if(position == 0){
             return TOP_TYPE;
        } else {
            return ITEM_TYPE;
        }
    }

    private class ScrollViewHolder extends RecyclerView.ViewHolder {
        private TextView mView;
        private SynScrollerLayout mSynScrollerLayout;
        private LinearLayout mChildRoot, ll_layout_item;
        private TextView tv_fans;
        private TextView tvPlatform;
        private TextView tv_fans_num;
        private TextView tv_video;
        private TextView tv_read;
        private TextView tv_play;
        private TextView tv_like;
        private TextView tv_share;
        private TextView tv_collect;
        private TextView tv_comments;
        private TextView tv_recommend;

        private ScrollViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView.findViewById(R.id.tv);
            mSynScrollerLayout = itemView.findViewById(R.id.syn_scroller);
            ll_layout_item = itemView.findViewById(R.id.ll_layout_item);
            mChildRoot = itemView.findViewById(R.id.ll_child_root);
            tv_fans = itemView.findViewById(R.id.tv_fans);
            tvPlatform = itemView.findViewById(R.id.tv_platform);
            tv_fans_num = itemView.findViewById(R.id.tv_fans_num);
            tv_video = itemView.findViewById(R.id.tv_video);
            tv_read = itemView.findViewById(R.id.tv_read);
            tv_play = itemView.findViewById(R.id.tv_play);
            tv_like = itemView.findViewById(R.id.tv_like);
            tv_share = itemView.findViewById(R.id.tv_share);
            tv_collect = itemView.findViewById(R.id.tv_collect);
            tv_comments = itemView.findViewById(R.id.tv_comments);
            tv_recommend = itemView.findViewById(R.id.tv_recommend);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout mRLFooter;
        TextView mTVFooter;

        FooterViewHolder(View itemView) {
            super(itemView);
            mRLFooter = itemView.findViewById(R.id.rl_footer);
            mTVFooter = itemView.findViewById(R.id.tv_footer);
        }
    }

}

좋은 웹페이지 즐겨찾기