简体   繁体   English

使用ViewPage滑动页面

[英]Swiping the pages with ViewPage

I'm trying to fill the page by blocks of linearlayout by data. 我正在尝试按数据的linearlayout块填充页面。 Also I'm using ViewPager for changing pages. 我也使用ViewPager来更改页面。 Every page should have its own data blocks, but now I'm getting linearlayouts that doesn't swipes out (blocks stand still when I'm swiping pages) but there is another linearlayout right above the other ones that doesn't constist data but sliding when I swipe pages. 每个页面都应该有自己的数据块,但是现在我得到的线性布局不会滑动(当我滑动页面时,块会保持静止),但是在其他线性布局的上方,还有一个不构成数据但是滑动页面时滑动。

main.xml: main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/page"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </android.support.v4.view.ViewPager>
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/page_blocks"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        </LinearLayout>

</RelativeLayout>

table_row: table_row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/program_frame"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip">

    <TextView
        android:id="@+id/StudyName"
        />
    <TextView
        android:id="@+id/Auditorium"
        />
    <TextView
        android:id="@+id/StudyKindName"
        />
    <TextView
        android:id="@+id/LectureTitle"
        />
    <ImageView
        android:id="@+id/imageView2"
         />
</RelativeLayout>

onCreateView of ViewPage: ViewPage的onCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.table_row, null);
        framesContainer = (LinearLayout) getActivity().findViewById(R.id.page_blocks);
        for (int i = 0; i < 16; i += 4) {
            Frame frame = new Frame(getActivity());
            try {
                frame.setStudyName(s.get().get(0).Days().get(i));
                frame.setStudyKindName(s.get().get(0).Days().get(i + 1));
                frame.setAuditorium(s.get().get(0).Days().get(i + 2));
                frame.setLectureTitle(s.get().get(0).Days().get(i + 3));
                framesContainer.addView(frame);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return view;
    }

Also pic: 另外图片: 在此处输入图片说明

Is there a way to hide the empty block and make all blocks slide after the page when swiping? 有没有一种方法可以隐藏空白块并使所有块在滑动时在页面后滑动?

Your problem is that you are using RelativeLayouts, and since you are not aligning its children elements(ViewPager and LinearLayout), they are overlapping: 您的问题是您正在使用RelativeLayouts,并且由于未对齐其子元素(ViewPager和LinearLayout),因此它们是重叠的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/page"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/page_blocks"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/page" >
    </LinearLayout>

This line tells the viewpager that its position is going to be at the top of the RelativeLayout: 此行告诉viewpager其位置将在RelativeLayout的顶部:

android:layout_alignParentTop="true"

And this one tells the LinearLayout its position is below the viewpager: 这告诉LinearLayout其位置在viewpager下面:

android:layout_below="@id/page"

There are some other properties that can be set --> RelativeLayout 可以设置其他一些属性-> RelativeLayout

To be honest, I'm not sure what you are trying to do. 老实说,我不确定您要做什么。 But maybe you just need to get rid of the RelativeLayout and use LinearLayouts instead. 但是也许您只需要摆脱RelativeLayout,而使用LinearLayouts即可。

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

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