RecycleView得使用之--- BaseQuickAdapter ---多种Item (实现不同布局的操作)

RecycleView得使用之--- BaseQuickAdapter ---多种Item (实现不同布局的操作)_第1张图片

实现的功能:

  a.  RecyclerView 多种Item的实现   b。分割线   c。条目点击事件  d。Item 动画添加      e。数据为空时加载空布局。  

 f。解析String  json串。

实现流程:

1.  //更新超级适配器版本

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'

2.布局文件。

  a。主布局文件。   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:id="@+id/homeGloryMusicRecycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">android.support.v7.widget.RecyclerView>
LinearLayout>
b。 空布局文件:
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ll_root_empty"
    android:orientation="vertical"
    >
   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="内容为空。。。。。。"
       style="@style/text_"
       />
LinearLayout>

3. 这类逻辑代码:

public class RecyclerMultiActivity extends BaseActivity {

    private HomeGloryMusicAdapter homeGloryMusicAdapter;
    @BindView(R.id.homeGloryMusicRecycler)
    RecyclerView homeGloryMusicRecycler;

    @Override
    protected View bindView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_multi, null);
        return view;
    }

    @Override
    protected void initData() {


//        DataManager.getInstance(this).RequestHttp(NetApi.postHomeGloryMusic(DataManager.getMd5Str("MUSICLIST")), new ResultListener() {
//            @Override
//            public void responseSuccess(HomeGloryMusicModel obj) {

        String json = "{\"result\":\"01\"" +
                ",\"pd\":[{\"MUSICIMAGE\":\"http://img8.zol.com.cn/bbs/upload/19941/19940204.JPG\"" +
                ",\"MUSIC_ID\":\"385504537256919040\",\"MUSICTYPE\":\"歌手\"}" +
                ",{\"MUSICIMAGE\":\"https://img5.duitang.com/uploads/item/201411/21/20141121230649_aTZr2.jpeg\",\"MUSIC_ID\":\"385504602604175360\",\"MUSICTYPE\":\"排行榜\"}" +
                ",{\"MUSICIMAGE\":\"http://image.rongyaohk.com/musicimgs/musichomeimgs/f1eb2cadbd4247659897d9fe3552de23.png\",\"MUSIC_ID\":\"385504653091012608\",\"MUSICTYPE\":\"曲库\"}" +
                ",{\"MUSICIMAGE\":\"http://image.rongyaohk.com/musicimgs/musichomeimgs/7bb5a1c7a4e54b35a8d27d7914dea4fb.png\",\"MUSIC_ID\":\"385504688956506112\",\"MUSICTYPE\":\"电台\"}]}";

        Gson gson = new Gson();
        HomeGloryMusicModel obj = gson.fromJson(json, HomeGloryMusicModel.class);
        List pd = obj.getPd();
        String result1 = obj.getResult();
        LogUtils.i("音乐解析文件","音乐解析文件"+result1);

                for (int i = 0; i < obj.getPd().size(); i++) {
                    if (i % 2 == 0) {
                        obj.getPd().get(i).setItemType(1);
                    }
                    else {
                        obj.getPd().get(i).setItemType(3);
                    }
                }

                if (homeGloryMusicAdapter == null) {
                    homeGloryMusicAdapter = new HomeGloryMusicAdapter(obj.getPd());
                    homeGloryMusicRecycler.setLayoutManager(new LinearLayoutManager(RecyclerMultiActivity.this, LinearLayoutManager.HORIZONTAL, false));
                    homeGloryMusicRecycler.setNestedScrollingEnabled(false); // 控件嵌套处理 响应控件的。
                    homeGloryMusicRecycler.setAdapter(homeGloryMusicAdapter);
                    homeGloryMusicAdapter.setOnItemClickListener(new HomeMusicServerOnItemClick()); // 点击事件用Adapter设置。

                    homeGloryMusicAdapter.setEmptyView(R.layout.empty_img,homeGloryMusicRecycler); // 数据为空时加载空布局。
                    homeGloryMusicAdapter.openLoadAnimation(); // 默认渐变动画。
                    homeGloryMusicRecycler.addItemDecoration(new AbSpacesItemDecoration(5));// 分割线。

                }

            }
//            @Override
//            public void onCompleted() {
//
//            }
//        });
//    }


    class HomeMusicServerOnItemClick implements BaseQuickAdapter.OnItemClickListener {

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            UIUtils.showToast("多种条目" + position + "被点击了");

        }
    }
}
4. Adapter 类操作。     

 public class HomeGloryMusicAdapter extends BaseMultiItemQuickAdapter {

    public HomeGloryMusicAdapter(List data) {
        super(data);
        addItemType(HomeGloryServerModel.PdBean.LEFT, R.layout.home_glory_service_recycler_first_item);  //1
        addItemType(HomeGloryServerModel.PdBean.RIGHT, R.layout.home_glory_service_recycler_last_item);//3
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeGloryMusicModel.PdBean item) {
        Glide.with(mContext).load(item.getMUSICIMAGE()).into((ImageView) helper.getView(R.id.home_glory_service_recycler_item_img));
        helper.setText(R.id.home_glory_service_recycler_item_tv, item.getMUSICTYPE());
    }
}

5.  model类  代码

public class HomeGloryMusicModel {

    private String result;
    private List pd;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public List getPd() {
        return pd;
    }

    public void setPd(List pd) {
        this.pd = pd;
    }

    public static class PdBean implements MultiItemEntity {
        /**
         * MUSICIMAGE : http://47.104.16.215:8088/musicimgs/musichomeimgs/17c5ef457f2a45dab8f2cac7a73b214a.png
         * MUSIC_ID : 385504537256919040
         * MUSICTYPE : 歌手
         */
        public static final int LEFT = 1;
//        public static final int CENTER = 2;
        public static final int RIGHT = 3;
        private int itemType;

        private String MUSICIMAGE;
        private String MUSIC_ID;
        private String MUSICTYPE;

        public String getMUSICIMAGE() {
            return MUSICIMAGE;
        }

        public void setMUSICIMAGE(String MUSICIMAGE) {
            this.MUSICIMAGE = MUSICIMAGE;
        }

        public String getMUSIC_ID() {
            return MUSIC_ID;
        }

        public void setMUSIC_ID(String MUSIC_ID) {
            this.MUSIC_ID = MUSIC_ID;
        }

        public String getMUSICTYPE() {
            return MUSICTYPE;
        }

        public void setMUSICTYPE(String MUSICTYPE) {
            this.MUSICTYPE = MUSICTYPE;
        }

        public void setItemType(int itemType) {
            this.itemType = itemType;
        }

        @Override
        public int getItemType() {
            return itemType;
        }
    }
}

6. 工具 类:

public class AbSpacesItemDecoration extends RecyclerView.ItemDecoration {

    private int space;

    public AbSpacesItemDecoration(int space) {
        this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = space;
        outRect.right = space;
        outRect.bottom = space;
        //注释这两行是为了上下间距相同
//        if(parent.getChildAdapterPosition(view)==0){
        outRect.top = space;
//        }
    }
}

你可能感兴趣的:(RecycleView)