亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android GridView實現橫向列表水平滾動

發布時間:2020-09-21 02:35:52 來源:腳本之家 閱讀:309 作者:sinat_21376777 欄目:移動開發

本文實例為大家分享了Android GridView實現橫向列表水平滾動的具體代碼,供大家參考,具體內容如下

有時候根據項目需要,使用可橫向滑動的GridView。僅以該文記錄一下,畢竟沒什么技術含量。

1.主界面布局代碼:activity_main.xml。設置android:numColumns=”auto_fit”是因為可以不定項的添加子項。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">

 <HorizontalScrollView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="fill_parent">

   <view.gridviewdemo.AblGridView
    android:id="@+id/id_gridview_home"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:scrollbars="none"
    android:stretchMode="spacingWidthUniform"/>
  </LinearLayout>
 </HorizontalScrollView>

</LinearLayout>

2.主界面GridView列表子項布局文件:home_gridview_item.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:layout_gravity="center"
 android:layout_margin="0dp"
 android:background="#ffffff"
 android:orientation="vertical">

 <ImageView
  android:id="@+id/id_iv_item"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="10dp"
  android:gravity="center" />

 <TextView
  android:id="@+id/id_tv_item"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:layout_marginTop="5dp"
  android:gravity="center"
  android:paddingBottom="10dp"
  android:text="標題文字"
  android:textSize="12sp" />
</LinearLayout>

3.java實現代碼:MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.GridView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
 AblGridView id_gridview_home;
 private int[] imgs = {R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc,
   R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc, R.mipmap.index1_nav_xc};
 private String[] titles = new String[]{"子項1", "子項2","子項3","子項4", "子項5"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  id_gridview_home= (AblGridView) findViewById(R.id.id_gridview_home);
  setGridView();
 }

 /**設置GirdView參數,綁定數據*/
 private void setGridView() {
  int size = imgs.length;
  int length = 100;
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  float density = dm.density;
  int gridviewWidth = (int) (size * (length + 4) * density);
  int itemWidth = (int) (length * density);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
  id_gridview_home.setLayoutParams(params); // 設置GirdView布局參數,橫向布局的關鍵
  id_gridview_home.setColumnWidth(itemWidth); // 設置列表項寬
  id_gridview_home.setHorizontalSpacing(5); // 設置列表項水平間距
  id_gridview_home.setStretchMode(GridView.NO_STRETCH);
  id_gridview_home.setNumColumns(size); // 設置列數量=列表集合數
  AblGridViewBaseAdapter adapter = new AblGridViewBaseAdapter(this,imgs,titles);
  id_gridview_home.setAdapter(adapter);
 }
}

4.自定義控件:AblGridView

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * 繼承GridView控件,為了更友好的顯示
 * 解決在scrollview中只顯示第一行數據的問題
 */
public class AblGridView extends GridView {
 public AblGridView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public AblGridView(Context context) {
  super(context);
 }

 public AblGridView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 }

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
    MeasureSpec.AT_MOST);
  super.onMeasure(widthMeasureSpec, expandSpec);
 }

}

5.重寫適配器:AblGridViewBaseAdapter

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 重寫適配器
 */
public class AblGridViewBaseAdapter extends BaseAdapter {

 private Context mContext;
 private String[] Title;//顯示標題數組
 private int[] Imgs;//顯示圖標數組

 public AblGridViewBaseAdapter(Context mContext,int[] imgs ,String[] titles) {
  super();
  this.mContext = mContext;
  Title = titles;
  Imgs = imgs;
 }

 @Override
 public int getCount() {
  return Title.length;
 }

 @Override
 public Object getItem(int position) {
  return position;
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  convertView = loadHomeHyOrYs(position, convertView, parent);
  return convertView;
 }

 /**
  * 加載會員或醫生的功能列表
  */
 public View loadHomeHyOrYs(int position, View convertView, ViewGroup parent) {
  if (convertView == null) {
   convertView = LayoutInflater.from(mContext).inflate(
     R.layout.home_gridview_item, parent, false);
  }
  TextView tv = get(convertView, R.id.id_tv_item);
  ImageView iv = get(convertView, R.id.id_iv_item);
  iv.setBackgroundResource(Imgs[position]);
  tv.setText(Title[position]);
  return convertView;
 }

 public <T extends View> T get(View view, int id) {
  SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
  if (viewHolder == null) {
   viewHolder = new SparseArray<View>();
   view.setTag(viewHolder);
  }
  View childView = viewHolder.get(id);
  if (childView == null) {
   childView = view.findViewById(id);
   viewHolder.put(id, childView);
  }
  return (T) childView;
 }

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武义县| 汕尾市| 洪泽县| 屏山县| 涞水县| 济宁市| 棋牌| 禄劝| 丹寨县| 江油市| 文水县| 龙江县| 阿拉善右旗| 德安县| 泊头市| 台南市| 黎川县| 区。| 新田县| 广平县| 特克斯县| 商丘市| 隆尧县| 枝江市| 张家川| 呼伦贝尔市| 澄城县| 福州市| 泗水县| 庆安县| 农安县| 临沂市| 南昌市| 安平县| 于都县| 通许县| 宿州市| 扎赉特旗| 启东市| 乌兰县| 韶关市|