ViewPager与多发性可见儿童和选择更大。 [英] ViewPager with mutiple visible children and selected bigger.

查看:148
本文介绍了ViewPager与多发性可见儿童和选择更大。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我添加了一个图库查看使用戴维·史密斯的 PagerContainer 例如,我使用初始化它code是在他的例子非常相似 PagerActivity ,虽然我改变了布局,因为我需要使用布局权重:

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>

现在我的 ViewPager 工作正常,看起来像这样(我省略了底部的ImageView

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:

我已经找到了接受的答案为<一个href=\"http://stackoverflow.com/questions/17437343/android-viewpager-with-center-item-bigger\">this的问题,但实施该解决方案意味着我会选择比目前尺寸(这已经是最大可能的)更大。

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).

所以我想修改 instantiateItem PagerAdapter 设置减少宽度和高度,并在使用onPageSelected 使用问题所示的方法。

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.

问题是我不知道哪一种的LayoutParams 我应该打电话叫的getWidth()的getHeight() instantiateItem 收益 0

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.

目前我使用的是:

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

但有时工作,有时抛出一个NPE。

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?

推荐答案

您必须使用的 PageTransform 用于这一目的。您必须实现回调接收两个参数。前者是视图对象,后者是一个 INT 。当它的值为零,则意味着该查看对象指的是完全可见对象到中心。 -1和-1,是向左的完全可见视图和右分别。你可以使用这个值来实现所需的转型。使用ViewCompat,例如,你可以改变translationX,translationY和刻度值

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与多发性可见儿童和选择更大。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆