直接贴代码
MainActivity.java
public class MainActivity extends AppCompatActivity { private GridView _gridView = null; private List<ObjectEntity> _dataSource = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initGridView(); } private void initGridView() { _gridView = (GridView) findViewById(R.id.grid_view); _dataSource = new ArrayList<>(); for (int i = 0; i < 8; i++) { ObjectEntity object = new ObjectEntity(); object.imgUrl = "http://a.hiphotos.baidu.com/album/w%3D2048/sign=3da6584ff2deb48ffb69a6dec4273b29/960a304e251f95ca11945098c8177f3e670952bf.jpg"; object.desc = "这是list描述信息"; _dataSource.add(object); } MyAdapter listAdapter = new MyAdapter(_dataSource, this); _gridView.setAdapter(listAdapter); _gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ObjectEntity data = _dataSource.get(position); Toast.makeText(getApplicationContext(), data.desc, Toast.LENGTH_SHORT).show(); } }); } }
MyAdapter.java
package bbb.aaa.testgridview; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class MyAdapter extends BaseAdapter { private List<ObjectEntity> dataSourceArray; private Context activityContext; public MyAdapter(List<ObjectEntity> dataSource, Context context) { this.dataSourceArray = dataSource; this.activityContext = context; } @Override public int getCount() { return this.dataSourceArray.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if ( null == convertView ) { LayoutInflater inflater = ((Activity)activityContext).getLayoutInflater(); convertView = inflater.inflate(R.layout.gridview_item, null); } ObjectEntity data = dataSourceArray.get(position); TextView info = (TextView)convertView.findViewById(R.id.gridview_text); info.setText(data.desc); return convertView; } }
ObjectEntity.java
package bbb.aaa.testgridview; public class ObjectEntity { public String imgUrl; public String desc; }
activity_main.xml
<RelativeLayout 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"> <TextView android:id="@+id/title_bar_text" android:layout_width="match_parent" android:layout_height="45dip" android:layout_alignParentTop="true" android:gravity="center_vertical|center_horizontal" android:text="12121212" android:textSize="20dip" android:textColor="#fff"/> <GridView android:id="@+id/grid_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/title_bar_text" android:layout_marginLeft="1dip" android:layout_marginRight="1dip" android:numColumns="2" android:columnWidth="5dip" android:cacheColorHint="#00000000" ></GridView> </RelativeLayout>
gridview_item.xml
<LinearLayout 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:paddingLeft="1dp" android:paddingRight="1dp" android:paddingTop="1dp" android:paddingBottom="1dp" android:orientation="vertical"> <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="80dp"> <TextView android:id="@+id/gridview_text" android:layout_width="fill_parent" android:layout_height="80dp" android:text="登录" android:layout_alignParentTop="false" android:layout_centerInParent="true" android:gravity="center|center_vertical|center_horizontal" android:textStyle="bold|normal" android:textColor="@color/white_color" android:textSize="10dp" android:background="@color/clean_blue_color" /> </RelativeLayout> </LinearLayout>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="clean_blue_color">#21afe5</color> <color name="test_blue_color">#123456</color> <color name="white_color">#ffffff</color> </resources>