您的位置:首页 > 其它

HorizontalScrollView 和GridView 实现横向滑动

2017-02-14 15:32 525 查看
用 HorizontalScrollView 和GridView
实现横向滑动时用几个地方需要注意的,这里记录一下,以便后续参考.

1.GridView 显示大小异常.

不管在布局中如何设置宽高,会发现都没有效果,这个时候要把HorizontalScrollView 的布局中增加android:fillViewport="true"

2.不可以使用HorizontalScrollView直接嵌套GridView,需要有Layout包裹

然后设置Gridview的layout_width 一个具体的宽度.一个可以超出屏幕最大宽度的值,可以在布局中设置也可以在java 代码中设置.

  如果在代码中手动设置GridView 的宽度是,需要注意的是设置setLayoutParams
的时候,参数的类型要和其parent 的layout 类型一致,比如GridView的parent 是RelativeLayout 那GridView 的setLayoutParams 就要是RelativeLayout.LayoutParams
类型的.

上面说了几个需要注意的地方,下面贴上相关的代码.

HorizontalscrollActivity .java

public class HorizontalscrollActivity extends Activity {

private List<HashMap<String, Object>> list ;

private final int DATA_SIZE = 8;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_horizontal_scroll);


DisplayMetrics dm = new DisplayMetrics();

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

float density = dm.density;

int ll_width = (int) ((100+5) * density * DATA_SIZE);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ll_width, RelativeLayout.LayoutParams.WRAP_CONTENT);

GridView gv = (GridView)this.findViewById(R.id.gv);

gv.setLayoutParams(params);

gv.setNumColumns(DATA_SIZE);

getData();

ListAdapter adapter = new SimpleAdapter(this, list, R.layout.gridview_item, 

new String[]{"itemImage","itemName"}, 

new int[]{R.id.itemImage,R.id.itemName});

gv.setAdapter(adapter);

}


private void getData() {

list = new ArrayList<HashMap<String,Object>>();

for (int i = 0; i < DATA_SIZE; i++) {

HashMap<String, Object> map = new HashMap<String, Object>();

map.put("itemImage", R.drawable.ic_launcher);

map.put("itemName", "图片-" + i);

list.add(map);

}


}



}


activity_horizontal_scroll.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:orientation="vertical" >


<HorizontalScrollView 

android:id="@+id/horizontalscrollview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scrollbars="none"

android:fillViewport="true">

<RelativeLayout android:id="@+id/ll_horizontalscrollview"

android:layout_width="match_parent"

android:layout_height="wrap_content">

   <GridView 

android:id="@+id/gv"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:numColumns="auto_fit"

android:horizontalSpacing="10dp"

/>

</RelativeLayout>

</HorizontalScrollView>


</LinearLayout>


gridview_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content" >


<ImageView

android:id="@+id/itemImage"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerHorizontal="true" >

</ImageView>


<TextView

android:id="@+id/itemName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/itemImage"

android:layout_centerHorizontal="true" >

</TextView>

</RelativeLayout>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: