簡體   English   中英

viewPager中帶有nestedScrollView的奇怪行為

[英]Strange behavior with nestedScrollView inside a viewPager

我在viewPager中有3個片段,每個片段都使用nestedScrollView。 每次我切換一次時,nestedScrollView都會滾動到appBar下方。 我認為在InfoFragment的生命周期中設置某些視圖可能會出現問題。 我將其張貼在下面。 在此處輸入圖片說明

信息片段的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>

InfoFragment。 當每個onStart()添加到演示者的子視圖時,就會調用UpdateView()。

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()
}

}

我的猜測是,在適配器中加載或更新數據時,recycleview請求重點關注。 作為解決方案,請嘗試添加以下android:descendantFocusability="blocksDescendants"yourRecycleView.setDescendantFocusability(RecyclerView.FOCUS_BLOCK_DESCENDANTS);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM