1.先来看看fragment的代码
[java] view plain copy
import java.util.ArrayList;
import java.util.List;
import com.example.fm_reuse.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 TestFm extends Fragment{
private List list = new ArrayList();
private int flag;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle bundle =this.getArguments();
if(bundle != null){
list = bundle.getStringArrayList("content");
flag = bundle.getInt("flag");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fm_test, container,false);
initView(view);
return view;
}
public void initView(View view){
tv = (TextView)view.findViewById(R.id.tv);
tv.setText(list.get(flag));
}
public static TestFm newInstance(List contentList,int flag){
Bundle bundle =new Bundle();
bundle.putStringArrayList("content", (ArrayList) contentList);
bundle.putInt("flag", flag);
TestFm testFm =new TestFm();
testFm.setArguments(bundle);
return testFm;
}
}
fm_test.xml
[html] view plain copy
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"/>
上面的代码很简单,newInstance这个方法在实例化fragment时将会用到,传入需要的数据并通过setArguments方法将数据保存,这样,当跳转到当前fragment时就可以在onCreate方法中通过getArguments拿到数据,就是这么的一个过程。下面我们来看一下MainActivity的代码:
[java] view plain copy
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import com.example.fm_reuse.adapter.FragmentVPAdapter;
import com.example.fm_reuse.fragment.TestFm;
public class MainActivity extends FragmentActivity {
private List titleList = new ArrayList(); //标题链表
private List contentList = new ArrayList(); //内容链表
private List fragmentList = new ArrayList(); //碎片链表
private int screenWidth; //屏幕宽度
private ViewPager vp;
private HorizontalScrollView scrollView;
private List textViews = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();//初始化内容和标题
//获取屏幕宽度
WindowManager windowManager =this.getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
vp = (ViewPager)findViewById(R.id.viewPager);
scrollView = (HorizontalScrollView)findViewById(R.id.scrollView);
//有多少个标题就有多少个碎片,动态添加
for(int i=0;i
TestFm testFm =new TestFm().newInstance(contentList, i);
fragmentList.add(testFm);
}
//初始化导航栏布局
LinearLayout navigationLl =new LinearLayout(this);
LinearLayout.LayoutParams mParams =new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
navigationLl.setLayoutParams(mParams);
navigationLl.setOrientation(LinearLayout.HORIZONTAL);
navigationLl.setBackgroundColor(Color.GREEN);
//往导航栏添加标题
if(titleList.size() <= 3){ //标题栏小于4个时,平分屏幕宽度
LinearLayout.LayoutParams params =new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.weight =1;
for (int i = 0; i < titleList.size(); i++) {
final TextView tv = new TextView(this);
tv.setText(titleList.get(i));
tv.setGravity(Gravity.CENTER);
final int finalI = i;
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setTextColor(Color.RED);
vp.setCurrentItem(finalI);
}
});
textViews.add(tv);
navigationLl.addView(tv, params);//往导航栏添加标题
}
}
if(titleList.size() > 3){ //标题大于四个,重新规划textView大小
LinearLayout.LayoutParams params1 =new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params1.width =300;
params1.height = LayoutParams.MATCH_PARENT;
for (int i = 0; i < titleList.size(); i++) {
final TextView tv = new TextView(this);
tv.setText(titleList.get(i));
tv.setGravity(Gravity.CENTER);
final int finalI = i;
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.setTextColor(Color.RED);
vp.setCurrentItem(finalI);
}
});
textViews.add(tv);
navigationLl.addView(tv, params1);
}
}
//第一个标题默认红色
if (textViews != null && textViews.size() > 0) {
textViews.get(0).setTextColor(Color.RED);
}
scrollView.addView(navigationLl);//往scrollView添加导航栏
vp.setAdapter(new FragmentVPAdapter(getSupportFragmentManager(), (ArrayList) fragmentList));
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setSelect(position);
if(position>=4){
scrollView.scrollBy((int) (0.25*screenWidth), 0);
}else if(position<4){
scrollView.scrollBy(-(int) (0.25*screenWidth), 0);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void setSelect(int position) {
vp.setCurrentItem(position);
for (int i = 0; i < textViews.size(); i++) {
textViews.get(i).setTextColor(Color.BLACK);
}
textViews.get(position).setTextColor(Color.RED);
}
public void initList(){
//添加标题
titleList.add("标题一");
titleList.add("标题二");
titleList.add("标题三");
titleList.add("标题四");
titleList.add("标题五");
titleList.add("标题六");
//添加内容
contentList.add("页面一");
contentList.add("页面二");
contentList.add("页面三");
contentList.add("页面四");
contentList.add("页面五");
contentList.add("页面六");
}
}
注释也写得很清楚了,所以就不多说了,相信大家都能看懂。
activity_main.xml
[html] view plain copy
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:fillViewport="true"
android:scrollbars="none"
>
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
最后把viewpager的适配器的代码也贴出来:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import java.util.ArrayList;
import com.example.fm_reuse.fragment.TestFm;
/**
* Created by _H_JY on 2015/11/23.
*/
public class FragmentVPAdapter extends FragmentPagerAdapter {
private ArrayList fragments;
private FragmentManager fm;
public FragmentVPAdapter(FragmentManager fm, ArrayList fragments) {
super(fm);
this.fm = fm;
this.fragments = fragments;
}
public void setFragments(ArrayList fragments) {
if(this.fragments != null){
FragmentTransaction ft = fm.beginTransaction();
for(Fragment f:this.fragments){
ft.remove(f);
}
ft.commit();
ft=null;
fm.executePendingTransactions();
}
this.fragments = fragments;
notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int arg0) {
return fragments.get(arg0);
}
@Override
public int getCount() {
return fragments.size();
}
}