码迷,mamicode.com
首页 > 其他好文 > 详细

scrollview嵌套listview滚动冲突解决方案;

时间:2016-04-14 22:10:34      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

主activity页面;

package com.example.scrollviewlistview;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;

import com.example.adapter.MybaseAdatpter;
import com.example.myview.Mylistview;


public class MainActivity extends Activity implements OnScrollListener {

    private List<String> list;
    private ScrollView myscollview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myscollview = (ScrollView) findViewById(R.id.sco);
        
        Mylistview listView=(Mylistview) findViewById(R.id.listview);
        initdata();
        listView.setAdapter(new MybaseAdatpter(list, getApplicationContext()));
        listView.setBackgroundColor(Color.RED);
        
        //设置listview的滚动监听;这个主要监听的是listview滑动到最后一条时,scrollview就能滚动了
        listView.setOnScrollListener(this);
        //设置listveiw的触屏事件;
        listView.setOnTouchListener(new OnTouchListener() {

            
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    //点击listview里面滚动停止时,scrollview拦截listview的触屏事件,就是scrollview该滚动了
                    myscollview.requestDisallowInterceptTouchEvent(false);
                } else {
                    //当listview在滚动时,不拦截listview的滚动事件;就是listview可以滚动,
                    myscollview.requestDisallowInterceptTouchEvent(true);
                }

                return false;
            }

            
        });

    }

    private void initdata() {
        list = new ArrayList<String>();
        for (int i = 0; i <30; i++) {
            list.add("data"+i);
            
        }
        
    }

//滚动监听,需实现的两个方法;
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        
    }

    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
        
        if((firstVisibleItem+visibleItemCount)==totalItemCount){
            //如果listview部分加载到最后一条了 ,拦截listview的触屏事件,意思就是scrollview可以滚动了;
            myscollview.requestDisallowInterceptTouchEvent(false);
            
            
            
            
        }
        
    } 

}

自定义的listview类;

package com.example.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class Mylistview extends ListView{

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

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

    public Mylistview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    //解决listview高度问题;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    
    int me=MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
    
    super.onMeasure(widthMeasureSpec, me);
}
}

适配器类;

package com.example.adapter;

import java.util.List;

import com.example.scrollviewlistview.R;

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

public class MybaseAdatpter extends BaseAdapter{
List<String> list;
Context context;

    public MybaseAdatpter(List<String> list, Context context) {
    super();
    this.list = list;
    this.context = context;
}

    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        
        convertView=View.inflate(context, R.layout.activity_main_item, null);
        TextView textView=(TextView) convertView.findViewById(R.id.te);
        textView.setText(list.get(position));
        
        return convertView;
    }

}

main.xml;

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
  android:id="@+id/sco"
     >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
   <com.example.myview.Mylistview 
       android:id="@+id/listview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       
       
       ></com.example.myview.Mylistview>
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />

<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="dfsdf"
    
    />
</LinearLayout>
</ScrollView>

main-item.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="match_parent"
    android:orientation="vertical" >
    <TextView 
        
        android:id="@+id/te"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />

</LinearLayout>

 

scrollview嵌套listview滚动冲突解决方案;

标签:

原文地址:http://www.cnblogs.com/zhengyanyan/p/5392778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!