简体   繁体   English

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

[英]Android different widths of layouts in ViewPager

I want to create some scroll view using Horizontal View Pager. 我想使用Horizo​​ntal View Pager创建一些滚动视图。 Left view must has full screen width, but right only a quarter of width (it will be a vertical panel like in Dolphin browser). 左视图必须具有全屏宽度,但只有四分之一宽度(它将是Dolphin浏览器中的垂直面板)。 It's possible to do that? 有可能这样做吗? I changed android:layout_width in right layout, but it didn't work. 我在正确的布局中改变了android:layout_width ,但它没有用。

My code: 我的代码:

 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);
    }
 } 

main_view.xml 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>

MyPagerAdapter.java 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);

}

left.xml 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>

right.xml 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>

Check Murphy's answer on this question . 检查墨菲对这个问题的回答。 You need to override PagerAdapter's getPageWidth() method on your PagerAdapter class, like this for example: 您需要在PagerAdapter类上覆盖PagerAdapter的getPageWidth()方法,例如:

@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);
}

Adrian is exactly right. 阿德里安完全正确。 ViewPager isn't designed to show a portion of the next post as a preview/teaser but it can do it. ViewPager并非旨在将下一篇文章的一部分显示为预览/预告片,但它可以执行此操作。 In my ViewPagerCursorAdapter extends PagerAdapter class I run this: 在我的ViewPagerCursorAdapter extends PagerAdapter类,我运行这个:

public Object instantiateItem(View collection, final int position) {
    cursor.moveToPosition(position);
    View newView = getPageView(cursor, collection);
    if (cursor.getCount() > 1) {
        ((ViewPager)collection).setPageMargin(-overlapMargin);
        if (! cursor.isLast()) { newView.setPadding(0, 0, overlapMargin, 0); }
    }
    ((ViewPager) collection).addView(newView);
    return newView; //returns the object reference as the tag for identification.
}

You will run into a strange z overlapping issue. 遇到一个奇怪的z重叠问题。 The trick is to either apply the background only to ViewPager's background or to apply it to the view inside the newView you just set the padding for. 诀窍是后台或者只适用于ViewPager的背景或将其应用到NewView的里面 ,你刚才设置的衬垫的视图。 Looks good and works great. 看起来很好,效果很好。

Looking at the source for ViewPager, this isn't something it's designed to do; 查看ViewPager的源代码,这不是它的设计目的; it uses its own width to calculate scroll distances etc, with the clear assumption that all children will have the same width as the ViewPager itself. 它使用自己的宽度来计算滚动距离等,明确假设所有孩子都具有与ViewPager本身相同的宽度。

For your specific case there may be a hacky workaround, though. 但是,对于您的具体情况, 可能会有一个hacky解决方法。 You can specify a margin between adjacent pages, and this margin can be negative. 您可以指定相邻页面之间的边距,此边距可以为负。 This may give the result you want, provided the Z-ordering of the ViewPager's children is appropriate. 如果ViewPager的子项的Z顺序合适,这可能会给出您想要的结果。 Give it a try, and see whether it does what you need. 试一试,看看它是否能满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM