标签:
方法一:自定义ListView
MainActivity.class
package com.bwie.test; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.view.View.MeasureSpec; import android.view.ViewGroup.LayoutParams; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { private ListView listView; private List<String> list; private boolean b; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.lv); // 创建数据 init(); // 设置适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); setListViewHeightBasedOnChildren(listView); //给listview设置滚动监听 setScroll(); } private void setScroll() { listView.setOnScrollListener(new OnScrollListener() { public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub if(scrollState==SCROLL_STATE_TOUCH_SCROLL&&b){ listView.getParent().getParent().requestDisallowInterceptTouchEvent(false); } } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub b=((firstVisibleItem+visibleItemCount)==totalItemCount); } }); } private void init() { list = new ArrayList<String>(); for (int i = 0; i < 50; i++) { list.add("data" + i); } } // 根据listview的数据来计算他的应得的高度,设置高度 public void setListViewHeightBasedOnChildren(ListView listView) { ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { listItem.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); // params.height = totalHeight // + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) // + listView.getPaddingTop() + listView.getPaddingBottom(); // int h = getWindowManager().getDefaultDisplay().getHeight(); // if (params.height > h / 2) { // params.height = h / 2; // } params.height = 500; listView.setLayoutParams(params); } }
自定义的listview类:MyListView.class
package com.bwie.test; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; public class MyListview extends ListView { public MyListview(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub //请求父亲(Scrollview不截断touch事件,因为Scrollview也有滚动效果) getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } }
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" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.bwie.test.MyListview android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" > </com.bwie.test.MyListview> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮1"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮2"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮3"/> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮4"/> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮5"/> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮6"/> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮7"/> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮8"/> <Button android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮9"/> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮5"/> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮6"/> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮7"/> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮8"/> <Button android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮9"/> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮6"/> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮7"/> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮8"/> <Button android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮9"/> <Button android:id="@+id/button5" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮5"/> <Button android:id="@+id/button6" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮6"/> <Button android:id="@+id/button7" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮7"/> <Button android:id="@+id/button8" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮8"/> <Button android:id="@+id/button9" android:layout_width="match_parent" android:layout_height="match_parent" android:text="按钮9"/> </LinearLayout> </ScrollView>
第二种方法:对listview设置触摸监听
MainActivity.class
package com.bwie.test; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.View.MeasureSpec; import android.view.ViewGroup.LayoutParams; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ScrollView; public class MainActivity extends Activity { private ListView listView; private List<String> list; private boolean b; private ScrollView sc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.lv); sc = (ScrollView) findViewById(R.id.sc); // 创建数据 init(); // 设置适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); setListViewHeightBasedOnChildren(listView); //给listview设置滚动监听 setScroll(); //对listview设置触摸监听 setTouch(); } private void setTouch() { // TODO Auto-generated method stub listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction()== 2){ sc.requestDisallowInterceptTouchEvent(true); } else{ sc.requestDisallowInterceptTouchEvent(false); } return false; } }); } private void setScroll() { listView.setOnScrollListener(new OnScrollListener() { public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub if(scrollState==SCROLL_STATE_TOUCH_SCROLL&&b){ listView.getParent().getParent().requestDisallowInterceptTouchEvent(false); } } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub b=((firstVisibleItem+visibleItemCount)==totalItemCount); } }); } private void init() { list = new ArrayList<String>(); for (int i = 0; i < 50; i++) { list.add("data" + i); } } // 根据listview的数据来计算他的应得的高度,设置高度 public void setListViewHeightBasedOnChildren(ListView listView) { ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { listItem.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); // params.height = totalHeight // + (listView.getDividerHeight() * (listAdapter.getCount() - 1)) // + listView.getPaddingTop() + listView.getPaddingBottom(); // int h = getWindowManager().getDefaultDisplay().getHeight(); // if (params.height > h / 2) { // params.height = h / 2; // } params.height = 500; listView.setLayoutParams(params); } }
标签:
原文地址:http://www.cnblogs.com/ldou/p/5391796.html