GridView 横向滚动

GridView横向滚动.

分类:androidjava2012-08-0723:1681人阅读评论(0)收藏举报

GridView和ListView都是android比较重要的控件,但是横滚的控件不是太多。

这里介绍怎么把GridView横向滚动起来,看到其他网友也有相应的解决方法,自己只是把这些知识总结一下,供大家参考!

首先让GridView横向滚动需要HorizontalScrollView这个控件,例如:

[html]viewplaincopyprint?

01.<SPANstyle="FONT-FAMILY:'MicrosoftYaHei';FONT-SIZE:14px"><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

02.xmlns:tools="http://schemas.android.com/tools"

03.android:layout_width="fill_parent"

04.android:layout_height="fill_parent">

05.

06.<HorizontalScrollView

07.android:layout_width="fill_parent"

08.android:layout_height="wrap_content"

09.android:scrollbars="none">

10.

11.<LinearLayout

12.android:layout_width="fill_parent"

13.android:layout_height="wrap_content"

14.android:orientation="horizontal">

15.

16.<GridView

17.android:id="@+id/gridview"

18.android:layout_width="fill_parent"

19.android:layout_height="wrap_content"

20.android:layout_gravity="center">

21.</GridView>

22.</LinearLayout>

23.</HorizontalScrollView>

24.

25.</LinearLayout></SPAN>

<LinearLayoutxmlns: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>

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

[html]viewplaincopyprint?

01.<SPANstyle="FONT-FAMILY:'MicrosoftYaHei';FONT-SIZE:14px"><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

02.xmlns:tools="http://schemas.android.com/tools"

03.android:layout_width="fill_parent"

04.android:layout_height="wrap_content"

05.android:orientation="vertical">

06.

07.<ImageView

08.android:layout_width="100dp"

09.android:layout_height="100dp"

10.android:background="#00ff00"/>

11.

12.<TextView

13.android:id="@+id/item_textview"

14.android:layout_width="100dp"

15.android:layout_height="20dp"

16.android:gravity="center"/>

17.

18.</LinearLayout></SPAN>

<LinearLayoutxmlns: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>

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

以下是Activity的实现方法。

[java]viewplaincopyprint?

01.<SPANstyle="FONT-FAMILY:'MicrosoftYaHei';FONT-SIZE:14px">publicclassMainActivityextendsActivity{

02.

03.privateGridViewgridView;

04.privateLayoutInflaterinflater;

05.privateList<String>dataList=newArrayList<String>();

06.

07.@Override

08.publicvoidonCreate(BundlesavedInstanceState){

09.super.onCreate(savedInstanceState);

10.setContentView(R.layout.activity_main);

11.gridView=(GridView)this.findViewById(R.id.gridview);

12.for(inti=0;i<10;i++){

13.dataList.add("测试"+i);

14.}

15.inflater=(LayoutInflater)this

16..getSystemService(Context.LAYOUT_INFLATER_SERVICE);

17.GridViewAdapteradapter=newGridViewAdapter();

18.gridView.setAdapter(adapter);

19.intsize=dataList.size();

20.DisplayMetricsdm=newDisplayMetrics();

21.getWindowManager().getDefaultDisplay().getMetrics(dm);

22.floatdensity=dm.density;

23.intallWidth=(int)(110*size*density);

24.intitemWidth=(int)(100*density);

25.LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(

26.allWidth,LinearLayout.LayoutParams.FILL_PARENT);

27.gridView.setLayoutParams(params);

28.gridView.setColumnWidth(itemWidth);

29.gridView.setHorizontalSpacing(10);

30.gridView.setStretchMode(GridView.NO_STRETCH);

31.gridView.setNumColumns(size);

32.}

33.

34.@Override

35.publicbooleanonCreateOptionsMenu(Menumenu){

36.getMenuInflater().inflate(R.menu.activity_main,menu);

37.returntrue;

38.}

39.

40.finalclassGridViewAdapterextendsBaseAdapter{

41.

42.@Override

43.publicintgetCount(){

44.returndataList.size();

45.}

46.

47.@Override

48.publicObjectgetItem(intposition){

49.returndataList.get(position);

50.}

51.

52.@Override

53.publiclonggetItemId(intposition){

54.returnposition;

55.}

56.

57.@Override

58.publicViewgetView(intposition,ViewconvertView,ViewGroupparent){

59.convertView=inflater.inflate(R.layout.gridview_item,null);

60.TextViewtextView=(TextView)convertView

61..findViewById(R.id.item_textview);

62.Stringstr=dataList.get(position);

63.textView.setText(str);

64.returnconvertView;

65.}

66.

67.}

68.}</SPAN>

publicclassMainActivityextendsActivity{

privateGridViewgridView;

privateLayoutInflaterinflater;

privateList<String>dataList=newArrayList<String>();

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gridView=(GridView)this.findViewById(R.id.gridview);

for(inti=0;i<10;i++){

dataList.add("测试"+i);

}

inflater=(LayoutInflater)this

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

GridViewAdapteradapter=newGridViewAdapter();

gridView.setAdapter(adapter);

intsize=dataList.size();

DisplayMetricsdm=newDisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

floatdensity=dm.density;

intallWidth=(int)(110*size*density);

intitemWidth=(int)(100*density);

LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(

allWidth,LinearLayout.LayoutParams.FILL_PARENT);

gridView.setLayoutParams(params);

gridView.setColumnWidth(itemWidth);

gridView.setHorizontalSpacing(10);

gridView.setStretchMode(GridView.NO_STRETCH);

gridView.setNumColumns(size);

}

@Override

publicbooleanonCreateOptionsMenu(Menumenu){

getMenuInflater().inflate(R.menu.activity_main,menu);

returntrue;

}

finalclassGridViewAdapterextendsBaseAdapter{

@Override

publicintgetCount(){

returndataList.size();

}

@Override

publicObjectgetItem(intposition){

returndataList.get(position);

}

@Override

publiclonggetItemId(intposition){

returnposition;

}

@Override

publicViewgetView(intposition,ViewconvertView,ViewGroupparent){

convertView=inflater.inflate(R.layout.gridview_item,null);

TextViewtextView=(TextView)convertView

.findViewById(R.id.item_textview);

Stringstr=dataList.get(position);

textView.setText(str);

returnconvertView;

}

}

}

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

相关推荐