自定义两行可左右滑动的GridView

效果图:

xml:

 

[html][/html] view plaincopy

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     android:layout_width=”match_parent”
  4.     android:layout_height=”match_parent”
  5.     android:orientation=”vertical” >
  6.     <HorizontalScrollView
  7.         android:id=”@+id/scrollView”
  8.         android:layout_width=”match_parent”
  9.         android:layout_height=”match_parent” >
  10.         <LinearLayout
  11.             android:id=”@+id/linearLayout1″
  12.             android:layout_width=”wrap_content”
  13.             android:layout_height=”wrap_content” >
  14.             <GridView
  15.                 android:id=”@+id/gridView1″
  16.                 android:layout_width=”match_parent”
  17.                 android:layout_height=”match_parent” >
  18.             </GridView>
  19.         </LinearLayout>
  20.     </HorizontalScrollView>
  21. </LinearLayout>

Java:

 

 

[java][/java] view plaincopy

  1. package com.max.test.gridview_viewpager;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.DisplayMetrics;
  5. import android.view.Menu;
  6. import android.widget.GridView;
  7. import android.widget.HorizontalScrollView;
  8. import android.widget.LinearLayout.LayoutParams;
  9. public class MainActivity extends Activity {
  10.     HorizontalScrollView horizontalScrollView;
  11.     GridView gridView;
  12.     DisplayMetrics dm;
  13.     private int NUM = 4; // 每行显示个数
  14.     private int hSpacing = 20;// 水平间距
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.main);
  19.         horizontalScrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
  20.         gridView = (GridView) findViewById(R.id.gridView1);
  21.         horizontalScrollView.setHorizontalScrollBarEnabled(false);// 隐藏滚动条
  22.         getScreenDen();
  23.         setValue();
  24.     }
  25.     private void setValue() {
  26.         MyGridViewAdapter adapter = new MyGridViewAdapter(this, 21);
  27.         gridView.setAdapter(adapter);
  28.         LayoutParams params = new LayoutParams(adapter.getCount() * 65,
  29.                 LayoutParams.WRAP_CONTENT);
  30.         gridView.setLayoutParams(params);
  31.         gridView.setColumnWidth(dm.widthPixels / NUM);
  32.         // gridView.setHorizontalSpacing(hSpacing);
  33.         gridView.setStretchMode(GridView.NO_STRETCH);
  34.         int count = adapter.getCount();
  35.         gridView.setNumColumns((count % 2 == 0) ? count / 2 : count / 2 + 1);
  36.     }
  37.     private void getScreenDen() {
  38.         dm = new DisplayMetrics();
  39.         getWindowManager().getDefaultDisplay().getMetrics(dm);
  40.     }
  41.     @Override
  42.     public boolean onCreateOptionsMenu(Menu menu) {
  43.         // Inflate the menu; this adds items to the action bar if it is present.
  44.         getMenuInflater().inflate(R.menu.main, menu);
  45.         return true;
  46.     }
  47. }

 

源码地址:点击这里

标签