GridView设置横向滚动

 

    

最近项目中需要使用到GridView,由于文字太长,内容多时竖直滚动GridView显得比较难看,于是找了一些资料发现可以实现GridView横向滚动。首先让GridView横向滚动需要HorizontalScrollView这个控件,例如:

main.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

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

            <GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" >
            </GridView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

<span style="font-family:'Microsoft YaHei';font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

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

            <GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center" >
            </GridView>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout></span>
以上是主要布局,然后我们写GridView中每个item中的布局,比较简单,只是给大家介绍
 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00ff00" />

    <TextView
        android:id="@+id/item_textview"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:gravity="center" />

</LinearLayout>

 

<span style="font-family:'Microsoft YaHei';font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00ff00" />

    <TextView
        android:id="@+id/item_textview"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:gravity="center" />

</LinearLayout></span>

上面是item的布局,比较简单,就是一个ImageView和TextView,然后是主要实现GridView的Adapter。

以下是Activity的实现方法。

public class MainActivity extends Activity { 
  1.  
  2.     private GridView gridView; 
  3.     private LayoutInflater inflater; 
  4.     private List<String> dataList = new ArrayList<String>(); 
  5.  
  6.     @Override 
  7.     public void onCreate(Bundle savedInstanceState) { 
  8.         super.onCreate(savedInstanceState); 
  9.         setContentView(R.layout.activity_main); 
  10.         gridView = (GridView) this.findViewById(R.id.gridview); 
  11.         for (int i = 0; i < 10; i++) { 
  12.             dataList.add("测试" + i); 
  13.         } 
  14.         inflater = (LayoutInflater) this 
  15.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  16.         GridViewAdapter adapter = new GridViewAdapter(); 
  17.         gridView.setAdapter(adapter); 
  18.         int size = dataList.size(); 
  19.         DisplayMetrics dm = new DisplayMetrics(); 
  20.         getWindowManager().getDefaultDisplay().getMetrics(dm); 
  21.         float density = dm.density; 
  22.         int allWidth = (int) (110 * size * density); 
  23.         int itemWidth = (int) (100 * density); 
  24.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 
  25.                 allWidth, LinearLayout.LayoutParams.FILL_PARENT); 
  26.         gridView.setLayoutParams(params); 
  27.         gridView.setColumnWidth(itemWidth); 
  28.         gridView.setHorizontalSpacing(10); 
  29.         gridView.setStretchMode(GridView.NO_STRETCH); 
  30.         gridView.setNumColumns(size); 
  31.     } 
  32.  
  33.     @Override 
  34.     public boolean onCreateOptionsMenu(Menu menu) { 
  35.         getMenuInflater().inflate(R.menu.activity_main, menu); 
  36.         return true
  37.     } 
  38.  
  39.     final class GridViewAdapter extends BaseAdapter { 
  40.          
  41.         @Override 
  42.         public int getCount() { 
  43.             return dataList.size(); 
  44.         } 
  45.  
  46.         @Override 
  47.         public Object getItem(int position) { 
  48.             return dataList.get(position); 
  49.         } 
  50.  
  51.         @Override 
  52.         public long getItemId(int position) { 
  53.             return position; 
  54.         } 
  55.  
  56.         @Override 
  57.         public View getView(int position, View convertView, ViewGroup parent) { 
  58.             convertView = inflater.inflate(R.layout.gridview_item, null); 
  59.             TextView textView = (TextView) convertView 
  60.                     .findViewById(R.id.item_textview); 
  61.             String str = dataList.get(position); 
  62.             textView.setText(str); 
  63.             return convertView; 
  64.         } 
  65.  
  66.     } 
  67. }
<span style="font-family:'Microsoft YaHei';font-size:14px;">public class MainActivity extends Activity {

	private GridView gridView;
	private LayoutInflater inflater;
	private List<String> dataList = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gridView = (GridView) this.findViewById(R.id.gridview);
		for (int i = 0; i < 10; i++) {
			dataList.add("测试" + i);
		}
		inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		GridViewAdapter adapter = new GridViewAdapter();
		gridView.setAdapter(adapter);
		int size = dataList.size();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		float density = dm.density;
		int allWidth = (int) (110 * size * density);
		int itemWidth = (int) (100 * density);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				allWidth, LinearLayout.LayoutParams.FILL_PARENT);
		gridView.setLayoutParams(params);
		gridView.setColumnWidth(itemWidth);
		gridView.setHorizontalSpacing(10);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setNumColumns(size);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	final class GridViewAdapter extends BaseAdapter {
		
		@Override
		public int getCount() {
			return dataList.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = inflater.inflate(R.layout.gridview_item, null);
			TextView textView = (TextView) convertView
					.findViewById(R.id.item_textview);
			String str = dataList.get(position);
			textView.setText(str);
			return convertView;
		}

	}
}</span>

主要是设置GridView的LayoutParams,new 这个LayoutParams对象必须是这个View所在Layout的布局类型,然后设置GridView总长度和每个item的长度,然后设置总个数就可以实现横滚的效果!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值