最近项目中需要使用到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>
<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的实现方法。
- 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 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>