Android之 如何解决ScrollView 和ListView滑动冲突的问题如何解决ScrollView can host only one direct child
来源:互联网 发布:apache性能测试工具ab 编辑:程序博客网 时间:2024/06/06 09:48
child。
解决办法:主要是ScrollView内部只能有一个子元素,即不能并列两个子元素,所以需要把所有的子元素放到一个LinearLayout内部或
RelativeLayout等其他布局
如何解决ScrollView 和ListView滑动冲突问题?
第一种解决办法:
关于嵌套滑动事件冲突的解决办法
下面的类可以按需要来使用,就能解决冲突问题,一般你只需设置一个父view或者一个子view,而不需要全部使用。
AbInnerListView
这个ListView不会与父亲是个ScrollView与List的产生事件冲突
AbInnerViewPager
这个ViewPager解决了外部是可滚动View(List或者scrollView) 与内部可滑动View的事件冲突问题
AbOuterListView
这个ListView不会与它里面有可滑动view的事件产生冲突
AbOuterScrollView
这个ScrollView与内部的滑动不冲突
AbUnSlideViewPager
不能滑动的ViewPager
实际场景:
(1)如果外边是ScrollView 内部有个ListView 那么你可以这样:将你的ListView换成AbInnerListView,或者将你的ScrollView换成AbOuterScrollView 。
(2)外边ListView或者scrollView,里面是一个ViewPager,那么你可以将ViewPager换成AbInnerViewPager。
(3)外边ListView,里面是ViewPager,也可以将ListView换成AbOuterListView。
(4)外边是slidingmenu 里面有图片联播 或者viewpager请为menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);属性。
第二种解决办法:
布局文件:goodsorder.xml
<pre name="code" class="html"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" > <ScrollView android:id="@+id/scrollview" android:scrollbars="none" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/sixline" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" > <TextView android:id="@+id/cardMessagetext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/cardMessage" android:textColor="#000000" android:textSize="14sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <ListView android:id="@+id/goodsList" android:fillViewport="true" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView> </RelativeLayout>ListView中Item的布局:goodsOrderList.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" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="14sp" /> <TextView android:id="@+id/ordersn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="14sp" /> <TextView android:id="@+id/orderamounttext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="14sp" android:paddingLeft="20dp" /> <TextView android:id="@+id/orderamount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="14sp" /> </LinearLayout>
最重要的是代码部分了,下面是主要的代码
public class GoodsOrderActivity extends Activity { /** Called when the activity is first created. */ private ListView goodsView; //商品简单信息显示 private ScrollView scrollView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.goodsorder); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.goodsOrderList, new String[]{"title","ordersn","orderamounttext","orderamount"}, new int[]{R.id.title,R.id.ordersn,R.id.orderamounttext,R.id.orderamount}); goodsView = (ListView)findViewById(R.id.goodsList); goodsView.setAdapter(adapter); setListViewHeight(goodsView);//设置listview的高度,在设置adapter之后设置高度 scrollView = (ScrollView)findViewById(R.id.scrollview); scrollView.post(runnable);//设置scrollView的显示位置 } /** * 设置Listview的高度 */ public void setListViewHeight(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } /** * 设置scrollview显示位置,解决scrollview嵌套listview页面过长时显示问题 */ private Runnable runnable = new Runnable() { @Override public void run() { GoodsOrderActivity.this.scrollView.scrollTo(1,1);// 改变滚动条的位置 } }; /** *获取listview数据 */ private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String,Object>> cartGoodsList = getCartGoods(); Map<String, Object> map = new HashMap<String, Object>(); map.put("orderamounttext", "数量:"); map.put("ordersn", "价格:"); map.put("title", "title"); map.put("orderamount","numberText"); list.add(map); return list; }
1、解决显示高度问题思路是:获取ListView中Item每一行的高度,然后算出总高度,最后给ListView设置一下高度,这样系统就知道了ListView的高度。
2、解决显示定位的问题,是设置ScrollView.scrollTo(),但是此方法在onCreate()方法中使用时,必须的另开一个线程,因为此方法,需要在ScrollView中的内容完全加载之后,才会调用,在onCreate()方法没结束之前,ScrollView中的内容是没有完全加载的。
所以需要另开一个线程。
- Android之 如何解决ScrollView 和ListView滑动冲突的问题如何解决ScrollView can host only one direct child
- ScrollView can host only one direct child 解决
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- scrollview can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- ScrollView can host only one direct child
- Android 控制台异常:ScrollView can host only one direct child
- android:ScrollView can host only one direct child
- android 异常:ScrollView can host only one direct child
- Android 控制台异常:ScrollView can host only one direct child .
- Android 控制台异常:ScrollView can host only one direct child
- Linux ALSA框架之四:Control设备的创建
- java中进制的转换,Byte与16进制的转换
- Linux ALSA框架之五:移动设备中的ALSA(ASoC)
- 回调函数理解的小例子
- Linux ALSA框架之六:ASoC架构中的Machine
- Android之 如何解决ScrollView 和ListView滑动冲突的问题如何解决ScrollView can host only one direct child
- Linux ALSA框架之七:ASoC架构中的Codec
- Linux ALSA框架之八:ASoC架构中的Platform
- 读安卓开发者文档笔记
- 自定义样式dialog及使用
- 光流法对运动目标跟踪Video
- Scalaz(38)- Free :Coproduct-Monadic语句组合
- webservice学习笔记
- float