天天看点

解决ScrollView和GridListView和ListView滑动冲突方法(二)自定义ListView和GridView

效果

解决ScrollView和GridListView和ListView滑动冲突方法(二)自定义ListView和GridView

直接上代码

主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.duanlian.scollgridlistview.MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.duanlian.scollgridlistview.view.CustomGridView
                android:id="@+id/gridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#e6e6e6"
                android:horizontalSpacing="1dp"
                android:numColumns="4"
                android:verticalSpacing="1dp"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#EBEBEB"/>

            <com.duanlian.scollgridlistview.view.CustomListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>
           

item布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:background="#ffffff"
    android:layout_height="wrap_content">


    <TextView
        android:id="@+id/item_gridview_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/item_gridview_imageview"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="推荐"
        android:textColor="#333333"
        android:textSize="14sp"
        />

    <ImageView
        android:id="@+id/item_gridview_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="12dp" />

</RelativeLayout>
           

主Activity

package com.duanlian.scollgridlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.duanlian.scollgridlistview.adapter.MyAdapter;
import com.duanlian.scollgridlistview.view.CustomGridView;
import com.duanlian.scollgridlistview.view.CustomListView;

public class MainActivity extends AppCompatActivity {
    private CustomGridView mGridView;
    private CustomListView mListView;
    private MyAdapter mGridViewAdapter;
    private String[] data = {"设置", "收藏", "消息", "活动", "充值", "兑换", "注单", "更多"};
    private int[] img_function = {R.mipmap.geren_icon_shezhi, R.mipmap.geren_icon_shoucang, R.mipmap.geren_icon_xiaoxi,
            R.mipmap.geren_icon_huodong, R.mipmap.geren_icon_chongzhi,
            R.mipmap.geren_icon_huihuan, R.mipmap.geren_icon_zhudan, R.mipmap.geren_icon_gengduo,};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mGridView = (CustomGridView) findViewById(R.id.gridview);
        mListView = (CustomListView) findViewById(R.id.listview);
        mGridViewAdapter = new MyAdapter(this, data, img_function);
        mGridView.setAdapter(mGridViewAdapter);
        mListView.setAdapter(mGridViewAdapter);

    }


}
           

Adapter

package com.duanlian.scollgridlistview.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.duanlian.scollgridlistview.MainActivity;
import com.duanlian.scollgridlistview.R;

/**
 * Created by duanlian on 2016/10/28 0028.
 */

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private String[] data;
    private int[] img_function;

    public MyAdapter(MainActivity mainActivity, String[] data, int[] img_function) {
        this.mContext = mainActivity;
        this.data = data;
        this.img_function = img_function;


    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data!=null? data[position]:null;
    }

    @Override
    public long getItemId(int position) {
        return data!=null?position:;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_gridview, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.item_gridview_imageview);
            holder.textView = (TextView) convertView.findViewById(R.id.item_gridview_textview);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.imageView.setBackgroundResource(img_function[position]);
        holder.textView.setText(data[position]);

        return convertView;
    }
    static class ViewHolder{
        ImageView imageView;
        TextView textView;

    }
}
           

自定义的ListView

package com.duanlian.scollgridlistview.view;

import android.widget.ListView;

/**
 * 自定义listView
 */

public class CustomListView extends ListView {
    public CustomListView(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 >> ,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

    }
}
           

自定义的GridView

package com.duanlian.scollgridlistview.view;

import android.widget.GridView;

/**
 * gridView和scollView冲突,自定义gridView
 */

public class CustomGridView extends GridView {
    public CustomGridView(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 >> ,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

    }
}
           

这样就能解决嵌套的冲突啦

demo可以去我的资源里下载