ListView嵌套GridView使用详解及注意事项
Posted 志向远大
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView嵌套GridView使用详解及注意事项相关的知识,希望对你有一定的参考价值。
1.listview万能适配器
package com.tts.hotelmanager.adapter;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.tts.hotelmanager.bean.ViewHoldItem;
/**
* ListView适配器
*
* @param <T>
*/
public class Adapter<T> extends BaseAdapter
public List<T> list;
private Context context;
private int layout;
public Adapter(Context context, int layout)
this.context = context;
this.layout = layout;
list = new ArrayList<T>();
public void setData(List<T> list)
this.list = list;
notifyDataSetChanged();
public void setClear()
list.clear();
notifyDataSetChanged();
public void remove(T t)
if (list.size() > 0 && list.contains(t))
list.remove(t);
notifyDataSetChanged();
public void removeInt(int i)
if (list.size() > 0 && i < list.size())
list.remove(i);
notifyDataSetChanged();
public void setDataChar(T t)
list.add(t);
this.notifyDataSetChanged();
@Override
public int getCount()
return list.size();
@Override
public T getItem(int position)
return (T)list.get(position);
@Override
public long getItemId(int position)
return position;
public List<T> getList()
return list;
@Override
public View getView(int position, View view, ViewGroup arg2)
ViewHoldItem vh = ViewHoldItem.get(context, position, view, layout,
arg2);
face.adapter(vh, list.get(position));
return vh.getViewHoldIitem();
public void setInterface(Interface1<T> face)
this.face = face;
private Interface1<T> face;
public interface Interface1<T>
public void adapter(ViewHoldItem viewHolder, T t);
2.MainActivity的代码
// 设置adapter的item
mAdapter = new Adapter<HashMap<String, String>>(this,
R.layout.item_comment_content);
mAdapter.setInterface(this); // 实现接口
// 给adapter设置数据以及绑定到listview上面
mAdapter.setData(mCommentsDetailList);
mListView.setAdapter(mAdapter);
3.因为由于ListView和GridView都是可滑动的控件,所以需要自定义GridView,重写其onMeasure()方法。在该方法中使GridView的高为wrap_content的大小,否则GridView中 的内容只能显示很小一部分。item中GridView的布局代码
<com.tts.hotelmanager.adapter.NoScrollGridView
android:id="@+id/item_comment_content_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:horizontalSpacing="10dp"
android:numColumns="5"
android:scrollbars="vertical"
android:verticalSpacing="10dp"
android:visibility="visible" />
4.自定义不可滑动的GridView
package com.tts.hotelmanager.adapter;
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);
5.在重写adapter的方法里面,获取服务器返回的图片路径,然后转换为String[]数组,然后遍历把每个图片uri对象存在arrayListForEveryGridView 里面,然后把数组放在GridViewAdapter 里面,通过GridViewAdapter 映射到gridView控件上面。
// 显示评论提交的图片
String imageAddress = hm.get("imageAddress");
hashMap = new HashMap<String, Object>();
arrayListForEveryGridView = new ArrayList<HashMap<String, Object>>();
if (!imageAddress.equals(""))
String[] imageArray = imageAddress.split(",");
for (int i = 0; i < imageArray.length; i++)
String uri = ConstantsMember.SHOW_PICTURE_URL + imageArray[i];
hashMap.put("image" + i, uri);
arrayListForEveryGridView.add(hashMap);
// 把数据放在adapter里面,通过adapter映射到gridView控件上面
GridViewAdapter gridViewAdapter = new GridViewAdapter(
CheckCommentActivity.this, arrayListForEveryGridView);
gridView.setAdapter(gridViewAdapter);
6.
package com.tts.hotelmanager.adapter;
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.ImageView;
import com.bumptech.glide.Glide;
import com.tts.hotelmanager.R;
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.item_check_comment_image, null, false);
holder.image = (ImageView) convertView
.findViewById(R.id.item_image);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
// 判断arrayListForEveryGridView是否为空
if (this.mList != null)
HashMap<String, Object> hashMap = this.mList.get(position);
// 判断获取的路径是否为空
if (holder.image != null)
String uri = hashMap.get("image" + position).toString();
if (uri != null)
// 用Glide显示图片,可以自己到网上下载jar包 Glide.with(mContext).load(uri).centerCrop()
.crossFade().into(holder.image);
return convertView;
private class ViewHolder
ImageView image;
以上是关于ListView嵌套GridView使用详解及注意事项的主要内容,如果未能解决你的问题,请参考以下文章
android listview里面能嵌套gridview吗
android listView嵌套gridview的使用心得
listview嵌套gridview,怎么设置gridview的点击事件