Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android GridView 實現單行橫向滑動的兩步做法

編輯:關於 Android 程式設計

項目裡常遇到這樣的需求:用 GridView 展示一排節點,但內容超出屏幕寬度,希望可以左右滑動查看。GridView 本身並不支持橫向滾動,解法是「外層套 HorizontalScrollView,內層把 GridView 的寬度撐開並把所有數據放到一行」。

核心思路

關鍵在於動態計算 GridView 的寬度:單個 item 寬度 × 數據條數。同時把 setNumColumns 設為數據條數,強制所有內容排成一行,這樣 GridView 的內容寬度超過屏幕,外層的 HorizontalScrollView 就能產生橫向滑動。

Activity 中的關鍵代碼

gridViewAdapter = new GridViewAdapter(mContext, list1);

ViewGroup.LayoutParams params = gridview.getLayoutParams();
params.width = DensityUtil.dip2px(mContext, 101) * list1.size();
gridview.setLayoutParams(params);
gridview.setNumColumns(list1.size());
gridview.setAdapter(gridViewAdapter);

其中 gridViewAdapter 是自定义アダプタ,gridview 是布局中的 GridView 控件,list1 為數據源。

dp 與 px 的轉換工具

public class DensityUtil {

    /** 根據手機分辨率,把 dp 轉為 px */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /** 根據手機分辨率,把 px 轉為 dp */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}

布局文件

外層 HorizontalScrollView 關閉滾動條,內部放一個包裹 GridView 的 LinearLayout:

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:layout_marginTop="@dimen/normalPadding">

    <LinearLayout
        android:id="@+id/liear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cacheColorHint="#00000000"
            android:columnWidth="100dp"
            android:gravity="center"
            android:horizontalSpacing="1.0dip"
            android:listSelector="#00000000"
            android:numColumns="auto_fit"
            android:stretchMode="spacingWidthUniform"
            android:verticalSpacing="1.0dip" />

    </LinearLayout>

</HorizontalScrollView>

item 樣式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dp" >

    <ImageView
        android:id="@+id/ImageIcon"
        android:layout_width="100dp"
        android:layout_height="90dp"
        android:src="@drawable/card_detaild_small"
        android:scaleType="fitXY" />

</RelativeLayout>

一個容易被忽略的細節

代碼裡計算寬度時用的單項寬度是 101dp,比 item 佈局中的 100dp 多出 1dp。這一步是刻意為之:GridView 在計算列間距時會產生取整誤差,若寬度恰好等於 item 寬度之和,最後一個 item 往往會被裁掉一兩像素,看起來像是沒顯示完整。多留 1dp 餘量即可規避。

另外提醒兩點:如果數據條數較少、總寬度小於屏幕,HorizontalScrollView 自然不會滑動,此時可能需要手動調整 item 寬度使其鋪滿;若數據量很大(幾十上百條),一次性撐開成單行會導致內存與流暢度問題,更合適的方案是改用 RecyclerView 配合 LinearLayoutManager.setOrientation(HORIZONTAL),它自帶 item 回收複用機制,是官方推薦的做法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved