Android 用viewPager替换容器
我制作了一个导航抽屉,正如你所知,导航抽屉将有一个容器,其中所有布局都是充气的。 我需要在其中一页上有一个可滚动的标签。我在将viewPager布局膨胀到容器中时遇到问题,该容器在以下片段类中实现:Android 用viewPager替换容器,android,android-fragments,Android,Android Fragments,我制作了一个导航抽屉,正如你所知,导航抽屉将有一个容器,其中所有布局都是充气的。 我需要在其中一页上有一个可滚动的标签。我在将viewPager布局膨胀到容器中时遇到问题,该容器在以下片段类中实现: public class Pager extends Fragment { ViewPager viewPager = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sav
public class Pager extends Fragment {
ViewPager viewPager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_pager);
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
MyAdapter myAdapter = new MyAdapter(fragmentManager);
viewPager.setAdapter(myAdapter);
}
}
class MyAdapter extends FragmentPagerAdapter{
Fragment fragment = null;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
fragment = new MapFragment();
break;
case 1:
fragment = new RegisterRestaurants();
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
}
页面布局:
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v4.view.ViewPager>
activity_main.xml:
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width
and height to consume the full space available. --> <android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.example.famfelimban.mydiet.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>'
'
您可以执行以下操作:
public class Pager extends Fragment {
ViewPager viewPager = null;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_pager, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ViewPager) view.findViewById(R.id.container);
MyAdapter myAdapter = new MyAdapter();
viewPager.setAdapter(myAdapter);
}
class MyAdapter extends FragmentPagerAdapter {
Fragment fragment = null;
public MyAdapter() {
super(getChildFragmentManager());
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment = new MapFragment();
break;
case 1:
fragment = new RegisterRestaurants();
break;
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
}
}
public class PagerActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pager);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.holder, new Pager())
.commit();
}
}
}
fragment_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
活动_pager.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
编辑
activity_pager.xml可以具有下一个:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<fragment android:id="@+id/navigation_drawer"
android:tag="navtag"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:name="com.example.NavigationFragment" />
</android.support.v4.widget.DrawerLayout>
感谢您的重播。我已经有了扩展ActionBarActivity的MainActivity,在这里我实现了导航抽屉。所以我不能使用PagePractivity,因为如果你明白我的意思,fragmentManager不会接受用活动替换片段@Stanislavbodnar您可以将类似的逻辑添加到您的ActionBarActivity中-只需将主视图作为ViewGroup的一部分,您可以在其中添加包含ViewPager的片段。是否可以从日志中包含错误消息请您的id在xml中称为“container”,您正试图通过“pager”引用它。阅读和理解logcat是你最好的朋友。寻呼机和容器是完全不同的,容器是版面膨胀的地方。pager是pager类的布局。我想问题是,除了可滚动选项卡之外,我还尝试使用导航抽屉检查容器标记所在的新更新代码@toidiucheck检查导入语句是否用于ViewPager是否在java代码中使用支持版本。你也可以提供完整的布局片段页面文件吗