2017-11-27 9 views
0

コンテンツ私はは右ページと左ではないスワイプ/行いますが、(少なくとも、離れたり、)スクロールアップがないこと、ViewPagerのタブの下に座っているViewPager.DecorViewを取得しようとしていますのフラグメントのListViewがスクロールアップされます。しかし、私はDecorViewを内容にスクロールさせる方法を見つけるのに苦労しています。ViewPager 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

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

.ktだけでX軸のいずれかの動作を設定するサンプル左/右スワイプを防止する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> 

答えて

0

ビルドカスタムViewPagerは、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)) 
    } 

} 
+0

私は 'ViewPager'がページを垂直方向に切り替えることを望んでいません。 'ViewPager'が通常どおりに行うことを水平にしたいのですが、フラグメントの' ListView'をスクロールして固定ビューを隠すようにしたいのです。 –

+0

フラグメントA B Cの変更のために左にスワイプしたくないということですか? – slee

+0

左/右の切片をスワイプしたいのですが、 "ヘッダ"をフラグメントの内容でスクロールしてもらいたいです。 (ただし、左右にスワイプするときに "ヘッダー"を移動しない) –

関連する問題