Android ViewPager 中不同宽度的布局

发布于 2024-12-20 18:12:26 字数 2747 浏览 2 评论 0原文

我想使用水平视图寻呼机创建一些滚动视图。左视图必须具有全屏宽度,但右视图只有四分之一宽度(它将是像 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);
    }
 } 

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

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

<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

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

I want to create some scroll view using Horizontal 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). It's possible to do that? I changed android:layout_width in right layout, but it didn't work.

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

<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

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

<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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是偏爱你 2024-12-27 18:12:26

查看墨菲对此问题的回答。您需要在 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);
}

Check Murphy's answer on this question. You need to override PagerAdapter's getPageWidth() method on your PagerAdapter class, like this for example:

@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);
}
小清晰的声音 2024-12-27 18:12:26

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

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

Looking at the source for ViewPager, this isn't something it's designed to do; 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.

For your specific case there may be a hacky workaround, though. 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. Give it a try, and see whether it does what you need.

默嘫て 2024-12-27 18:12:26

阿德里安是完全正确的。 ViewPager 的设计目的并不是将下一篇文章的一部分显示为预览/预告片,但它可以做到这一点。在我的 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.
}

遇到奇怪的 z 重叠问题。诀窍是要么仅将背景应用于 ViewPager 的背景,要么将其应用于刚刚为其设置内边距的 newView 内的视图。看起来不错而且效果很好。

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. In my ViewPagerCursorAdapter extends PagerAdapter class I run this:

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. 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. Looks good and works great.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文