Android - PageViewer视图卡在屏幕上

时间:2016-10-04 09:17:07

标签: android view fragment swipe viewer

我试图让这个页面浏览器上有碎片工作,但我偶然发现了一个问题,即视图在屏幕上停留在屏幕上。理想情况下,我希望它只是在一个特定的布局上,但我不太清楚为什么它会继续"到其他屏幕(在片段之间的过渡期间它也停留在屏幕上)。

任何反馈都将不胜感激!

Link to Imgur Screenshots

主:

 [AllowAnonymous]
    public ActionResult Register()
    {
        ViewBag.GendId = new SelectList(db.)
        return View();
    }

适配器:

private void initUI()
{
    // ---------Page Viewer with Fragments -----------

    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }


    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }
}

片段类基本上是这样的:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            // Main Frag
            return new Fragment1();
        case 1:
            // Second Frag
            return new Fragment2();
        case 2:
            // Third Frag
            return new Fragment3();
    }

    return null;
}

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

主要布局:

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main, container, false);

    return rootView;
}
}

1 个答案:

答案 0 :(得分:1)

EditText位于主布局中,与ViewPager所在的布局相同。应在选项卡中对其进行充气。您需要为每个标签创建单独的布局。

主要布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

layout_fragment_1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

然后你需要在片段1中膨胀新的布局。

View rootView = inflater.inflate(R.layout.layout_fragment_1, container, false);

return rootView;