Android 使用 ViewPager 和 Fragments
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10180539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working with ViewPager and Fragments
提问by kibria3
I am a beginner Android developer. I am trying to get my head around the ViewPager. Before I was trying to work with this example:
我是一名初学者 Android 开发人员。我正在尝试了解 ViewPager。在我尝试使用此示例之前:
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/
I had the views working, but could not get functionality working within that, and I was told that I need to use Fragments within the ViewPager.
我有视图工作,但无法在其中运行功能,我被告知我需要在 ViewPager 中使用 Fragments。
I went with this tutorial instead:
我改为使用本教程:
http://tamsler.blogspot.co.uk/2011/11/android-viewpager-and-fragments-part-ii.html
http://tamsler.blogspot.co.uk/2011/11/android-viewpager-and-fragments-part-ii.html
I have got the basics of a ViewPager with a Fragment working:
我已经掌握了带有 Fragment 工作的 ViewPager 的基础知识:
FragmentPagerActivity.java
FragmentPagerActivity.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class FragmentPagerActivity extends FragmentActivity {
private static final int NUMBER_OF_PAGES = 10;
private ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
return PageFragment.newInstance("My Message " + index);
}
@Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
}
PageFragment.java:
PageFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
public static PageFragment newInstance(String title) {
PageFragment pageFragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
pageFragment.setArguments(bundle);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(getArguments().getString("title"));
return view;
}
}
main.xml:
主文件:
<?xml version="1.0" encoding="utf-8"?>
<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/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
fragment.xml:
片段.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="TextView" />
</LinearLayout>
I want to be able to replace the current layout within the ViewPager with 7 of my own layouts that already exist and add functionality to the layouts using Fragments. What can I do with the code that I already have and the changes to make to get this properly working?
我希望能够用我自己已经存在的 7 个布局替换 ViewPager 中的当前布局,并使用 Fragments 向布局添加功能。我可以如何处理我已有的代码以及为使其正常工作而进行的更改?
If there are any examples of a ViewPager with Fragments available online as well rather than just going briefly over it like the do in the Adroid Developers blog, I will be very grateful.
如果有任何带有 Fragments 的 ViewPager 在线示例,而不是像 Adroid Developers 博客中那样简单地介绍它,我将不胜感激。
回答by Mahdi Hijazi
Declare this inside your MyFragmentPagerAdapter:
在 MyFragmentPagerAdapter 中声明:
private Fragment[] fragments = new Fragment[] { new Fragment1(),Fragment2(),Fragment3(),Fragment4(),Fragment5()};
then implement getItem method like this:
然后像这样实现 getItem 方法:
@Override
public Fragment getItem(int index) {
return fragments[index];
}
and for each fragment have it is own class and layout:
并且对于每个片段都有自己的类和布局:
public class Fragment1 extends Fragment {
//your code here
}
回答by Ross Hambrick
Assuming you know how to create fragments themselves, just do something like this to get them into your view pager:
假设您知道如何自己创建片段,只需执行以下操作即可将它们放入您的视图寻呼机:
@Override
public Fragment getItem(int page) {
switch (page) {
case 0: return new MyFirstFragment();
case 1: return new MySecondFragment();
case 2: return new MyThirdFragment();
//and so on....
}
return null;
}
@Override
public int getCount() {
return [the count of total fragments];
}
instead of this:
而不是这个:
@Override
public Fragment getItem(int index) {
return PageFragment.newInstance("My Message " + index);
}
回答by Flaxie
I modified the answer from hambonious a bit:
我从 hambonious 修改了答案:
Declare this inside your MyFragmentPagerAdapter :
在 MyFragmentPagerAdapter 中声明:
private SparseArray<Fragment> fragments = new SparseArray<Fragment>();
private SparseArray<Fragment> fragments = new SparseArray<Fragment>();
then implement getItem method like this:
然后像这样实现 getItem 方法:
@Override
public Fragment getItem(int index) {
if(fragments != null && fragments.size() >= index)
return fragments.get(i);
else return null;
}
You also have to override getSize()
您还必须覆盖 getSize()
@Override
public int getCount() {
return fragments.size();
}
Then you can dynamic add and remove fragments from it.
然后你可以动态地添加和删除其中的片段。