欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ListView嵌套GridView使用详解及注意事项

程序员文章站 2023-11-25 10:21:28
mainactivity如下: 复制代码 代码如下: package cn.testlistviewandgridview; import java.util.arrayl...
mainactivity如下:
复制代码 代码如下:

package cn.testlistviewandgridview;
import java.util.arraylist;
import java.util.hashmap;
import android.app.activity;
import android.os.bundle;
import android.widget.listview;
/**
* demo描述:
* listview嵌套gridview使用详解
* 即listview的每个item中都包含一个gridview
*
* 注意事项:
* 由于listview和gridview都是可滑动的控件.
* 所以需要自定义gridview,重写其onmeasure()方法.
* 在该方法中使gridview的高为wrap_content的大小,否则gridview中
* 的内容只能显示很小一部分
*
* 参考资料:
* 1 http://bbs.csdn.net/topics/380245627
* 2 http://blog.csdn.net/lsong89/article/details/8598856
* thank you very much
*/
public class mainactivity extends activity {
private listview mlistview;
private listviewadapter mlistviewadapter;
private arraylist<arraylist<hashmap<string,object>>> marraylist;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
init();
}
private void init(){
mlistview=(listview) findviewbyid(r.id.listview);
initdata();
mlistviewadapter=new listviewadapter(marraylist, mainactivity.this);
mlistview.setadapter(mlistviewadapter);
}
private void initdata(){
marraylist=new arraylist<arraylist<hashmap<string,object>>>();
hashmap<string, object> hashmap=null;
arraylist<hashmap<string,object>> arraylistforeverygridview;

for (int i = 0; i < 10; i++) {
arraylistforeverygridview=new arraylist<hashmap<string,object>>();
for (int j = 0; j < 5; j++) {
hashmap=new hashmap<string, object>();
hashmap.put("content", "i="+i+" ,j="+j);
arraylistforeverygridview.add(hashmap);
}
marraylist.add(arraylistforeverygridview);
}

}
}

listviewadapter如下:
复制代码 代码如下:

package cn.testlistviewandgridview;
import java.util.arraylist;
import java.util.hashmap;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.gridview;
import android.widget.imageview;
public class listviewadapter extends baseadapter {
private arraylist<arraylist<hashmap<string, object>>> mlist;
private context mcontext;

public listviewadapter(arraylist<arraylist<hashmap<string, object>>> 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.listview_item, null, false);
holder.imageview = (imageview) convertview.findviewbyid(r.id.listview_item_imageview);
holder.gridview = (gridview) convertview.findviewbyid(r.id.listview_item_gridview);
convertview.settag(holder);
} else {
holder = (viewholder) convertview.gettag();
}


if (this.mlist != null) {
if (holder.imageview != null) {
holder.imageview.setimagedrawable
(mcontext.getresources().getdrawable(r.drawable.e));
}
if (holder.gridview != null) {
arraylist<hashmap<string, object>> arraylistforeverygridview = this.mlist.get(position);
gridviewadapter gridviewadapter=new gridviewadapter(mcontext, arraylistforeverygridview);
holder.gridview.setadapter(gridviewadapter);
}
}
return convertview;
}

private class viewholder {
imageview imageview;
gridview gridview;
}
}

gridviewadapter如下:
复制代码 代码如下:

package cn.testlistviewandgridview;
import java.util.arraylist;
import java.util.hashmap;
import android.content.context;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.button;
import android.widget.toast;
public class gridviewadapter extends baseadapter{
private context mcontext;
private arraylist<hashmap<string, object>> mlist;

public gridviewadapter(context mcontext,arraylist<hashmap<string, object>> 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.gridview_item, null, false);
holder.button = (button)convertview.findviewbyid(r.id.gridview_item_button);
convertview.settag(holder);

} else {
holder = (viewholder) convertview.gettag();
}


if (this.mlist != null) {
hashmap<string, object> hashmap = this.mlist.get(position);
if (holder.button != null) {
holder.button.settext(hashmap.get("content").tostring());
holder.button.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
toast.maketext(mcontext, "第"+(position+1)+"个", toast.length_short).show();
}
});
}
}
return convertview;
}

private class viewholder {
button button;
}
}

noscrollgridview如下:
复制代码 代码如下:

package cn.testlistviewandgridview;
import android.content.context;
import android.util.attributeset;
import android.widget.gridview;
public class noscrollgridview extends gridview {
public noscrollgridview(context context) {
super(context);

}
public noscrollgridview(context context, attributeset attrs) {
super(context, attrs);
}

@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
int expandspec = measurespec.makemeasurespec(integer.max_value >> 2,measurespec.at_most);
super.onmeasure(widthmeasurespec, expandspec);
}
}

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"
>
<listview
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="false"
/>
</relativelayout>

listview_item.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="fill_parent"
android:orientation="vertical"
>
<imageview
android:id="@+id/listview_item_imageview"
android:layout_width="fill_parent"
android:layout_height="20dip"
android:scaletype="fitxy"
android:src="@drawable/e"
/>
<cn.testlistviewandgridview.noscrollgridview
android:id="@+id/listview_item_gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchmode="columnwidth"
android:verticalspacing="5dip"
android:horizontalspacing="5dip"
android:numcolumns="2"/>
</linearlayout>

gridview_item.xml如下:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dip"
>
<button
android:id="@+id/gridview_item_button"
android:layout_width="140dip"
android:layout_height="40dip"
android:background="@drawable/e"
android:textcolor="@android:color/background_light"
android:clickable="true"
/>
</linearlayout>