这不是最佳做法,但如果您真的想要该功能,您可以在触摸父滚动视图后禁止它的触摸事件,并在离开子容器后重新允许它。
这是我之前的做法,“listScanners”是我的列表视图:
listScanners.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
这是我的布局中与问题相关的部分:
/* Theres more before this ... */
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="0dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:gravity="center"
android:background="@null"
>
<TextView
android:id="@+id/emptylist1"
android:layout_width="450dp"
android:layout_height="80dp"
android:textColor="#6f6f6f"
android:textSize="20sp"
android:padding="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="No Scanners have been added..."/>
<ListView
android:id="@+id/listview_scanners"
android:layout_width="450dp"
android:layout_height="80dp"
android:padding="3dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
/* Theres more after this ... */
正确方法:
理想情况下,你应该有这样的东西,你的布局看起来像这样:
在线性布局中非常重要的地方:
android:isScrollContainer="true"
isScrollContainer 意味着线性布局包含一个可以滚动的视图,这意味着它可以在线性布局中具有一定的大小,但是,当您滚动它时,它可能包含更多。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#afafaf"
android:isScrollContainer="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="@+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View First List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="@null"
>
<ListView
android:id="@+id/listview1"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="@+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View Second List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="@null"
>
<ListView
android:id="@+id/listview2"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="@+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View Third List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="@null"
>
<ListView
android:id="@+id/listview3"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
</LinearLayout>
我刚刚在 5 分钟内完成了这个,你得到的布局如下所示:(这只是一个草稿,显然只是为了给你一个想法)
最后但并非最不重要的一点是,在显示列表的代码中,您将拥有如下内容:
public class Screen_View_Lists extends Activity
{
BaseAdapter listAdapter;
ListView list1,list2,list3;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.screen_view_packages);
list1 = (ListView) findViewById(R.id.listview1);
list2 = (ListView) findViewById(R.id.listview2);
list2 = (ListView) findViewById(R.id.listview3);
listAdapter = new Adapter_List_Main(this, packages);//This is my own adapter, you probably use your own custom one as well.
list1.setAdapter(listAdapter);
//Setup list to support context menu
registerForContextMenu(list1);
//Setup list to support long click events.
list1.setLongClickable(true);
//Action Listener for long click on item in the list
list1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
//do things here
}
});
//Action Listener for short click on item in the list
list1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//do things here
}
});
//And so one . . .
}
}