与ViewPager和片段工作 [英] Working with ViewPager and Fragments
问题描述
我是初学者Android开发者。我试图让我的头周围的ViewPager。之前我是想用这个例子来工作:
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:
<一个href="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/
我有意见的工作,但无法得到的功能范围内的工作,而我被告知,我需要在ViewPager中使用的碎片。
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.
我本教程去代替:
<一个href="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
我有一个ViewPager的基础知识与片段的工作:
I have got the basics of a ViewPager with a Fragment working:
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中:
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之:
fragment.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>
我希望能够更换ViewPager在当前布局与我自己的布局已经存在7和添加功能使用片段布局。我能做些什么与code,我已经和修改,使得到这个正常工作?
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?
如果有一个ViewPager与现有的任何片段在网上的例子还有,而不只是简单地去在它喜欢做的的Adroid开发者博客,我将非常感激。
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.
推荐答案
声明这是你的MyFragmentPagerAdapter里面:
Declare this inside your MyFragmentPagerAdapter:
private Fragment[] fragments = new Fragment[] { new Fragment1(),Fragment2(),Fragment3(),Fragment4(),Fragment5()};
然后实现的getItem方法是这样的:
then implement getItem method like this:
@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
}
这篇关于与ViewPager和片段工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!