Я пытаюсь получить ViewPager.DecorView
который находится под вкладками ViewPager
, который не ViewPager
влево / вправо страницы, но выполняет прокрутку вверх (или, по крайней мере, в сторону), когда содержимое ListView
прокручивается вверх. Тем не менее, я изо всех сил пытаюсь найти любой способ сделать прокрутку 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>
1<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)) } }