ListView嵌套GridView的简单实例
我的项目想实现一个listview里面的每个item都嵌套一个GridView,顶部还有主题等内容,如
总所周知,关于ListView嵌套GridView,最主要问题莫过于嵌套状态下滑动冲突问题,具体怎么解决,喜欢冗长无注释的代码的,请点击这里这篇文章跟其他的都大同小异了,不过在缺少注释的情况下,我发现了一点点小问题:
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//public MyGridView(Context context, AttributeSet attrs, int //defStyleAttr, int defStyleRes) {
// super(context, attrs, defStyleAttr, defStyleRes);
// }
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// expandSpec:拓展空间,其中MeasureSpec.AT_MOST为“最大模式”
// AT_MOST:最大模式,比喻为布局里的match_parent
// EXACTLY:精确模式,比喻为布局里的"50dp"
// UNSPECIFIED:未指定模式,比喻为布局里的wrap_content
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
// @Override
// public boolean dispatchTouchEvent(MotionEvent ev) {
// if (ev.getAction() == MotionEvent.ACTION_MOVE){
// //返回true直接结束当前事件消费
// return true;
// }
// return super.dispatchTouchEvent(ev);
// }
// boolean haveScrollbars = false;
// public void setHaveScrollbar(boolean haveScrollbar) {
// this.haveScrollbar = haveScrollbar;
// }
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// if (haveScrollbars == false) {
// int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
// super.onMeasure(widthMeasureSpec, expandSpec);
// } else {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// }
// }
}
上面这个GridView就算自定义好了,接下来我们简单贴上listview的适配器主要方法getview(…)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_lv_gridview, null);
holder.mMyGridView = (MyGridView) convertView.findViewById(R.id.gridView_show_controller);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//注意:这是重要的地方
//鉴于我们想让每个item下的GridView都能独立,因此只能通过new 出适配器来
//单独定义每个item,这样才能让每个listview的item内容都有不同的GridView
DevicesAdapter devicesAdapter = new DevicesAdapter(mContext);
devicesAdapter.setDevicesList(deviceList);
holder.mMyGridView.setAdapter(devicesAdapter);
holder.mMyGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//监听最后一个item(position==list.size()-1),
//动态更改其作用功能(比如增加一条数据,或者blablabla...)
if (position == parent.getCount() - 1) {
mItemListener.onAddClick();
} else {
mItemListener.onDeviceItemClick(deviceList.get(position));
}
}
});
return convertView;
}
static class ViewHolder {
MyGridView mMyGridView;
}
只要再定义GridView的适配器就大功告成了(适配器相信到这时候大家应该都很熟了,我就不注释了哈,请原谅我比较懒)
public class DevicesAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
private ArrayList<DeviceInfos> devicesList;
public DevicesAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public void setDevicesList(ArrayList<DeviceInfos> devicesList) {
this.devicesList = devicesList;
}
@Override
public int getCount() {
if (devicesList == null) {
return 1;
}
return devicesList.size() + 1;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_grid_view, null);
holder = new ViewHolder();
holder.mImageView = (ImageView) convertView.findViewById(R.id.img_controller);
holder.mTextView = (TextView) convertView.findViewById(R.id.tv_controller_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (devicesList!=null && position < devicesList.size()) {
String childDeviceName = devicesList.get(position).getChildDeviceName();
holder.mImageView.setImageResource(R.drawable.huajidadi);
holder.mTextView.setText(childDeviceName);
} else {
holder.mImageView.setImageResource(R.drawable.add);
holder.mTextView.setVisibility(View.GONE);
}
return convertView;
}
static class ViewHolder {
ImageView mImageView;
TextView mTextView;
}
}
布局我就不给了,就简单的几个item的布局,请同学们自己定义吧…
总结:解决滑动冲突的方法或者涉及点击、移动等属性的,用自定义控件再重写事件分发,可以很好的解决。但是对于同样是ViewGroup就没办法像View的组件那样,通过调用onInterceptTouchEvent(MotionEvent ev)进行拦截了,因此只能另寻他法,而这关键就是重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)这个方法,使得GridView的“控件大小”被固定,这样就不会与另一个滑动事件冲突了。
您可能感兴趣的文章:Android ScrollView 下嵌套 ListView 或 GridView出现问题解决办法Android 中ScrollView嵌套GridView,ListView的实例Android开发之ListView、GridView 详解及示例代码关于Android中ListView嵌套GridView的问题Android RecyclerView详解之实现 ListView GridView瀑布流效果Android之ScrollView嵌套ListView和GridView冲突的解决方法android ListView和GridView拖拽移位实现代码Android 中ListView和GridView赋值错位