【发布时间】:2014-12-13 18:34:08
【问题描述】:
在我的 Activity 中,我使用 Dave Smith 的 PagerContainer 示例添加了一个 Gallery 类似 View,我用来实例化它的代码与他的示例中的 PagerActivity 非常相似,虽然我更改了布局,因为我需要使用布局权重:
<?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)
但我想调整未选中项目的大小,旨在强调居中的项目(选择),例如:
或:
我已找到this 问题的公认答案,但实施该解决方案意味着我将使选择大于其当前尺寸(这已经是可能的最大值)。
所以我想在PagerAdapter 中编辑instantiateItem 以设置减小的宽度和高度,并在onPageSelected 中使用问题中显示的方法。
问题是我不确定我应该调用哪种LayoutParams,然后在instantiateItem 中调用getWidth() 和getHeight() 返回0。
目前我正在使用:
View v = container.getChildAt(position);
PagerContainer.LayoutParams params = (PagerContainer.LayoutParams)v.getLayoutParams();
但它有时会起作用,有时会抛出 NPE。
我的方法是正确的还是应该以其他方式进行? 在这两种情况下,我应该改变什么来实现我想要的?
【问题讨论】: