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

特點(diǎn)描述:
1.可上下滾動(dòng)
2.有類似于網(wǎng)格布局的樣式
3.子條目具有點(diǎn)擊事件
剛看到這個(gè)效果時(shí),首先想到的是使用分類型的RecyclerView 或者 ListView ,里面再嵌套GridView來實(shí)現(xiàn)。
但轉(zhuǎn)而又一想ScrollerView也可以滾動(dòng),只要往里面循環(huán)添加子item不就可以了嗎。
實(shí)現(xiàn)的邏輯大致如下:


具體的實(shí)現(xiàn)如下:
第一步:布局里寫一個(gè)ScrollerView,里面添加一個(gè)豎直的線性布局
第二步:實(shí)例化垂直線性布局
allhonor_hscroll = (LinearLayout) findViewById(R.id.allhonor_hscroll);
第三步:聯(lián)網(wǎng)請(qǐng)求獲取數(shù)據(jù)
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);
//聯(lián)網(wǎng)成功后使用fastjson來解析數(shù)據(jù)
processData(response);
}
@Override
public void onBefore(Request request, int id) {
}
@Override
public void onAfter(int id) {
}
});
}
/**
* 使用fastjson進(jìn)行解析
*
* @param json
*/
private void processData(String json) {
//使用GsonFormat生成對(duì)應(yīng)的bean類
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(json);
String data = jsonObject.getString("data");
List<AllHonorBean.HornorBean> hornorsList = JSON.parseArray(data, AllHonorBean.HornorBean.class);
//測(cè)試是否解析數(shù)據(jù)成功
// String strTest = hornorsList.get(0).getRegion();
// Log.e("TAG", strTest);
//第四步:裝配數(shù)據(jù),使用兩層for循環(huán)
}
第四步:設(shè)置兩種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文本一定要為空,不要設(shè)置任何內(nèi)容-->
<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>
第五步:裝配數(shù)據(jù)
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中直接裝配數(shù)據(jù)
tv_allhornors_big.setText(region);
//將布局添加進(jìn)去
allhonor_hscroll.addView(globalView);
List<AllHonorBean.HornorBean.FestivalsBean> festivalsList = hornorsListBean.getFestivals();
//-->內(nèi)層,每次裝兩個(gè)數(shù)據(jù)
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);
//順帶在這里就直接添加點(diǎn)擊事件的監(jiān)聽
if (j < festivalsList.size() - 1) {
setListener(tv_sn0, tv_sn1);
}
//裝配左邊的數(shù)據(jù)
honorName0 = festivalsList.get(j).getFestivalName();
tv_sn0.setText(honorName0);
//判讀越界否
if (j < festivalsList.size() - 1) {
//裝配右邊的數(shù)據(jù)
honorName1 = festivalsList.get(j + 1).getFestivalName();
tv_sn1.setText(honorName1);
}
//添加進(jìn)去
allhonor_hscroll.addView(smallView);
}
}
}
點(diǎn)擊事件的監(jiān)聽:
private void setListener(final TextView tv_sn0, final TextView tv_sn1) {
//給左邊的TextView 設(shè)置監(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 設(shè)置監(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();
}
});
}
完成~
再看一眼最后效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Scroller實(shí)現(xiàn)彈性滑動(dòng)效果
- Android用Scroller實(shí)現(xiàn)一個(gè)可向上滑動(dòng)的底部導(dǎo)航欄
- 詳解Android應(yīng)用開發(fā)中Scroller類的屏幕滑動(dòng)功能運(yùn)用
- 詳解Android Scroller與computeScroll的調(diào)用機(jī)制關(guān)系
- Android游戲開發(fā)實(shí)踐之人物移動(dòng)地圖的平滑滾動(dòng)處理
- android開發(fā)教程之使用線程實(shí)現(xiàn)視圖平滑滾動(dòng)示例
- Android 使用 Scroller 實(shí)現(xiàn)平滑滾動(dòng)功能的示例代碼
相關(guān)文章
解決android Listview的item中最外層Margin失效的問題
下面小編就為大家?guī)硪黄鉀Qandroid Listview的item中最外層Margin失效的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Flutter實(shí)現(xiàn)密碼強(qiáng)度校驗(yàn)結(jié)果的示例詳解
我們經(jīng)常在一些網(wǎng)站上看到這樣的密碼強(qiáng)度指示,使用三段線,分別用不同的顏色來表示弱密碼、中等強(qiáng)度密碼和強(qiáng)密碼,本篇我們就用?Flutter?來實(shí)現(xiàn)這樣一個(gè)密碼強(qiáng)度校驗(yàn)示例,希望對(duì)大家有所幫助2023-08-08
詳解Android中Activity的啟動(dòng)模式及應(yīng)用場(chǎng)景
今天給大家介紹下安卓開發(fā)中不得不涉及的Activity啟動(dòng)模式及應(yīng)用場(chǎng)景,Activity一共有四種啟動(dòng)模式,分別是Standard模式、SingleTop模式、SingleTask模式以及SingleInstance模式,,需要的朋友可以參考下2023-09-09
android自定義控件和自定義回調(diào)函數(shù)步驟示例
這篇文章主要介紹了android自定義控件步驟示例,包括為View類增加屬性、響應(yīng)用戶消息、自定義回調(diào)函數(shù)等方法2014-01-01
Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果逐步完成過程
在Android開發(fā)中,通過自定義View實(shí)現(xiàn)自己想要的效果是作為android開發(fā)程序員的一項(xiàng)必備技能,自定義View對(duì)于android開發(fā)來說也是比較難的一項(xiàng)技術(shù)2023-04-04
Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
本文給大家分享一段實(shí)例代碼給大家介紹android實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼,閃屏頁用到了handler和CountDownTimer類,還需配置一下Activity的主題,感興趣的朋友參考下吧2016-02-02
Android編程中調(diào)用Camera時(shí)預(yù)覽畫面有旋轉(zhuǎn)問題的解決方法
這篇文章主要介紹了Android編程中調(diào)用Camera時(shí)預(yù)覽畫面有旋轉(zhuǎn)問題的解決方法,涉及Android針對(duì)Camera調(diào)用攝像頭源碼部分的相關(guān)修改技巧,需要的朋友可以參考下2015-11-11
Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法
這篇文章主要介紹了Android編程開發(fā)之TextView單擊鏈接彈出Activity的方法,涉及Android中TextView控件的相關(guān)操作技巧,需要的朋友可以參考下2016-01-01
android 簡(jiǎn)單圖片動(dòng)畫播放的實(shí)例代碼
android 簡(jiǎn)單圖片動(dòng)畫播放的實(shí)例代碼,需要的朋友可以參考一下2013-06-06

