簡體   English   中英

有沒有一種方法可以使ViewPager DecorView的片段內容滾動?

[英]Is there a way to make ViewPager DecorView's scroll with the fragment content?

我正在嘗試獲取一個ViewPager.DecorView ,它位於ViewPager.DecorView的選項卡下,它隨頁面向左/向右滑動,但是在片段的ListView的內容時確實向上滾動(或至少ViewPager滾動)向上滾動。 但是,我正在努力尋找使DecorView隨內容DecorView滾動的任何方法。

我是否需要將滾動狀態傳達給Activity ,然后將其傳遞給ViewPager以將DecorView滾動走? 感覺好像我在那兒缺少什么。

我也嘗試過使用CoordinatorLayout ,但是似乎無法在Activity / Fragment正常工作

這是當前的來源: https : //github.com/fifersheep/android-sample-fixed-banner

但是為了不依賴鏈接,這是我目前所擁有內容的簡化版本:

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val pager = findViewById<ViewPager>(R.id.pager)
        pager.adapter = MainActivityPagerAdapter(supportFragmentManager)

       val tabLayout = findViewById<TabLayout>(R.id.tab_layout)
        tabLayout.setupWithViewPager(pager)
    }

    private inner class MainActivityPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
        val titles = listOf("Fragment A", "Fragment B", "Fragment C")

        override fun getItem(position: Int): Fragment = CustomFragment.instance(titles[position])

        override fun getPageTitle(position: Int): CharSequence = titles[position]

        override fun getCount(): Int = titles.size
    }
}

activity_main.xml

<LinearLayout
    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:orientation="vertical"
    tools:context="uk.lobsterdoodle.sample.fixedscrollingtablayoutbanner.MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="#fff"
        app:tabIndicatorHeight="2dp"
        app:tabMode="fixed"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabSelectedTextColor="#fff"
        app:tabTextColor="#6fff"
        android:background="@color/colorPrimaryDark" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <uk.lobsterdoodle.sample.fixedscrollingtablayoutbanner.BannerView
            android:id="@+id/static_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"/>

    </android.support.v4.view.ViewPager>
</LinearLayout>

CustomFragment.kt

class CustomFragment : Fragment() {
        private lateinit var adapter: ArrayAdapter<String>

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val view = inflater.inflate(R.layout.fragment_sample, container, false)
            adapter = ArrayAdapter(context, R.layout.my_list_item, (1..20).map { "${arguments.get("header")}: Item #$it" })
            view.findViewById<ListView>(R.id.list_view).adapter = adapter
            return view
        }

        companion object {
            fun instance(header: String): Fragment {
                val frag = CustomFragment()
                val args = Bundle()
                args.putString("header", header)
                frag.arguments = args
                return frag
            }
        }
    }

fragment_sample.xml

<android.support.constraint.ConstraintLayout
                 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">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:listitem="@layout/my_list_item"/>

</android.support.constraint.ConstraintLayout>

BannerView.kt

@ViewPager.DecorView
class BannerView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
    init {
        inflate(context, R.layout.view_banner, this)
    }
}

my_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#eee">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Simplification of banner layout"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#ddd" />
</LinearLayout>

構建自定義ViewPager以防止向左/向右滑動,只需將X軸的任何運動設置為0即可:

class CustomViewPager : ViewPager {


    constructor(context: Context) : super(context) {

    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {

    }






    private fun transferXY(ev: MotionEvent): MotionEvent {
        //Prevent X movement


              val height = height.toFloat()



             ev.setLocation(0f, ev.y / width * height)

        return ev
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val intercepted = super.onInterceptTouchEvent(transferXY(ev))
        transferXY(ev)
        return intercepted
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        return super.onTouchEvent(transferXY(ev))
    }

}

暫無
暫無

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

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