天天看點

118.Android 簡單的分組清單(BaseRecyclerViewAdapterHelper) //1.第一步 導入依賴庫://2.第二步 建立SectionActivity頁面://3.第三步 建立SectionBean類繼承SectionEntity,寫出兩個重要的構造方法://4.第四步 建立SectionAdapter擴充卡://5.第五步 各個xml布局檔案:

118.Android 簡單的分組清單(BaseRecyclerViewAdapterHelper) //1.第一步 導入依賴庫://2.第二步 建立SectionActivity頁面://3.第三步 建立SectionBean類繼承SectionEntity,寫出兩個重要的構造方法://4.第四步 建立SectionAdapter擴充卡://5.第五步 各個xml布局檔案:
118.Android 簡單的分組清單(BaseRecyclerViewAdapterHelper) //1.第一步 導入依賴庫://2.第二步 建立SectionActivity頁面://3.第三步 建立SectionBean類繼承SectionEntity,寫出兩個重要的構造方法://4.第四步 建立SectionAdapter擴充卡://5.第五步 各個xml布局檔案:

 //1.第一步 導入依賴庫:

//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
//RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'      

//2.第二步 建立SectionActivity頁面:

//manifest注冊:

<activity
    android:name=".phone.activity.SectionActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="LockedOrientationActivity" />      

//activity代碼:

/**
 * @author CJF
 */
public class SectionActivity extends AppCompatActivity {
    private final GridLayoutManager manager = new GridLayoutManager(this, 4);
    private final SectionAdapter adapter = new SectionAdapter(R.layout.section_item, R.layout.section_item_header, new ArrayList<>());


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_list);

        RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);

        mTreeListRecy.setLayoutManager(manager);
        mTreeListRecy.setAdapter(adapter);

        List<SectionBean> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            //添加标題内容
            list.add(new SectionBean(true, "标題" + i));

            //添加資料内容
            for (int j = 0; j < 10; j++) {
                list.add(new SectionBean(new SectionBean.SectionDataBean("資料" + j)));
            }
        }

        adapter.addData(list);
    }
    
}      

//3.第三步 建立SectionBean類繼承SectionEntity,寫出兩個重要的構造方法:

/**
 * @author CJF
 */
public class SectionBean extends SectionEntity<SectionBean.SectionDataBean> {

    /**
     * 綁定标題頭布局的構造方法
     *
     * @param isHeader
     * @param header
     */
    public SectionBean(boolean isHeader, String header) {
        super(isHeader, header);
    }

    /**
     * 綁定資料内容的構造方法
     *
     * @param sectionDataBean
     */
    public SectionBean(SectionDataBean sectionDataBean) {
        super(sectionDataBean);
    }


    /**
     * 資料内容
     */
    public static class SectionDataBean {
        private String str;

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }

        public SectionDataBean(String str) {
            this.str = str;
        }
    }
    
}
      

//4.第四步 建立SectionAdapter擴充卡:

/**
 * @author CJF
 */
public class SectionAdapter extends BaseSectionQuickAdapter<SectionBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public SectionAdapter(int layoutResId, int sectionHeadResId, List<SectionBean> data) {
        super(layoutResId, sectionHeadResId, data);
    }

    @Override
    protected void convertHead(BaseViewHolder helper, SectionBean item) {
        helper.setText(R.id.mSectionItemHeaderText, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, SectionBean item) {
        SectionBean.SectionDataBean bean = item.t;
        helper.setText(R.id.mSectionItemText, bean.getStr());
    }

}      

//5.第五步 各個xml布局檔案:

//activity_tree_list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/color_white"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mTreeListRecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>      

//section_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:background="@drawable/selector_common_item"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/mSectionItemText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:background="@drawable/selector_common_item"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>      

//section_item_header:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:background="@drawable/selector_common_item"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/mSectionItemHeaderText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:background="@drawable/selector_common_item"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>      

//-------------------------------------------------------------END-----------------------------------------------------------