我正在尝试在Scrollview中有两个列表布局。我正在为这两个列表布局使用嵌套滚动视图。 Scrollview适用于第二个列表布局,但滚动视图不适用于第一个列表布局。父级Scrollview也正常工作。第一个列表视图的问题。第一个列表视图的滚动视图不起作用。我在哪里做错了
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp"
android:background="@android:color/white"
android:padding="10dp">
<LinearLayout
android:id="@+id/slistLayout1"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
>
<ListView
android:id="@+id/pListView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset"
android:clickable="true"
android:focusable="true" >
</ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp"
android:background="@android:color/white"
android:padding="10dp">
<LinearLayout
android:id="@+id/slistLayout2"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
>
<ListView
android:id="@+id/pListView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset"
android:clickable="true"
android:focusable="true" >
</ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/back"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/footer"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/cross"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/dcr_online"/>
<Button
android:id="@+id/backbt"
android:layout_width="50sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/add_button_selector"
android:layout_marginBottom="5dp"
android:gravity="center"
android:hapticFeedbackEnabled="true"
android:onClick="finishActivity"
android:soundEffectsEnabled="true"
android:text="<"
android:textColor="#FFF"
android:textSize="25sp"
/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:1)
请尝试这个。希望您能有用。
请使用自定义ListView。
自定义ListView
public class MyCustomListView extends ListView {
public MyCustomListView(Context context) {
super(context);
}
public MyCustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.FILL_PARENT) {
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
} else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/slistLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<your_packagename.MyCustomListView
android:id="@+id/pListView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:focusable="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset" />
<your_packagename.MyCustomListView
android:id="@+id/pListView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:fadeScrollbars="false"
android:focusable="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="insideInset" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/back"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/footer"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/cross"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/dcr_online"/>
<Button
android:id="@+id/backbt"
android:layout_width="50sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/add_button_selector"
android:layout_marginBottom="5dp"
android:gravity="center"
android:hapticFeedbackEnabled="true"
android:onClick="finishActivity"
android:soundEffectsEnabled="true"
android:text="<"
android:textColor="#FFF"
android:textSize="25sp"
/>
</LinearLayout>
</ScrollView>