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); } }

ListView 中嵌套 GridView的更多相关文章

  1. ListView中嵌套GridView点击事件

    做一个项目时,需要在ListView中嵌套GridView,因为ListView的每个条目中不一定出现GridView,那么问题来了,添加GridView的Item的点击事件后,有GridView出现 ...

  2. [置顶] gridview中嵌套gridview(并实现子gridview的数据绑定),页面传值,加密,数据绑定

    先来张效果图 gridview 中嵌套gridview的原理是这样的,在父gridview中建一个摸板列,然后再模版列当中在放入子gridview,然后再父gridview的OnRowDataBoun ...

  3. ScrollView中嵌套GridView,ListView只显示一行的解决办法

    转载:http://blog.csdn.net/luohai859/article/details/39347583 关于为什么只显示一行,个人理解是:如果单独使用GridView和ListView, ...

  4. ScrollView中嵌套GridView,Listview的办法

    按照android的标准,ScrollView中是不能嵌套具有滑动特性的View的,但是有时如果设计真的有这样做的需要,或者为了更方便简单的实现外观(比如在外在的大布局需要有滑动的特性,并且内部有类似 ...

  5. android 之 ListView 里面嵌套 GridView 遇到的问题及其解决方法。

    我们直接入主题.所有问题例子请参照下图 1,怎样使图片具有点击事件? 答: 解决方法: 在你的BaseAdapter里面不要设置下面这三个东西,然后再设置GridView的onItemClick. g ...

  6. ListView 里面嵌套 GridView 遇到的问题及其解决方法。

    我们直接入主题.所有问题例子请参照下图 1,怎样使图片具有点击事件? 答: 解决方法: 在你的BaseAdapter里面不要设置下面这三个东西,然后再设置GridView的onItemClick. g ...

  7. Android如何在ListView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  8. 在ListView中使用GridView, Style这样写:

    参考:http://msdn.microsoft.com/zh-cn/library/vstudio/ms788747.aspx 需求,自定义ListView中的滚动条 这岂不是很简单?刷刷刷写完了, ...

  9. Android 在ListView中嵌套ListView的事件处理

    前天在工作中遇到在ListView中的Item需要用ListView来展现处理后的内容,然后就遇到了一个很头疼的问题,作为Item的ListView没法进行滑动,而且显示也不正常,只是显示几个子Ite ...

随机推荐

  1. mysql表情存储报错问题

    mysql采用utf-8字符编码,但在移动端使用输入法的表情并存储数据库的时候,出现错误. java.sql.SQLException: Incorrect string value: '\xF0\x ...

  2. HDU 3191 次短路长度和条数

    http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/det ...

  3. pyhton基础

    python是一种什么语言?python是一种动态解释性的强类型定义的语言(1)编程语言分类 编译型: 把源程序的每一条语句都编译成机器语言,并保存成二进制文件, 这样运行时计算机可以直接以机器语言来 ...

  4. express的proxy实现前后端分离

    var express = require('express') var proxy = require('http-proxy-middleware') var app = express() ap ...

  5. child_process

    child_process const { spawn } = require('child_process'); const ls = spawn('ls', ['-lh', '/usr']); l ...

  6. Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: Unable to load the mojo 'resources' (or one of its required components)

    1.异常提示: Description Resource Path Location Type Execution default-resources of goal org.apache.maven ...

  7. inline-block元素,在同一行上下显示

    两个元素使用了inline-block,并列显示时,会上下显示,给人感觉不在同一行 原因:其中一个元素使用了overflow:hidden,导致了基线变更 解决:1.另一个元素也添加overflow: ...

  8. java 从网上下载文件的几种方式

    package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import ...

  9. restful课程凌杂知识点

      request.post:字典形式数据 request.body:收到的是源数据

  10. jar 解压war包到指定目录

    用 jar -xvf  .jar 命令默认解压到当前目录,想要解压到指定目录 需要使用unzip .jar -d 目录 如: unzip pay.war -d /home/zookeeper1/tes ...