具有自定义宽度的Viewpager行为错误

时间:2018-11-14 12:19:06

标签: android kotlin android-viewpager

我有一个自定义宽度(override fun getPageWidth(...))的viewpager,其中包含不同的片段。当片段不足以填满窗口宽度时,它就会发疯,将整个viewpager移动到屏幕的两边,就像您在此gif中所看到的那样。

当有足够的物品(碎片)时,不会发生此行为。

这是我的home_new_applications.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

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

</LinearLayout>

这是fragment_application_fav.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/itemFavAppLayout"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:background="@color/transparent"
android:orientation="vertical">


<android.support.v7.widget.CardView
    android:id="@+id/itemFavCardLayout"
    android:layout_width="100dp"
    android:layout_height="160dp"
    android:background="@color/colorWhite"
    app:cardCornerRadius="4dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="@dimen/card_image_margin"
        android:paddingRight="@dimen/card_image_margin"
        android:paddingTop="@dimen/card_image_margin">

        <cat.gencat.mobi.gencatapp.presentation.component.GencatImageView
            android:id="@+id/itemFavAppImage"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/itemFavAppText"
            style="@style/FavCardText"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/itemFavAppImage"
            android:layout_marginRight="15dp"
            android:layout_marginTop="6dp"
            android:ellipsize="end"
            android:maxLines="2" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="6dp"
            android:layout_below="@id/itemFavAppImage"
            android:layout_marginTop="6dp">

            <TextView
                android:id="@+id/itemFavAppInstall"
                style="@style/FavCardInstallText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="@string/home_card_install" />

            <ImageButton
                android:id="@+id/itemFavAppIB"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="-6dp"
                android:background="@color/transparent"
                android:foreground="?attr/selectableItemBackground"
                android:src="@mipmap/ic_more_vert_black_24dp" />
        </RelativeLayout>

    </RelativeLayout>
</android.support.v7.widget.CardView>

<include layout="@layout/item_add_application" />

这是我的holder

class HomeNewApplicationsHolder(var activity: Activity, var view : View, val listener: HomeAdapter.HomeListener, var supportFM : FragmentManager?) :  GeneralViewHolder<HomeApplicationsNewItem>(view) {
val viewPager : ViewPager = view.findViewById<ViewPager>(R.id.homeNewAppsVP)

companion object {
    fun createViewHolder(activity: Activity, parent: ViewGroup?, listener: HomeAdapter.HomeListener, supportFM: FragmentManager?): GeneralViewHolder<HomeApplicationsNewItem> {
        return HomeNewApplicationsHolder(activity, LayoutInflater.from(parent?.context)
                .inflate(R.layout.home_new_applications, parent, false), listener, supportFM)
    }
}

override fun decorate(item: HomeApplicationsNewItem, position: Int) {
    var adapter = NewApplicationsPagerAdapter(activity, supportFM, item.applications)
    viewPager.adapter = adapter
    val pagerPadding = 10
    viewPager.pageMargin = 10
    viewPager.clipToPadding = false
    viewPager.setPadding(pagerPadding, 0, pagerPadding, 0)
    }
}

这里是PagerAdapter

class NewApplicationsPagerAdapter(var activity: Activity, fragmentManager: FragmentManager?, var applications : List<ApplicationGencat>) : FragmentStatePagerAdapter(fragmentManager) {

override fun getItem(position: Int): Fragment {
    return NewApplicationFragment.newInstance(applications[position])
}

override fun getCount(): Int {
    return applications.size
}

override fun getPageWidth(position: Int): Float {
    var result = 0f
    val containerWidth = activity.applicationContext.resources.getDimension(R.dimen.new_apps_container_inner_width) + activity.applicationContext.resources.getDimension(R.dimen.new_apps_container_outer_padding) * 2
    val size : Point = Point()
    activity.windowManager.defaultDisplay.getSize(size)
    result = containerWidth/size.x

    return if (position == 0 && isEmpty()) {
        1f - result
    } else result
}

private fun isEmpty() : Boolean {
    var result = false
    if (applications.isEmpty()) result = true
    return result
    }
}

关于如何解决此问题的任何想法? 谢谢!

0 个答案:

没有答案