带有Scrollview的Android滑动面板

时间:2014-03-14 02:01:46

标签: scrollview slidingpanelayout

我尝试使用链接here中的滑动面板,我正面对图片中显示的问题: enter image description here

我的想法是在用户向上滑动底部菜单时拥有自定义内容,并且可以滚动此内容。但是当面板进入锚定状态时,内容高度似乎没有正确设置,因为某些内容没有显示出来。

我在上面的链接中下载了示例,问题已经发生。我做了一个小改动,添加了scrollview

我错过了什么吗?任何帮助将不胜感激。

<RelativeLayout 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=".DemoActivity" >

<com.sothree.slidinguppanel.SlidingUpPanelLayout
    xmlns:sothree="http://schemas.android.com/apk/res-auto"
    android:id="@+id/sliding_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    sothree:collapsedHeight="68dp"
    sothree:dragView="@+id/name"
    sothree:shadowHeight="4dp" >

    <TextView
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:text="Main Content"
        android:textSize="16sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#eee"
        android:clickable="true"
        android:focusable="false"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:textSize="14sp" />

            <Button
                android:id="@+id/follow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical|right"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="14sp" />
        </LinearLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/slide_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/graphic" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="teste" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/graphic" />

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/graphic" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

出现此问题的原因是完全测量了滑动视图,然后将其部分地放在屏幕上。向上滑动视图时,其大小不会改变,只是它的位置。这意味着锚定时ScrollView不完全可见。

我想出的解决方案是使用PanelSlideListener事件来调整ScrollView的大小,因为滑动视图的位置会发生变化。只需为您的ScrollView提供“@ + id / scroll_view”ID,并使用AndroidSlidingUpPanel演示应用程序中以下onCreate()代码的修改部分。

final float anchorPoint = 0.5f;
final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
final SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
layout.setAnchorPoint(anchorPoint);
layout.setPanelSlideListener(new PanelSlideListener() {
    @Override
    public void onPanelSlide(View panel, float slideOffset) {
        Log.i(TAG, "onPanelSlide, offset " + slideOffset);
        setActionBarTranslation(layout.getCurrentParalaxOffset());
        resizeScrollView(panel, slideOffset);
    }

    @Override
    public void onPanelExpanded(View panel) {
        Log.i(TAG, "onPanelExpanded");
        resizeScrollView(panel, 0.0f);
    }

    @Override
    public void onPanelCollapsed(View panel) {
        Log.i(TAG, "onPanelCollapsed");
    }

    @Override
    public void onPanelAnchored(View panel) {
        Log.i(TAG, "onPanelAnchored");
        resizeScrollView(panel, anchorPoint);
    }

    private void resizeScrollView(View panel, float slideOffset) {
        // The scrollViewHeight calculation would need to change based on
        // what views you have in your sliding panel. The calculation below
        // works because your layout has 2 child views.
        // 1) The row with the drag view which is layout.getPanelHeight() high.
        // 2) The ScrollView.
        final int scrollViewHeight =
            (int) ((panel.getHeight() - layout.getPanelHeight()) * (1.0f - slideOffset));
        scrollView.setLayoutParams(
            new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                          scrollViewHeight));
    }
});