android使用 ScrollerView 實現 可上下滾動的分類欄實例
如果不考慮更深層的性能問題,我個人認為ScrollerView還是很好用的。而且單用ScrollerView就可以實現分類型的RecyclerView或ListView所能實現的效果。
下面我單單從效果展示方面考慮,使用ScrollerView實現如下圖所示的可滾動的多條目分類,只是為了跟大家一起分享一下新思路。(平時:若從復用性等方面考慮,這顯然是存在瑕疵的~)

特點描述:
1.可上下滾動
2.有類似于網格布局的樣式
3.子條目具有點擊事件
剛看到這個效果時,首先想到的是使用分類型的RecyclerView 或者 ListView ,里面再嵌套GridView來實現。
但轉而又一想ScrollerView也可以滾動,只要往里面循環(huán)添加子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循環(huán)
}
第四步:設置兩種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++) {
//創(chuàng)建并添加第一種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) {
//創(chuàng)建并添加第二種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);
//順帶在這里就直接添加點擊事件的監(jiān)聽
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);
}
}
}
點擊事件的監(jiān)聽:
private void setListener(final TextView tv_sn0, final TextView tv_sn1) {
//給左邊的TextView 設置監(jiān)聽
tv_sn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "" + tv_sn0.getText(), Toast.LENGTH_SHORT).show();
}
});
//給右邊的TextView 設置監(jiān)聽
tv_sn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "" + tv_sn1.getText(), Toast.LENGTH_SHORT).show();
}
});
}
完成~
再看一眼最后效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android使用VideoView出現無法播放此視頻問題的解決方法
Android提供了 VideoView組件,它的作用與ImageView類似,只是ImageView用于顯示圖片,而VideoView用于播放視頻,下面這篇文章主要給大家介紹了關于利用VideoView出現無法播放此視頻問題的解決方法,需要的朋友可以參考下2018-07-07
Jetpack?Compose實現點擊事件click的多種方法
這篇文章主要介紹了Jetpack?Compose實現點擊事件的多種方法,Jetpack?Compose是一款基于Kotlin的聲明式UI工具包,可以方便地創(chuàng)建漂亮的用戶界面,下面我們就來看看Jetpack?Compose添加點擊事件都可以怎么實現2024-02-02
Android Native庫的加載及動態(tài)鏈接的過程
這篇文章主要介紹了Android Native庫的加載及動態(tài)鏈接的加載過程,需要的朋友可以參考下2018-01-01
Android實現將一個Activity設置成窗口樣式的方法
這篇文章主要介紹了Android實現將一個Activity設置成窗口樣式的方法,涉及Android的窗口樣式設置與布局技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-02-02

