0

我在 viewPager 中有 3 个片段,每个片段都使用一个nestedScrollView。每次我在其中一个之间切换时,nestedScrollView 都会在 appBar 下方滚动。我认为这可能是我在 InfoFragment 的生命周期中设置某些视图的问题。我会在下面贴出来。在此处输入图像描述

Info 片段的 XML:

 <android.support.v4.widget.NestedScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/scroll_view"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="72dp"
 android:clipToPadding="false"
 >

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:ignore="RtlSymmetry"
     tools:targetApi="N">

     <TextView
         android:id="@+id/tagline"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="16dp"
         android:padding="16dp"
         android:gravity="center"
         android:textSize="18sp"
         android:textStyle="italic|bold"
         android:fontFamily="sans-serif-condensed"
         tools:text="Good vs Evil" />

     <TextView
         android:id="@+id/overview"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="14dp"
         android:gravity="center"
         android:textSize="18sp"
         tools:text="@string/dummy_summary"
         />

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="16dp"
         android:textSize="20sp"
         android:textStyle="bold"
         android:text="@string/cast"/>

     <android.support.v7.widget.RecyclerView
         android:id="@+id/cast_list"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_margin="16dp"
         android:orientation="vertical"
         android:clipToPadding="false"
         android:nestedScrollingEnabled="false"
         app:layoutManager="LinearLayoutManager"
         tools:listitem="@layout/card_actor"
         tools:layout_height="200dp"/>

     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="16dp"
         android:textSize="20sp"
         android:textStyle="bold"
         android:text="@string/where_to_watch"/>

     <android.support.v7.widget.RecyclerView
         android:id="@+id/source_list"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_margin="16dp"
         android:orientation="vertical"
         android:clipToPadding="false"
         android:nestedScrollingEnabled="false"
         app:layoutManager="LinearLayoutManager"
         tools:listitem="@layout/card_source" />
 </LinearLayout>

包含 viewPager 的片段中的 XML:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:targetApi="lollipop">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:ignore="RtlSymmetry"/>

<android.support.v4.widget.ContentLoadingProgressBar
    android:id="@+id/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    style="?android:progressBarStyle"
    tools:targetApi="lollipop" />

<RelativeLayout
    android:id="@+id/error"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <TextView
        android:id="@+id/error_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:padding="32dp"
        />

    <Button
        android:id="@+id/error_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/error_message"
        android:layout_centerHorizontal="true"
        android:text="@string/recycle"/>
</RelativeLayout>

信息片段。UpdateView() 在将自身添加到演示者的子视图时在每个 onStart() 中调用。

class MovieInfoFragment() : BaseFragment(), MovieContract.Subview {
// Parameters
override val layoutId: Int = R.layout.fragment_movie_info

// Objects
@Inject lateinit var presenter: MovieContract.Presenter
@Inject lateinit var creditsAdapter: CreditsAdapter
@Inject lateinit var sourceAdapter: SourceAdapter

// Views
val overview: TextView get() = find(R.id.overview)
val tagline: TextView get() = find(R.id.tagline)
val castList: RecyclerView get() = find(R.id.cast_list)
val sourceList: RecyclerView get() = find(R.id.source_list)

fun updateView() {
    if (view == null || activity == null) return
    presenter.getMovie()?.let {
        overview.text = "   ${it.overview}"
        tagline.text = it.tagline
        tagline.visibleIf(!it.tagline.isNullOrBlank())
        creditsAdapter.credits = it.credits
        sourceAdapter.setNewItems(it.sources)
    }
}

override fun updateMovie(movie: Movie) {
    updateView()
}

override fun updateColor(color: Int) {

}

override fun scrollToTop() {

}

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    castList.adapter = creditsAdapter
    sourceList.adapter = sourceAdapter
    sourceAdapter.listener = { presenter.onSourceClicked(it) }
}

override fun onStart() {
    super.onStart()
    presenter.addSubview(this)
}

override fun onStop() {
    presenter.removeSubview(this)
    super.onStop()
}

}

4

1 回答 1

0

我的猜测是,在适配器中加载或更新数据时,recycleview 请求焦点。作为解决方案,请尝试添加以下内容 android:descendantFocusability="blocksDescendants"yourRecycleView.setDescendantFocusability(RecyclerView.FOCUS_BLOCK_DESCENDANTS);

于 2017-03-08T10:34:30.007 回答