private GridView gv_2; private GridView gv_1; private List<String> list; private List<String> lists; gv_1=(GridView)findViewById(R.id.gv_1); gv_2=(GridView)findViewById(R.id.gv_2); initData(); gv_1.setAdapter(new MyAdapter(this,list)); gv_2.setAdapter(new MyAdapter(this,lists)); } private void initData() { list=new ArrayList<String>() ; lists=new ArrayList<String>() ; for (int i=0;i<24;i++){ list.add("测试数据"+i); lists.add("+测试数据"+i); } } }
====================================================================
public class MyAdapter extends BaseAdapter{ private Context con; private List<String> list; public MyAdapter(Context con,List<String> list) { this.con = con; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if(convertView==null){ convertView=View.inflate(con, R.layout.layout_xx1,null); holder=new ViewHolder(); holder.but=(Button) convertView.findViewById(R.id.but); convertView.setTag(holder); }else { holder=(ViewHolder)convertView.getTag(); } holder.but.setText(list.get(position).toString()); return convertView; } public class ViewHolder{ private Button but; } }===========================================================================
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.day19_20171220.MainActivity"> <TextView android:layout_weight="1" android:text="我的频道" android:textSize="35dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <GridView android:numColumns="4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="6" android:id="@+id/gv_1"></GridView> <TextView android:layout_weight="1" android:text="频道推荐" android:textSize="35dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <GridView android:numColumns="4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="6" android:id="@+id/gv_2"></GridView> </LinearLayout>======================================================================================
layout_xx1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/but"/> </LinearLayout>