国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android使用 ScrollerView 實現 可上下滾動的分類欄實例

2019-12-12 04:00:07
字體:
來源:轉載
供稿:網友

如果不考慮更深層的性能問題,我個人認為ScrollerView還是很好用的。而且單用ScrollerView就可以實現分類型的RecyclerView或ListView所能實現的效果。

下面我單單從效果展示方面考慮,使用ScrollerView實現如下圖所示的可滾動的多條目分類,只是為了跟大家一起分享一下新思路。(平時:若從復用性等方面考慮,這顯然是存在瑕疵的~)

特點描述:

1.可上下滾動

2.有類似于網格布局的樣式

3.子條目具有點擊事件

剛看到這個效果時,首先想到的是使用分類型的RecyclerView 或者 ListView  ,里面再嵌套GridView來實現。

但轉而又一想ScrollerView也可以滾動,只要往里面循環添加子item不就可以了嗎。

實現的邏輯大致如下:


具體的實現如下:

第一步:布局里寫一個ScrollerView,里面添加一個豎直的線性布局

第二步:實例化垂直線性布局

allhonor_hscroll = (LinearLayout) findViewById(R.id.allhonor_hscroll); 

第三步:聯網請求獲取數據

setAllHonorData(); 
/**   * 使用okhttp   */   public void setAllHonorData() {     OkHttpUtils         .get()         .url(url)         .build()         .execute(new StringCallback() {           @Override           public void onError(okhttp3.Call call, Exception e, int id) {             Log.e("TAG", "111");             Log.e("TAG", "onError" + e.getMessage());           }            @Override           public void onResponse(String response, int id) {             Log.e("TAG", "222");             Log.e("TAG", "onRespons" + response);              //聯網成功后使用fastjson來解析數據             processData(response);           }            @Override           public void onBefore(Request request, int id) {           }            @Override           public void onAfter(int id) {           }          });   } 
/**    * 使用fastjson進行解析    *    * @param json    */   private void processData(String json) {     //使用GsonFormat生成對應的bean類     com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(json);     String data = jsonObject.getString("data");     List<AllHonorBean.HornorBean> hornorsList = JSON.parseArray(data, AllHonorBean.HornorBean.class);      //測試是否解析數據成功 //    String strTest = hornorsList.get(0).getRegion(); //    Log.e("TAG", strTest);      //第四步:裝配數據,使用兩層for循環    } 

第四步:設置兩種item的布局

第一種item布局:item_allhornors0.xml

<?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"   android:background="#ffffff"   android:orientation="vertical">    <TextView     android:id="@+id/tv_allhornors_big"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="#11000000"     android:gravity="center_vertical"     android:paddingBottom="12dp"     android:paddingLeft="13dp"     android:paddingTop="12dp"     android:text="熱門"     android:textColor="#000000"     android:textSize="14sp" />    <View     android:layout_width="match_parent"     android:layout_height="1dp"     android:background="#11000000" />  </LinearLayout> 

第二種item布局:item_allhornors1.xml

<?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:background="#ffffff"   android:orientation="vertical">    <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">      <TextView       android:id="@+id/tv_allhornors_sn0"       android:layout_width="0dp"       android:layout_height="wrap_content"       android:layout_weight="1"       android:clickable="true"       android:gravity="center"       android:paddingBottom="12sp"       android:paddingTop="12sp"       android:text="你好你好"       android:textColor="#000000"       android:textSize="13sp" />      <View       android:layout_width="1dp"       android:layout_height="match_parent"       android:background="#11000000" />      <!--注意這里的text文本一定要為空,不要設置任何內容-->     <TextView       android:id="@+id/tv_allhornors_sn1"       android:layout_width="0dp"       android:layout_height="wrap_content"       android:layout_weight="1"       android:clickable="true"       android:gravity="center"       android:paddingBottom="12sp"       android:paddingTop="12sp"       android:text=""       android:textColor="#000000"       android:textSize="13sp" />   </LinearLayout>    <View     android:layout_width="match_parent"     android:layout_height="1dp"     android:background="#11000000" />  </LinearLayout> 

第五步:裝配數據

if (hornorsList != null && hornorsList.size() > 0) {       //-->外層       for (int i = 0; i < hornorsList.size(); i++) {         //創建并添加第一種item布局         View globalView = View.inflate(this, R.layout.item_allhornors0, null);         TextView tv_allhornors_big = (TextView) globalView.findViewById(R.id.tv_allhornors_big);         AllHonorBean.HornorBean hornorsListBean = hornorsList.get(i);         String region = hornorsListBean.getRegion();         //外層for中直接裝配數據         tv_allhornors_big.setText(region);         //將布局添加進去         allhonor_hscroll.addView(globalView);          List<AllHonorBean.HornorBean.FestivalsBean> festivalsList = hornorsListBean.getFestivals();         //-->內層,每次裝兩個數據         for (int j = 0; j < festivalsList.size(); j = j + 2) {           //創建并添加第二種item布局           View smallView = View.inflate(this, R.layout.item_allhornors1, null);           final TextView tv_sn0 = (TextView) smallView.findViewById(R.id.tv_allhornors_sn0);           TextView tv_sn1 = (TextView) smallView.findViewById(R.id.tv_allhornors_sn1);            //順帶在這里就直接添加點擊事件的監聽           if (j < festivalsList.size() - 1) {             setListener(tv_sn0, tv_sn1);           }            //裝配左邊的數據           honorName0 = festivalsList.get(j).getFestivalName();           tv_sn0.setText(honorName0);            //判讀越界否           if (j < festivalsList.size() - 1) {             //裝配右邊的數據             honorName1 = festivalsList.get(j + 1).getFestivalName();             tv_sn1.setText(honorName1);           }            //添加進去           allhonor_hscroll.addView(smallView);         }       }     } 

點擊事件的監聽:

private void setListener(final TextView tv_sn0, final TextView tv_sn1) {     //給左邊的TextView 設置監聽     tv_sn0.setOnClickListener(new View.OnClickListener() {       @Override       public void onClick(View view) {         Toast.makeText(MainActivity.this, "" + tv_sn0.getText(), Toast.LENGTH_SHORT).show();       }     });      //給右邊的TextView 設置監聽     tv_sn1.setOnClickListener(new View.OnClickListener() {       @Override       public void onClick(View view) {         Toast.makeText(MainActivity.this, "" + tv_sn1.getText(), Toast.LENGTH_SHORT).show();       }     });   } 

完成~

再看一眼最后效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 札达县| 四平市| 高州市| 晋城| 高阳县| 札达县| 尚志市| 马边| 虞城县| 裕民县| 沾益县| 平泉县| 陆河县| 涟源市| 东兴市| 井研县| 和田市| 台山市| 会宁县| 巧家县| 定陶县| 永嘉县| 青铜峡市| 道真| 嫩江县| 定远县| 湖口县| 三穗县| 长岭县| 湖北省| 和林格尔县| 无为县| 三门峡市| 雷州市| 鹰潭市| 兴城市| 乳源| 万盛区| 莒南县| 达拉特旗| 临泉县|