思路:
每一个Tab里面的内容都放到一个fragment里面,然后再每个fragment里面处理具体的业务逻辑,在主页面上,像加入view那样加入fragment.
首先来看fragment里面代码:
/** * Created by LiuHuiChao on 2015/11/16. */ public class ViewPagerFragment1 extends Fragment { private String text; private TextView tv = null; public ViewPagerFragment1(){} @SuppressLint("ValidFragment") public ViewPagerFragment1(String text){ super(); this.text = text; } /** * 覆盖此函数,先通过inflater inflate函数得到view最后返回 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.tab1, container, false); tv = (TextView)v.findViewById(R.id.tv); tv.setText(text); return v; } }
fragment对应的layout:
<?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"> <TextView android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:textSize="20dp" android:layout_gravity="center" android:layout_weight="1" android:text="tab1" /> </LinearLayout>
之后是主页:
/** * Created by LiuHuiChao on 2015/11/16. */ public class ViewPagerFragment extends FragmentActivity { /** 页面list **/ List<Fragment> fragmentList = new ArrayList<Fragment>(); /** 页面title list **/ List<String> titleList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main3); ViewPager pager=(ViewPager)findViewById(R.id.viewPagerThree); fragmentList.add(new ViewPagerFragment1("页面1")); fragmentList.add(new ViewPagerFragment1("页面2")); fragmentList.add(new ViewPagerFragment1("页面3")); titleList.add("title 1 "); titleList.add("title 2 "); titleList.add("title 3 "); pager.setAdapter(new TestFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, titleList)); } class TestFragmentPagerAdapter extends FragmentPagerAdapter{ private List<Fragment> fragmentList; private List<String> titleList; public TestFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragmentList,List<String> titleList) { super(fm); this.fragmentList=fragmentList; this.titleList=titleList; } @Override public CharSequence getPageTitle(int position) { //return (titleList.size() > position) ? titleList.get(position) : ""; return titleList.get(position); } @Override public Fragment getItem(int position) { return (fragmentList == null || fragmentList.size() == 0) ? null : fragmentList.get(position); } @Override public int getCount() { return fragmentList == null ? 0 : fragmentList.size(); } } }
主页对应layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPagerThree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <android.support.v4.view.PagerTabStrip android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" /> </android.support.v4.view.ViewPager> </LinearLayout>
run了一下,发现以前自己想改掉的bug还在,明天再改吧,我的鱿鱼丝吃完了,而且我 也困了。感觉用fragment做的方式更好,感谢百度!明天收拾那两个小bug。晚安,亲们!
时间: 2024-10-13 17:47:23