android listview嵌套viewpager,viewpager嵌套gridview,解决内嵌无法显示以及事件冲突的问题

GridView:解决无法有高度的问题

/**
 * 解决和ScrollView嵌套使用时,滚动发生冲突
 */
public class MyGridView extends GridView
{

    public MyGridView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyGridView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyGridView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

VIewPager:解决事件冲突,不能滑动的问题,必须的设置一个高度,否则无法显示,可根据item计算并动态设置其高度


package com.wangzhi.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**


 * @version
 * @Date: 2015年11月27日 下午6:09:45
 */
public class GestateViewPager extends ViewPager {

	private float mDownX;
	private float mDownY;

	public GestateViewPager(Context context) {
		super(context);
	}

	public GestateViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownX = ev.getX();
			mDownY = ev.getY();
			getParent().requestDisallowInterceptTouchEvent(true);
			break;
		case MotionEvent.ACTION_MOVE:
			if (Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY)) {
				getParent().requestDisallowInterceptTouchEvent(true);
			} else {
				getParent().requestDisallowInterceptTouchEvent(false);
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			getParent().requestDisallowInterceptTouchEvent(false);
			break;

		default:
			break;
		}
		return super.dispatchTouchEvent(ev);
			
	}
}

设置viewpager的高度:初始化时,就设置一个默认高度,否则无法显示

<pre name="code" class="java">private void initHeadView() {
		
		adViewPager = (AdvPagerView) headView.findViewById(R.id.vp_welfare_ad);

		int categoryHeight = Tools.dip2px(mContext, 224);
		vpCategory = (GestateViewPager) headView.findViewById(R.id.vp_welfare_category);
		LayoutParams params = vpCategory.getLayoutParams();
		params.height = categoryHeight;
		headView.findViewById(R.id.rl_category_parent).getLayoutParams().height = categoryHeight;

		llDotsContainer = (LinearLayout) headView.findViewById(R.id.ll_dots_container);
		gvActiveManagement = (MyGridView) headView.findViewById(R.id.gv_welfare_management);
		lvWelfare.addHeaderView(headView);
	}


 

指示器:下标的小点

/**
	 * 
	 * @description 设置viewpagerIndicator
	 * @author zhongwr
	 * @param dotSize
	 *            下标点的个数:viewPager共有几页
	 * @update 2015年5月11日 下午5:51:30
	 */
	@SuppressLint("NewApi")
	private void initDots(int dotSize, int currentIndex) {
		if (dotSize > 1 && null != llDotsContainer) {
			llDotsContainer.setVisibility(View.VISIBLE);
			llDotsContainer.removeAllViews();
			ImageView imgDot = null;
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(8, 8);
			params.setMargins(5, 0, 0, 0);
			for (int i = 0; i < dotSize; i++) {
				imgDot = new ImageView(mContext);
				imgDot.setLayoutParams(params);
				if (i == currentIndex) {
					imgDot.setImageResource(R.drawable.fuli_red);
				} else {
					imgDot.setImageResource(R.drawable.fuli_grey);
				}
				llDotsContainer.addView(imgDot);
			}
		} else {
			llDotsContainer.setVisibility(View.GONE);
		}
	}


viewpager中嵌套gridview:此gridview必须得是代码动态new出来,否则无法显示

/**
	 * 
	 * @description 更新各类item的view数据:活动集中营。。。
	 * @author zhongwr
	 * @params
	 * @update 2016年1月6日 上午11:07:31
	 */
	private void updateCategoryView() {
		int size = spArrayCateMap.size();
		List<MyGridView> gridList = new ArrayList<MyGridView>();

		for (int i = 0; i < size; i++) {
			// listview头部的viewpager(一定要有固定高度)嵌套的gridview,不能使用inflate(无法显示),一定要使用new Gridview,才会显示
			// View view = View.inflate(mContext, R.layout.welfare_category_pager_item, null);
			// MyGridView gridView = (MyGridView) view.findViewById(R.id.gv_welfare_category_pager);
			MyGridView gv = new MyGridView(mContext);
			GridView.LayoutParams lParams = new GridView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
					AbsListView.LayoutParams.WRAP_CONTENT);
			gv.setLayoutParams(lParams);
			gv.setNumColumns(3);
			int horizontalSpace = Tools.dip2px(mContext, 15);
			int veritivalSpace = Tools.dip2px(mContext, 21);
			gv.setVerticalSpacing(veritivalSpace);
			gv.setHorizontalSpacing(horizontalSpace);
			gv.setPadding(horizontalSpace, 0, horizontalSpace, 0);
			setCategoryAdapter(gv, spArrayCateMap.get(i));
			gridList.add(gv);
		}
		if (!Tools.isListEmpty(gridList)) {
			vpCategory.setAdapter(new WelfareCategoryPagerAdapter(mContext, gridList));
			initDots(size, 0);
		}
	}


xml的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lay_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/group_chat_bottom" >

    <RelativeLayout
        android:id="@+id/include_title_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/basenormaltitle" />

        <TextView
            android:id="@+id/tv_welfare_feedback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="18dp"
            android:padding="10dp"
            android:text="意见反馈"
            android:textColor="@color/blue_1"
            android:textSize="14dp" />

        <ImageView
            android:id="@+id/iv_welfare_mine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="18dp"
            android:src="@drawable/welfare_my" />
    </RelativeLayout>

    <com.PullToRefreshListView
        android:id="@+id/lv_welfare_fragment"
        style="@style/AttentionListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/include_title_parent"
        android:divider="@null"
        android:scrollbars="none" >
    </com.PullToRefreshListView>

</RelativeLayout>

listview中嵌套的头部布局header.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="wrap_content"
    android:background="@color/white"
    android:orientation="vertical" >

    <com.AdvPagerView
        android:id="@+id/vp_welfare_ad"
        android:layout_width="match_parent"
        android:layout_height="135dp" />

    <!-- 运营位:分类入口item -->

    <RelativeLayout
        android:id="@+id/rl_category_parent"
        android:layout_width="match_parent"
        android:layout_height="224dp"
        android:layout_marginBottom="17dp"
        android:layout_marginTop="15dp" >

        <com.GestateViewPager
            android:id="@+id/vp_welfare_category"
            android:layout_width="match_parent"
            android:layout_height="224dp"
            android:persistentDrawingCache="animation" />

        <LinearLayout
            android:id="@+id/ll_dots_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="18dp"
            android:background="@color/transparent"
            android:orientation="horizontal" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/bg_1" />
    <!-- 运营位 :活动精选 -->

    <RelativeLayout
        android:id="@+id/rl_management_title_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="20dp"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/tv_management_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:drawableLeft="@drawable/fuli_activity"
            android:drawablePadding="7dp"
            android:text="活动精选"
            android:textColor="@color/red_1"
            android:textSize="16dp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/tv_management_title"
            android:background="@color/gray7_horizontal_line" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/tv_management_title"
            android:background="@color/gray7_horizontal_line" />
    </RelativeLayout>

    <com.MyGridView
        android:id="@+id/gv_welfare_management"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="9dp"
        android:numColumns="2"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_marginBottom="20dp"
        android:verticalSpacing="9dp" />

</LinearLayout>


注:整个界面是一个listview;listview的头部嵌套一个viewpager,viewpager放置gridview,解决girdview无法滑动的问题

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值