关于多个ListView与ScrollView滑动冲突的一个简单处理方法

这个是我的第一个博文。希望可以帮到一些开发者!!!

废话不多说直接上代码

主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="smartnet.testlistview.MainActivity">
    <ScrollView
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第一个listView" />
            <smartnet.testlistview.ListViewForScrollView
                android:id="@+id/listview1"
                android:padding="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </smartnet.testlistview.ListViewForScrollView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二个listView" />
            <smartnet.testlistview.ListViewForScrollView
                android:id="@+id/listview2"
                android:padding="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </smartnet.testlistview.ListViewForScrollView>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第三个listView" />
            <smartnet.testlistview.ListViewForScrollView
                android:id="@+id/listview3"
                android:padding="3dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </smartnet.testlistview.ListViewForScrollView>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

ListView的item布局

<?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="vertical">
    <RelativeLayout
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/set_item_name"
            android:layout_centerVertical="true"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/set_item_photo"
            android:layout_alignParentRight="true"
            android:background="@mipmap/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

问题的解决方法代码

public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }
    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /*这个类只需要重写这个onMeasure的方法即可*/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /*计算宽高是关键*/
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

主Activity的资源加载

package smartnet.testlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private ListView listView1,listView2,listView3;
    /*装在listView的资源文字及图片*/
    private String[] names1={"设置11","设置12","设置13","设置14","设置15","设置16"};
    private int [] photo1={R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right};
    private String[] names2={"设置21","设置22","设置23","设置24","设置25","设置26"};
    private int [] photo2={R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right};
    private String[] names3={"设置31","设置32","设置33","设置34","设置35","设置36"};
    private int [] photo3={R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right,R.mipmap.right};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntiView();
    }
    /*初始化视图*/
    private void IntiView() {
        listView1=(ListView)findViewById(R.id.listview1);
        listView2=(ListView)findViewById(R.id.listview2);
        listView3=(ListView)findViewById(R.id.listview3);
        /*第一个ListView的适配器*/
        listView1.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return names1.length;
            }

            @Override
            public Object getItem(int i) {
                return names1[i];
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int position, View view, ViewGroup viewGroup) {
                View v=View.inflate(MainActivity.this, R.layout.set_item, null);
                TextView item_name=(TextView) v .findViewById(R.id.set_item_name);
                ImageView item_photo=(ImageView)v.findViewById(R.id.set_item_photo);
                item_name.setText(names1[position]);
                item_photo.setBackgroundResource(photo1[position]);
                return v;
            }
        });
        /*第个ListView的适配器*/
        listView2.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return names2.length;
            }

            @Override
            public Object getItem(int i) {
                return names2[i];
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int position, View view, ViewGroup viewGroup) {
                View v=View.inflate(MainActivity.this, R.layout.set_item, null);
                TextView item_name=(TextView) v .findViewById(R.id.set_item_name);
                ImageView item_photo=(ImageView)v.findViewById(R.id.set_item_photo);
                item_name.setText(names2[position]);
                item_photo.setBackgroundResource(photo2[position]);
                return v;
            }
        });
        /*第三个ListView的适配器*/
        listView3.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return names3.length;
            }

            @Override
            public Object getItem(int i) {
                return names3[i];
            }

            @Override
            public long getItemId(int i) {
                return i;
            }

            @Override
            public View getView(int position, View view, ViewGroup viewGroup) {
                View v=View.inflate(MainActivity.this, R.layout.set_item, null);
                TextView item_name=(TextView) v .findViewById(R.id.set_item_name);
                ImageView item_photo=(ImageView)v.findViewById(R.id.set_item_photo);
                item_name.setText(names3[position]);
                item_photo.setBackgroundResource(photo3[position]);
                return v;
            }
        });
        /*listView1点击事件的Toast*/
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),names1[i],Toast.LENGTH_SHORT).show();
            }
        });
        /*listView2点击事件的Toast*/
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),names2[i],Toast.LENGTH_SHORT).show();
            }
        });
        /*listView3点击事件的Toast*/
        listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),names3[i],Toast.LENGTH_SHORT).show();
            }
        });
    }

}

效果图:




希望更多的人可以贡献自己的源码,所以辛苦,可是可以帮到很多开发者!!!记住免费哦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值