标签:android http io ar os sp java 文件 on
FragmentActivity+ ViewPager + FragmentManager实现
废话少说,直接上我今晚的成果:
package com.itfanr.viewpagertab; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.support.v4.app.FragmentTransaction ; import android.support.v4.app.FragmentManager; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.TextView; public class MainActivity extends FragmentActivity { private FragmentManager fragmentManager; private ViewPager viewPager; private Context context; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = this.getSupportFragmentManager(); ; this.viewPager = (ViewPager) findViewById(R.id.mainViewPager); this.viewPager.setAdapter(new MainFragmentAdapter(fragmentManager)); this.viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){ @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { FragmentManager fragmentManager = MainActivity.this.fragmentManager ; List<Fragment> list = fragmentManager.getFragments() ; FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction() ; fragmentTransaction.show(list.get(arg0)) ; fragmentTransaction.commit() ; } }) ; } public static class PlaceholderFragment extends Fragment { private String text; private TextView textView; public PlaceholderFragment(String text) { this.text = text; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); this.textView = (TextView) getView().findViewById(R.id.textView); textView.setText(this.text); } } }
package com.itfanr.viewpagertab; import java.util.ArrayList; import java.util.List; import android.support.v4.app.FragmentManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import com.itfanr.viewpagertab.MainActivity.PlaceholderFragment; public class MainFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> mainFragment; public MainFragmentAdapter(FragmentManager fragmentManager) { super(fragmentManager); this.mainFragment = new ArrayList<Fragment>(); this.mainFragment.add(new PlaceholderFragment("第一个fragment")); this.mainFragment.add(new PlaceholderFragment("第二个fragment")); } @Override public Fragment getItem(int arg0) { return this.mainFragment.get(arg0); } @Override public int getCount() { return this.mainFragment.size(); } }
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainViewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" tools:context=".MainActivity" > </android.support.v4.view.ViewPager>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.placeholder.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
后面这两个分别是布局文件activity_main.xml和fragment_main.xml。
标签:android http io ar os sp java 文件 on
原文地址:http://my.oschina.net/itfanr/blog/349652