1、主布局文件
<?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="fill_parent" android:background="#F5F5F5" android:orientation="vertical" > <ListView android:id="@+id/lv_apply_classify" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:dividerHeight="10dip" > </ListView> </LinearLayout>
2、listview 对应的item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_apply_classify_name" android:layout_width="match_parent" android:layout_height="35dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:gravity="center_vertical" android:singleLine="true" android:text="应用" android:textSize="@dimen/font_body_16" android:textColor="@color/black" /> <com.example.view.ClassifyGridView android:id="@+id/gv_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dp" android:horizontalSpacing="1dip" android:numColumns="2" android:verticalSpacing="1dip" > </com.<span style="font-family: Arial, Helvetica, sans-serif;">exaple</span><span style="font-family: Arial, Helvetica, sans-serif;">.ClassifyGridView></span> </LinearLayout>
3、gridview对应的item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/lv_item_selector" android:orientation="vertical" android:padding="5dip" > <TextView android:id="@+id/tv_apply_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_marginTop="5dip" android:singleLine="true" android:text="应用" android:textSize="@dimen/font_body_16" android:textColor="@color/black" /> <TextView android:id="@+id/tv_description_name" android:layout_width="120dip" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:layout_marginTop="5dip" android:maxLines="2" android:textSize="@dimen/font_body_14" android:text="应用 描述" android:textColor="@color/gray" /> </LinearLayout>
4、主Activity
public class ClassifyActivity extends Activity { private ListView mListView; private ClassifyListViewAdapter mListViewAdapter; private List<ApplyTest> mArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.apply_main_classify); init(); } private void init() { mListView = (ListView) findViewById(R.id.lv_apply_classify); initData(); mListViewAdapter = new ClassifyListViewAdapter(mArrayList, ClassifyActivity.this); mListView.setAdapter(mListViewAdapter); } private void initData() { mArrayList = new ArrayList<ApplyTest>(); for (int i = 0; i < 2; i++) { ApplyTest testApplyTest = new ApplyTest(); testApplyTest.setName("测试分类"+i); List<ApplySub> arrayListForEveryGridView = new ArrayList<ApplySub>(); for (int j = 0; j < 4; j++) { ApplySub sub = new ApplySub(); sub.setName("子项"+i+j); arrayListForEveryGridView.add(sub); } testApplyTest.setSubs(arrayListForEveryGridView); mArrayList.add(testApplyTest); } } public class ApplyTest { private String name; private List<ApplySub> subs; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<ApplySub> getSubs() { return subs; } public void setSubs(List<ApplySub> subs) { this.subs = subs; } } public class ApplySub { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }
5、listview适配器
public class ClassifyListViewAdapter extends BaseAdapter { private List<ApplyTest> mList; private Context mContext; public ClassifyListViewAdapter(List<ApplyTest> mList, Context mContext) { super(); this.mList = mList; this.mContext = mContext; } @Override public int getCount() { if (mList == null) { return 0; } else { return this.mList.size(); } } @Override public Object getItem(int position) { if (mList == null) { return null; } else { return this.mList.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(this.mContext).inflate(R.layout.apply_main_classify_gv, null); holder.gridView = (GridView) convertView.findViewById(R.id.gv_toolbar); holder.tv_apply_classify_name = (TextView) convertView.findViewById(R.id.tv_apply_classify_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (this.mList != null) { if (holder.gridView != null) { ApplyTest mApplyTest = this.mList.get(position); ClassifyGridViewAdapter gridViewAdapter = new ClassifyGridViewAdapter(mContext,mApplyTest.getSubs()); holder.gridView.setAdapter(gridViewAdapter); holder.tv_apply_classify_name.setText("分类"+position+mApplyTest.getName()); } } return convertView; } private class ViewHolder { GridView gridView; public TextView tv_apply_classify_name; } }
6、gridview 适配器
public class ClassifyGridViewAdapter extends BaseAdapter { private Context mContext; private List<ApplySub> mList; public ClassifyGridViewAdapter(Context mContext, List<ApplySub> mList) { super(); this.mContext = mContext; this.mList = mList; } @Override public int getCount() { if (mList == null) { return 0; } else { return this.mList.size(); } } @Override public Object getItem(int position) { if (mList == null) { return null; } else { return this.mList.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(this.mContext).inflate(R.layout.apply_main_classify_group, null); holder.tv_apply_name = (TextView) convertView.findViewById(R.id.tv_apply_name); holder.tv_description_name = (TextView) convertView.findViewById(R.id.tv_description_name); holder.iv_apply_classify_icon = (RemoteImageView) convertView.findViewById(R.id.iv_apply_classify_icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (this.mList != null) { ApplySub sApplySub = this.mList.get(position); if (holder.tv_apply_name != null) { holder.tv_apply_name.setText(sApplySub.getName()); // holder.tv_description_name.setText(hashMap.get("content").toString()); holder.tv_apply_name.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "第" + (position + 1) + "个", Toast.LENGTH_SHORT).show(); } }); } } return convertView; } private class ViewHolder { TextView tv_apply_name; TextView tv_description_name; } }
7、重写gridview让其不滚动
public class ClassifyGridView extends GridView { public ClassifyGridView(android.content.Context context,android.util.AttributeSet attrs) { super(context, attrs); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
时间: 2024-10-11 12:41:54