列表视图不在android prelollipop版本中滚动

时间:2016-02-24 13:16:50

标签: android

配置文件页面包含折叠布局,选项卡布局和视图寻呼机片段。视图寻呼机内的列表视图滚动。如果我们滚动片段外的列表视图,它不会在Pre-lollipop版本中滚动,但如果我们回收,则recyclerview工作正常滚动布局的任何位置。

这是我的代码

<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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="?android:attr/colorBackground"
        tools:context="com.cogzidel.hotfuse.PostFragment">
        <!--<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"/>

        <ImageButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:id="@+id/button_back"
            android:src="@drawable/ic_back_white"
            android:background="@null"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignBottom="@+id/toolbar" />
    -->

        <ListView
            android:id="@+id/recycler_view"

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/toolbar" />

        <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:visibility="gone"
            android:text="No Posts for you" />




    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

yout TextView覆盖了父级的完整可见区域,因为它的高度为

android:layout_height="match_parent"

将其更改为

android:layout_height="wrap_content"或任何适当的值 然后触摸/滚动事件将传递到listView。

然后你就可以滚动了。

答案 1 :(得分:0)

请尝试使用此代替ListView。

package com.github.takahirom.nested_listview;


import android.content.Context;

import android.support.v4.view.MotionEventCompat;

import android.support.v4.view.NestedScrollingChild;

import android.support.v4.view.NestedScrollingChildHelper;

import android.support.v4.view.ViewCompat;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.widget.ListView;


public class NestedListView extends ListView implements NestedScrollingChild {

    private int mLastY;

    private final int[] mScrollOffset = new int[2];

    private final int[] mScrollConsumed = new int[2];

    private int mNestedOffsetY;

    private NestedScrollingChildHelper mChildHelper;


    public NestedListView(Context context) {

        this(context, null);

    }


    public NestedListView(Context context, AttributeSet attrs) {

        this(context, attrs, 0);

    }


    public NestedListView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        mChildHelper = new NestedScrollingChildHelper(this);

        setNestedScrollingEnabled(true);

    }


    /**

*

* @see android.support.v4.widget.NestedScrollView#onTouchEvent(MotionEvent)

*/

    @Override

    public boolean onTouchEvent(MotionEvent ev) {

        boolean returnValue = false;


        MotionEvent event = MotionEvent.obtain(ev);

        final int action = MotionEventCompat.getActionMasked(event);

        if (action == MotionEvent.ACTION_DOWN) {

            mNestedOffsetY = 0;

        }

        event.offsetLocation(0, mNestedOffsetY);

        int eventY = (int) event.getY();

        switch (action) {

            case MotionEvent.ACTION_MOVE:

                int deltaY = mLastY - eventY;

                // NestedPreScroll

                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {

                    deltaY -= mScrollConsumed[1];

                    mLastY = eventY - mScrollOffset[1];

                    event.offsetLocation(0, mScrollOffset[1]);

                    mNestedOffsetY += mScrollOffset[1];

                }

                returnValue = super.onTouchEvent(event);


                // NestedScroll

                if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {

                    event.offsetLocation(0, mScrollOffset[1]);

                    mNestedOffsetY += mScrollOffset[1];

                    mLastY -= mScrollOffset[1];

                }

                break;

            case MotionEvent.ACTION_DOWN:

                returnValue = super.onTouchEvent(event);

                mLastY = eventY;

                // start NestedScroll

                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);

                break;

            case MotionEvent.ACTION_UP:

                // TODO: fling

                returnValue = super.onTouchEvent(event);

                break;

            case MotionEvent.ACTION_CANCEL:

                returnValue = super.onTouchEvent(event);

                // end NestedScroll

                stopNestedScroll();

                break;

        }

        return returnValue;

    }



    // NestedScrollingChild

    @Override

    public void setNestedScrollingEnabled(boolean enabled) {

        mChildHelper.setNestedScrollingEnabled(enabled);

    }


    @Override

    public boolean isNestedScrollingEnabled() {

        return mChildHelper.isNestedScrollingEnabled();

    }


    @Override

    public boolean startNestedScroll(int axes) {

        return mChildHelper.startNestedScroll(axes);

    }


    @Override

    public void stopNestedScroll() {

        mChildHelper.stopNestedScroll();

    }


    @Override

    public boolean hasNestedScrollingParent() {

        return mChildHelper.hasNestedScrollingParent();

    }


    @Override

    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed,

                                        int[] offsetInWindow) {

        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);

    }


    @Override

    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {

        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);

    }


    @Override

    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {

        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);

    }


    @Override

    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {

        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);

    }


}

示例项目就在这里。 https://github.com/takahirom/NestedListView/blob/master/nested-listview/src/main/java/com/github/takahirom/nested_listview/NestedListView.java