一、Activity部分代码
package com.max.test.gridview_viewpager;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
HorizontalScrollView horizontalScrollView;
GridView gridView;
DisplayMetrics dm;
private int NUM = 4; // 每行显示个数
private int LIEWIDTH;//每列宽度
private int LIE = 12;//列数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.scrollView);
gridView = (GridView) findViewById(R.id.gridView1);
horizontalScrollView.setHorizontalScrollBarEnabled(false);// 隐藏滚动条
getScreenDen();
LIEWIDTH = dm.widthPixels / NUM;
setValue();
}
private void setValue() {
MyGridViewAdapter adapter = new MyGridViewAdapter(this, LIE);
gridView.setAdapter(adapter);
LayoutParams params = new LayoutParams(adapter.getCount() * LIEWIDTH,
LayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params);
gridView.setColumnWidth(dm.widthPixels / NUM);
gridView.setStretchMode(GridView.NO_STRETCH);
int count = adapter.getCount();
gridView.setNumColumns(count);
}
private void getScreenDen() {
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
二、适配器
package com.max.test.gridview_viewpager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyGridViewAdapter extends BaseAdapter {
int count;
Context context;
public MyGridViewAdapter(Context context, int count) {
// TODO Auto-generated constructor stub
this.context = context;
this.count = count;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stud
ViewHolder holder;
if (null == convertView) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(
R.layout.listitem, null);
holder.tv = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText("数量 " + (position + 1) + " ");
return convertView;
}
class ViewHolder {
ImageView iv;
TextView tv;
}
}
三、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="78dip"
android:orientation="vertical"
android:background="#FF83FA" >
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="78dip"
android:layout_weight="0.10" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
四、listitem.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:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF" />
</LinearLayout>