3

RecyclerView高さが に設定されていますwrap_content。今、私はそれに実装OnLoadMoreする必要がありますが、問題があります。私が使用した、

RecyclerView.OnScrollListener.onScrolled(RecyclerView recyclerView, int dx, int dy)

RecyclerViewしかし、スクロールしないため、呼び出されません。その高さはwrap_contentです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_rtl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tool_bar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="com.yarima.msn.Activities.ProfileActivity">

            <FrameLayout
                 android:id="@+id/FRAGMENT_PLACEHOLDER"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:background="@color/white">

                 <!-- Some Content That I want to scroll with recyclerview -->
            </FrameLayout>

            <android.support.v7.widget.RecyclerView
                 android:id="@+id/recyclerview_posts"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:scrollbars="vertical"
                 android:bellow="@id/FRAGMENT_PLACEHOLDER"/>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

そのため、より多くのページを にロードするには、別のアプローチを使用する必要がありますRecyclerViewonLoadMoreこれを行う最善の方法は、最後のアイテムが表示されたときにイベントを呼び出すことだと思いRecyclerViewます。アダプターのメソッドからこれを実行しようとしましonBindViewHolderたが、すべてのページが完全に読み込まれました。

if(getItemCount()-position == 1 && onLoadMoreListener != null){
    if (recyclerView != null) {
        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = recyclerView.getLayoutManager().getItemCount();
        firstVisibleItem = ((LinearLayoutManager)recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            loading = true;
            onLoadMoreListener.onLoadMore();
        }
    }
}

onLoadMoreスクロールイベントを使わずに実装する別の方法は何ですか?

アップデート:

私のスクロールはスムーズRecyclerViewに動作します。android:layout_height:"wrap_content"ScrollView

更新 2:

私の問題は、RecyclerView身長がの場合wrap_content、 のスクロール イベントを呼び出せRecyclerViewないことです。そのため、リストの最後にいつRecyclerView到達したかを確認し、そのOnLoadMore方法でイベントを実装する別の方法が必要です。

アップデート 3

問題に書く前にxmlを単純化しました...実際のxmlでは、ViewPagerの代わりにRecyclerView. そしてViewPager、各タブにはRecyclerView異なる内容の が含まれているという 4 つのタブがあります。

この上に、ViewPagerユーザーに関する情報がいくつかあり、それらすべてを一緒にスクロールしたいと考えています。そこで、このヘッダーとViewPagerを aに入れScrollView、の高さを に設定RecyclerViewwrap_contentます。

インスタグラムのプロフィールページからご覧いただけます。私はこのページがそのように機能することを望んでいます。

この情報をヘッダーに表示することはできません。この方法では、この情報をすべてのタブRecyclerViewのそれぞれに追加する必要があるためです。RecyclerView

4

2 に答える 2

0

編集済み

scrollviewでスクロールイベントを探すことができます

https://developer.android.com/reference/android/view/View.html#onScrollChanged(int , int, int, int)

于 2016-12-12T11:21:35.843 に答える
0

あなたViewPagerとあなたのRecyclerView内部を維持する必要がありますNestedScrollView。最終的な xml は次のようになります。

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewPager/>
    <RecyclerView/>

</android.support.v4.widget.NestedScrollView>

の高さを に設定しRecyclerViewますmatch_parent

ここで別の問題に直面します。がロードされると、ページは自動的に一番下までスクロールしますRecyclerView。しかし、それにも解決策があります。

下への自動スクロールを防ぐ子android:descendantFocusability="blocksDescendants"レイアウトに追加します。NestedScrollView

上記が機能しない場合は、にアイテムがロードされnestedScrollView.scrollTo(0, 0);た後にコードから設定してみてください。RecyclerView

于 2016-12-15T15:26:21.950 に答える