ViewPager 有多个可见的孩子和选择更大

ViewPager with multiple visible children and selected bigger(ViewPager 有多个可见的孩子和选择更大)

本文介绍了ViewPager 有多个可见的孩子和选择更大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Activity 中,我使用 Dave Smith 的 问题的公认答案,但正在实施该解决方案意味着我将使选择大于其当前尺寸(这已经是最大可能的).

所以我想在 PagerAdapter 中编辑 instantiateItem 以设置减小的宽度和高度,并在 onPageSelected 中使用问题.

问题是我不确定我应该调用哪种类型的 LayoutParams 并调用 getWidth()getHeight()instantiateItem 中返回 0.

目前我正在使用:

查看 v = container.getChildAt(position);PagerContainer.LayoutParams 参数 = (PagerContainer.LayoutParams)v.getLayoutParams();

但它有时会起作用,有时会抛出 NPE.

我的方法是正确的还是应该以其他方式进行?在这两种情况下,我应该改变什么来实现我想要的?

解决方案

你必须使用 PageTransform 用于此目的.您必须实现的回调接收两个参数.前者是 View 对象,后者是 int.当它的值为零时,表示视图对象指向中心的完全可见对象.-1 和 -1 分别是左侧和右侧的完全可见视图.您可以使用此值来实现所需的转换.例如,使用 ViewCompat,您可以更改 translateX、translationY 和 Scale 值

In my Activity I've added a Gallery-like View using Dave Smith's PagerContainer example and the code I'm using to instantiate it is very similar to the PagerActivity in his example, while I changed the layout because I needed to use layout weights:

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

    <com.example.myapp.PagerContainer
        android:id="@+id/pager_container"
        android:layout_width="match_parent"
        android:overScrollMode="never"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_height="0dp"
        android:layout_weight="0.66"
        >
        <android.support.v4.view.ViewPager
            android:layout_width="150dp"
            android:overScrollMode="never"
            android:layout_height="match_parent"
            android:layout_gravity="center" />
    </com.example.myapp.PagerContainer>

    <ImageView
        android:layout_width="150dp"
        android:layout_height="0dp"
        android:layout_weight="0.34"
        android:id="@+id/current_selection_logo"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

Now my ViewPager works properly and looks like this (I omitted the bottom ImageView)

But I'd like to resize the unselected items, aiming to emphasize the centered item (selection), something like:

or:

I've found the accepted answer to this question, but implementing that solution means that I'll make the selection bigger than its current dimensions (which already are the maximum possible).

So I was thinking to edit instantiateItem in the PagerAdapter  to set reduced width and height, and in onPageSelected use the approach shown in the question.

The problem is that I'm not sure about which kind of LayoutParams I should call and calling getWidth() and getHeight() in instantiateItem returns 0.

Currently I'm using:

View v = container.getChildAt(position);
PagerContainer.LayoutParams params = (PagerContainer.LayoutParams)v.getLayoutParams();

but it sometimes works and sometimes throws a NPE.

Is my approach correct or should I proceed in another way? In both cases, what should I change to achieve what I want?

解决方案

You have to use a PageTransform for this purpose. The callback you have to implement receives two parameters. The former is the View object, the latter is an int. When its value is zero, it means the view objects refers to the fully visible object to the center. -1 and -1, are the fully visible views to the left and right respectively. You can use this value to implement the transformation you want. Using ViewCompat, for instance, you can change the translationX, translationY and Scale values

这篇关于ViewPager 有多个可见的孩子和选择更大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ViewPager 有多个可见的孩子和选择更大

基础教程推荐