20 ViewPager demo5,6:FragmentAdapter 导航数据
Demo5
文件结构:
MainActivity.java
package com.qf.day20_viewpager_demo5;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
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;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.qf.day20_viewpager_demo5.fragment.MyFragment;
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private List<Fragment> list;
private TextView[]tvs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
initTab();
//数据源 Fragment
initData();
//adapter
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for(int i=0;i<3;i++){
tvs[i].setBackgroundColor(Color.BLUE);
}
tvs[arg0].setBackgroundColor(Color.RED);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
//导航书签
public void initTab() {
tvs = new TextView[3];
LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
for(int i=0;i<3;i++){
tvs[i] = (TextView) layout.getChildAt(i);
tvs[i].setBackgroundColor(Color.BLUE);
tvs[i].setTag(i);
tvs[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem((Integer)v.getTag());
}
});
}
tvs[0].setBackgroundColor(Color.RED);
}
public void initData() {
list = new ArrayList<Fragment>();
MyFragment myFragment1 = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", 1);
myFragment1.setArguments(bundle);
MyFragment myFragment2 = new MyFragment();
Bundle bundle1 = new Bundle();
bundle1.putInt("index", 2);
myFragment2.setArguments(bundle1);
MyFragment myFragment3 = new MyFragment();
Bundle bundle2 = new Bundle();
bundle2.putInt("index", 3);
myFragment3.setArguments(bundle2);
list.add(myFragment1);
list.add(myFragment2);
list.add(myFragment3);
}
/**
* 自定义的Adapter
* @author sxy
*
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
/**
* 根据指定的下标 返回对应的Fragmengt对象
*/
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
/**
* Fragment的数量
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
}
MyFragment.java
package com.qf.day20_viewpager_demo5.fragment;
import com.qf.day20_viewpager_demo5.R;
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 MyFragment extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.layout_fragment, container, false);
tv = (TextView) view.findViewById(R.id.tv);
Bundle bundle = getArguments();
int index = bundle.getInt("index");
switch (index) {
case 1:
tv.setText("新闻");
break;
case 2:
tv.setText("娱乐");
break;
case 3:
tv.setText("段子");
break;
default:
break;
}
return view;
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_new"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="新闻"
/>
<TextView
android:id="@+id/tv_happy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
/>
<TextView
android:id="@+id/tv_dz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="段子"
/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
layout_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/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#f00"
/>
</LinearLayout>
Demo6
MainActivity.java
package com.qf.day20_viewpager_demo5;
import java.util.ArrayList;
import java.util.List;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.graphics.Color;
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;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.qf.day20_viewpager_demo5.fragment.MyFragment;
public class MainActivity extends FragmentActivity implements TabListener{
private ViewPager viewPager;
private List<Fragment> list;
private TextView[]tvs;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("新闻").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("娱乐").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("段子").setTabListener(this));
//数据源 Fragment
initData();
//adapter
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// // TODO Auto-generated method stub
// for(int i=0;i<3;i++){
// tvs[i].setBackgroundColor(Color.BLUE);
// }
//
// tvs[arg0].setBackgroundColor(Color.RED);
//ViewPager绑定ActionBar
actionBar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
// //导航书签
// public void initTab() {
// tvs = new TextView[3];
// LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
// for(int i=0;i<3;i++){
// tvs[i] = (TextView) layout.getChildAt(i);
// tvs[i].setBackgroundColor(Color.BLUE);
// tvs[i].setTag(i);
// tvs[i].setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// viewPager.setCurrentItem((Integer)v.getTag());
// }
// });
// }
// tvs[0].setBackgroundColor(Color.RED);
// }
public void initData() {
list = new ArrayList<Fragment>();
MyFragment myFragment1 = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", 1);
myFragment1.setArguments(bundle);
MyFragment myFragment2 = new MyFragment();
Bundle bundle1 = new Bundle();
bundle1.putInt("index", 2);
myFragment2.setArguments(bundle1);
MyFragment myFragment3 = new MyFragment();
Bundle bundle2 = new Bundle();
bundle2.putInt("index", 3);
myFragment3.setArguments(bundle2);
list.add(myFragment1);
list.add(myFragment2);
list.add(myFragment3);
}
/**
* 自定义的Adapter
* @author sxy
*
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
/**
* 根据指定的下标 返回对应的Fragmengt对象
*/
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
/**
* Fragment的数量
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
//Tab绑定ViewPager
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
MyFragment.java
package com.qf.day20_viewpager_demo5.fragment;
import com.qf.day20_viewpager_demo5.R;
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 MyFragment extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.layout_fragment, container, false);
tv = (TextView) view.findViewById(R.id.tv);
Bundle bundle = getArguments();
int index = bundle.getInt("index");
switch (index) {
case 1:
tv.setText("新闻");
break;
case 2:
tv.setText("娱乐");
break;
case 3:
tv.setText("段子");
break;
default:
break;
}
return view;
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
layout_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/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#f00"
/>
</LinearLayout>