Android中不同宽度的布局在ViewPager中

use*_*084 6 android width android-viewpager

我想使用Horizo​​ntal View Pager创建一些滚动视图.左视图必须具有全屏宽度,但只有四分之一宽度(它将是Dolphin浏览器中的垂直面板).有可能这样做吗?我改变android:layout_width了正确的布局,但它没有用.

我的代码:

 public class TestActivity extends FragmentActivity {

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_view);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager pager = (ViewPager) findViewById(R.id.panelPager);
    pager.setAdapter(adapter);
    pager.setCurrentItem(0);
    }
 } 
Run Code Online (Sandbox Code Playgroud)

main_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/panelPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MyPagerAdapter.java

public class MyPagerAdapter extends PagerAdapter {

@Override
public int getCount() {
    return 2;
}

@Override
public Object instantiateItem(final View collection, final int position) {

    LayoutInflater inflater =
            (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch (position) {
    case 0:
        resId = R.layout.left;
        break;
    case 1:
        resId = R.layout.right;
        break;
    }

    View view = inflater.inflate(resId, null);
    ((ViewPager) collection).addView(view, 0);

    return view;
}

@Override
public void destroyItem(final View arg0, final int arg1, final Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public boolean isViewFromObject(final View arg0, final Object arg1) {
    return arg0 == ((View) arg1);

}
Run Code Online (Sandbox Code Playgroud)

left.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="LEFT" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

right.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/light_blue" >


<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="RIGHT"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

SE *_*lio 9

检查墨菲对这个问题的回答.您需要getPageWidth()在PagerAdapter类上覆盖PagerAdapter的方法,例如:

@Override
public float getPageWidth(int page) {
    if(page==0) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return (float)LEFT_FRAGMENT_PIXEL_WIDTH / size.x;
    }
    else
        return super.getPageWidth(page);
}
Run Code Online (Sandbox Code Playgroud)


Adr*_*ian 2

查看 ViewPager 的源代码,这不是它设计的目的;它使用自己的宽度来计算滚动距离等,并明确假设所有子项都将具有与 ViewPager 本身相同的宽度。

不过,对于您的具体情况,可能有一个棘手的解决方法。您可以指定相邻页面之间的边距,并且该边距可以为负值。如果 ViewPager 子级的 Z 顺序合适,这可能会给出您想要的结果。尝试一下,看看它是否满足您的需要。