1. 程式人生 > >android:RecyclerView佈局管理顯示不同跨度和空佈局

android:RecyclerView佈局管理顯示不同跨度和空佈局

佈局管理器顯示不同跨度:

實用RecyclerView必須設定一個佈局管理器,這裡設定GridLayoutManager,還需要給佈局管理器設定一個方法;

 //設定表格,根據position計算在該position處item的跨度(佔幾列資料)
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //計算在哪個position更換跨度,要佔滿一行跨度就是2
                if (position < type1Names.length)
                    return 2;
                else //不需要佔滿一行跨度就是1
                    return 1;
            }
        });
介面卡:
public class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final MainActivity context;
    private final List<TypeBean> typeBeanList;
    private final int breakPoint;

    public MyRecyclerAdapter(MainActivity mainActivity, List<TypeBean> typeBeanList, int breakPoint) {
        this.context = mainActivity;
        this.typeBeanList = typeBeanList;
        this.breakPoint = breakPoint;
    }

    @Override
    //判斷是載入那個佈局
    public int getItemViewType(int position) {
        if (position < breakPoint) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    //返回一個自定義的ViewHolder(當RecyclerView需要一個ViewHolder時會回撥該方法,如果有可複用的View不會回撥)
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(context).inflate(R.layout.item_recycler_type1, parent, false);
                holder = new Type1ViewHolder(view);
                break;
            case 1:
                view = LayoutInflater.from(context).inflate(R.layout.item_recycler_type2, parent, false);
                holder = new Type2ViewHolder(view);
                break;
        }
        return holder;
    }

    @Override
    //當一個View需要出現在螢幕上時,該方法會被回撥,我們需要再該方法中根據資料來更改檢視
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        TypeBean typeBean = typeBeanList.get(position);

        switch (getItemViewType(position)) {
            case 0:
                Type1ViewHolder holder1 = (Type1ViewHolder) holder;
                holder1.tvName.setText(typeBean.getName());
                holder1.tvId.setText(typeBean.getId() + "");
                break;
            case 1:
                Type2ViewHolder holder2 = (Type2ViewHolder) holder;
                holder2.tvName.setText(typeBean.getName());
                holder2.tvId.setText(typeBean.getId() + "");
                if (position % 2 == 1)
                    holder2.fgxView.setVisibility(View.GONE);
                else
                    holder2.fgxView.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    //一共多少item
    public int getItemCount() {
        return typeBeanList.size();
    }

    //佈局1
    public class Type1ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName, tvId;

        public Type1ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvId = itemView.findViewById(R.id.tv_id);
        }
    }

    //佈局2
    public class Type2ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName, tvId;
        public View fgxView;

        public Type2ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tvId = itemView.findViewById(R.id.tv_id);
            fgxView = itemView.findViewById(R.id.fgx_view);
        }
    }

}
當adapter沒有內容時,需要顯示一個“空佈局”頁面,現在很多專案中都會用到;以前是根據資料是否為null來顯示隱藏RecyclerView和空佈局,其實可以自己封裝一下,在RecyclerView中新增一個方法即可:
    /**
     * @param emptyView 空介面顯示的控制元件
     * @param view      顯示狀態與RecycleView一致的View
     */
    private void checkIfEmpty(View emptyView, View... view) {
        //獲取adapter下面item的個數
        final boolean emptyViewVisible = getAdapter().getItemCount() == 0;
        if (getAdapter() == null) return;
        //如果item=0空佈局顯示,否則隱藏
        if (emptyView != null)
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
        //如果item=0佈局陣列隱藏,否則顯示
        if (view != null)
            for (View v : view) {
                if (v != null)
                    v.setVisibility(emptyViewVisible ? GONE : VISIBLE);
            }
        //如果item=0隱藏EmptyRecyclerView,否則顯示
        setVisibility(emptyViewVisible ? GONE : VISIBLE);
    }
使用方法非常簡單,在RecyclerView設定adapter之後新增:
 View view=View.inflate(MainActivity.this,R.layout.include_empty,null);
        /**
         *  設定空佈局
         *  第一個引數:當資料清空時顯示的佈局
         *  第二個引數:後可新增多個佈局,當資料清空時一起隱藏的佈局
         */
        mRecyclerView.setEmptyView(view,tvHeader);



點選開啟連結下載原始碼