Android使用GridView實(shí)現(xiàn)橫向滾動(dòng)效果
本文實(shí)例為大家分享了Android使用GridView實(shí)現(xiàn)橫向滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
第一次做橫向滑動(dòng),看了一些列子,基本就2總:HorizontalListView和GridView。考慮的了下選擇用比較熟的GridView,并且在2種方案都使用過(guò),根據(jù)本人實(shí)際情況,采用了更適合的GridView。
也希望看過(guò)這篇博客的大神們,能指點(diǎn)下HorizontalListView和GridView兩個(gè)方案的優(yōu)缺點(diǎn)。
思路:
XML界面:用HorizontalScrollView + GridView 配合使用。
Java代碼部分:和普通GridView使用基本一致,但需要手動(dòng)設(shè)置GridView的width以及Item的Width等。
筆者實(shí)際情況是:左右滑動(dòng),1行以4個(gè)為基準(zhǔn)。
在不同尺寸的平板下,呈現(xiàn)都是一個(gè)界面4個(gè)Item。
先上效果圖
模擬器Nexus 10 API 18 2560x1600: xhdpi 效果如下:

模擬器Nexus 9 API 18 2048x1536: xhdpi 效果如下:

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:gravity="center">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<GridView
android:id="@+id/dev_gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform">
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
android:numColumns=”auto_fit” –>列數(shù)量自適應(yīng)
android:stretchMode=”spacingWidthUniform” –>Item間距均勻
Java
這里是參考了網(wǎng)上他人的代碼后,更具自己的實(shí)際情況進(jìn)行更改,并附上了詳細(xì)的注釋。
/**
* 水平GridView設(shè)置
* @param size Item總數(shù)
* @param gridView 需要設(shè)置的GridView
*/
private void setHorizontalGridView(int size, GridView gridView) {
int length = size;
//一個(gè)界面要顯示的幾個(gè)Item
int AnInterfaceNum=4;
//每個(gè)Item的間距(注:如果間距過(guò)大,但屏幕寬度不夠,多出的部份會(huì)被無(wú)視)
int spcing = 30;
//計(jì)算當(dāng)個(gè)Item的寬度( 屏幕寬度 減去- 一個(gè)屏幕下要總item數(shù)量的間距之和 最后除/ 單個(gè)屏幕要顯示幾個(gè)Item)
int itemWidth = ((getResources().getDisplayMetrics().widthPixels) - ((AnInterfaceNum - 1) * spcing)) / AnInterfaceNum;
//這里筆者并不理解為什么網(wǎng)上有些代碼這里需要用到屏幕密度,但會(huì)影響我最終效果,就注釋掉
// float density = dm.density;
//
// int gridviewWidth = (int) (size * (length) * density)+((size-1)*30);
// int itemWidth = (int) ((length) * density);
//筆者更具實(shí)際情況改寫(xiě)如下:
//GridView總長(zhǎng)度
int gridviewWidth = (length * (itemWidth)) + ((length - 1) * spcing);
@SuppressWarnings("deprecation")
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
gridView.setLayoutParams(params); // 設(shè)置GirdView布局參數(shù),橫向布局的關(guān)鍵
gridView.setColumnWidth(itemWidth); // 設(shè)置列表項(xiàng)寬
gridView.setHorizontalSpacing(spcing); // 設(shè)置列表項(xiàng)水平間距
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setNumColumns(length); // 設(shè)置列數(shù)量=列表集合數(shù)
}
這塊代碼是核心部分,并不建議直接copy使用,建議先看懂后,再根據(jù)實(shí)際情況進(jìn)行更改。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問(wèn)題的解決
這篇文章主要介紹了Android中利用NetworkInfo判斷網(wǎng)絡(luò)狀態(tài)時(shí)出現(xiàn)空指針(NullPointerException)問(wèn)題的解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android自定義FloatingActionButton滑動(dòng)行為只隱藏不出現(xiàn)的問(wèn)題小結(jié)
這篇文章主要介紹了Android自定義FloatingActionButton滑動(dòng)行為只隱藏不出現(xiàn)的問(wèn)題小結(jié),需要的朋友可以參考下2017-01-01
PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載
這篇文章主要介紹了PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載的相關(guān)資料,需要的朋友可以參考下2015-10-10
解決react-native軟鍵盤(pán)彈出擋住輸入框的問(wèn)題
這篇文章主要介紹了解決react-native軟鍵盤(pán)彈出擋住輸入框的問(wèn)題,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
android加密參數(shù)定位實(shí)現(xiàn)方法
這篇文章主要介紹了android加密參數(shù)定位方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Android開(kāi)發(fā)圖片水平旋轉(zhuǎn)180度方法
今天小編就為大家分享一篇Android開(kāi)發(fā)圖片水平旋轉(zhuǎn)180度方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android逆向之dex2oat的實(shí)現(xiàn)解析
虛擬機(jī)的發(fā)生展經(jīng)歷了從初期的dalvik,到中期的dalvik,以及后期的ART。但是市面上的APK文件早已已經(jīng)全球流行。為了能夠讓這些APK不加改動(dòng)的在所有虛擬機(jī)上面運(yùn)行,google采用了類(lèi)似適配器模式。即在讓虛擬運(yùn)行之前多一道工序。就是dexopt2021-10-10

