GridView布局,自定义适配器,水平滚动

时间:2022-02-21 16:02:36

添加GridItem布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>

主界面添加GridView布局XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_gravity="center"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_margin="10dp" > <GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
>
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>

自定义GridView适配器

package com.example.psq;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 MyGridAdapter extends BaseAdapter{ LayoutInflater inflater = null;
ArrayList<AppInfo> listInfo;
public MyGridAdapter(Context context,ArrayList<AppInfo> listInfo){
//inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = LayoutInflater.from(context);
//this.inflater = inflater;
this.listInfo = listInfo;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listInfo.size();
}
@Override
public Object getItem(int index) {
// TODO Auto-generated method stub
return listInfo.get(index);
}
@Override
public long getItemId(int index) {
// TODO Auto-generated method stub
return index;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null || convertView.getTag() == null){
convertView = inflater.inflate(R.layout.grid_item,null);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView1);
holder.textView = (TextView)convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}else{ holder = (ViewHolder)convertView.getTag();
}
AppInfo appInfo = listInfo.get(position);
holder.imageView.setImageResource(appInfo.rid);
holder.textView.setText(appInfo.title);
return convertView;
}
public class ViewHolder{
ImageView imageView;
TextView textView;
}
}

主界面Activity

package com.example.psq;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.example.psq.R; import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity{ GridView gridView;
LayoutInflater inflater;
ArrayList<AppInfo> listInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); gridView = (GridView)findViewById(R.id.gridView1);
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater = LayoutInflater.from(this);
listInfo = new ArrayList<AppInfo>();
for(int i=0;i<15;i++){
AppInfo appInfo = new AppInfo();
appInfo.rid = R.drawable.icon001;
appInfo.title = "001";
listInfo.add(appInfo);
} int size = listInfo.size();
int length = 100;
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
float density = dm.density;
int gridviewWidth = (int) (size * (length + 4) * density);
int itemWidth = (int) (length * density);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
gridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
gridView.setColumnWidth(itemWidth); // 设置列表项宽
gridView.setHorizontalSpacing(5); // 设置列表项水平间距
gridView.setStretchMode(GridView.NO_STRETCH);
gridView.setNumColumns(size); // 设置列数量=列表集合数
//gridView.setAdapter(new MyAdapter());
gridView.setAdapter(new MyGridAdapter(this,listInfo));
}
@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;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

AppInfo类

public class AppInfo {
public int rid;
public String title;
}