在你的MainActivity.java类方法onCreate写一段代码:
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
// Create ScreenSlidePagerAdapter class and then implement viewPager.
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
在你的activity_main.xml文件中实现这个东西。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="140sp"
android:layout_marginBottom="10dp"
android:layout_gravity="center_horizontal"/>
<com.xx.xxx.MyGridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</ScrollView>
创建自定义类,即 MyGridView.java 文件:
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.MATCH_PARENT) {
// The great Android "hackatlon", the love, the magic.
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
else {
// Any other height should be respected as is.
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
已编辑:
根据我的代码,我的 ImageAdapter.java 类:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download, R.drawable.download,
R.drawable.download
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
希望对你有帮助...!继续编码。