亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android使用GridView實(shí)現(xiàn)橫向滾動效果

 更新時間:2018年07月17日 14:53:50   作者:LeArn淡然  
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)橫向滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android使用GridView實(shí)現(xiàn)橫向滾動效果的具體代碼,供大家參考,具體內(nèi)容如下

第一次做橫向滑動,看了一些列子,基本就2總:HorizontalListView和GridView??紤]的了下選擇用比較熟的GridView,并且在2種方案都使用過,根據(jù)本人實(shí)際情況,采用了更適合的GridView。

也希望看過這篇博客的大神們,能指點(diǎn)下HorizontalListView和GridView兩個方案的優(yōu)缺點(diǎn)。

思路:

XML界面:用HorizontalScrollView + GridView 配合使用。
Java代碼部分:和普通GridView使用基本一致,但需要手動設(shè)置GridView的width以及Item的Width等。

筆者實(shí)際情況是:左右滑動,1行以4個為基準(zhǔn)。
在不同尺寸的平板下,呈現(xiàn)都是一個界面4個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;
    //一個界面要顯示的幾個Item
    int AnInterfaceNum=4;
    //每個Item的間距(注:如果間距過大,但屏幕寬度不夠,多出的部份會被無視)
    int spcing = 30;
    //計(jì)算當(dāng)個Item的寬度( 屏幕寬度 減去- 一個屏幕下要總item數(shù)量的間距之和 最后除/ 單個屏幕要顯示幾個Item)
    int itemWidth = ((getResources().getDisplayMetrics().widthPixels) - ((AnInterfaceNum - 1) * spcing)) / AnInterfaceNum;
    //這里筆者并不理解為什么網(wǎng)上有些代碼這里需要用到屏幕密度,但會影響我最終效果,就注釋掉
    //    float density = dm.density;
    //
    //    int gridviewWidth = (int) (size * (length) * density)+((size-1)*30);
    //    int itemWidth = (int) ((length) * density);
    //筆者更具實(shí)際情況改寫如下:
    //GridView總長度
    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)行更改。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論