Android中fragment切换标题操作

我这里要介绍这种标题的一种方法,我是用fragment来搭建的,最外层是一个Activity,应该会有更好的方法,大家有什么好办法可以一起来讨论

Android中fragment切换标题操作_第1张图片

1.先看布局
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" >
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/nav_bar"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/nav_bar_height"
        android:background="@color/nav_bg_color">
        <Button
            android:layout_width="@dimen/nav_bar_height"
            android:layout_height="@dimen/nav_bar_height"
            android:background="@drawable/nav_back_btn_selector"
            android:id="@+id/left_back_btn"
            android:layout_gravity="left|center_vertical"/>

        <TextView
            android:id="@+id/title_text_view"
            android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:padding="7dp"
            android:text="我的账户"
            android:gravity="center"
            android:singleLine="true"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="60dp"
            android:textSize="22sp"
            android:textColor="@color/white"
            android:layout_gravity="center"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="@dimen/nav_bar_height"
            android:id="@+id/right_btn"
            android:textSize="18sp"
            android:background="@null"
            android:textColor="@color/white"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_gravity="right|center_vertical"
            android:layout_marginRight="10dp"/>
    FrameLayout>

    <RelativeLayout
        android:id="@+id/relative_comm_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eeeeee" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/tv_msg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="7dp"
                    android:text="保证金账户"
                    android:textSize="18sp"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/sign_hotoriginal"
                    android:layout_width="@dimen/account_item__shape"
                    android:layout_height="2dp"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/tv_msg"
                    android:layout_marginBottom="2dp"
                    android:background="@color/calendar_close_color"
                    android:paddingTop="5dp" />


            RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" >

                <TextView
                    android:id="@+id/tv_msg2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="7dp"
                    android:textSize="18sp"
                    android:text="收入账户"
                    android:textColor="@color/black" />

                <TextView
                    android:id="@+id/sign_popular"
                    android:layout_width="@dimen/account_item__shape"
                    android:layout_height="2dp"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/tv_msg2"
                    android:layout_marginBottom="2dp"
                    android:background="@color/calendar_close_color"
                    android:visibility="invisible"
                    android:paddingTop="5dp" />

            RelativeLayout>


        LinearLayout>
    RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dddddd"
        android:paddingTop="5dp"
        android:layout_marginBottom="2dp" />

    <android.support.v4.view.ViewPager
        android:id="@+id/cartoon_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    android.support.v4.view.ViewPager>

LinearLayout>
Android中fragment切换标题操作_第2张图片

2.adapter类  pagerAdapter

public class MyAccountPageAdapter extends FragmentPagerAdapter {
    private ArrayList list;

    public MyAccountPageAdapter(FragmentManager fm,ArrayList list) {
        super(fm);
        this.list=list;
    }

    @Override
    public Fragment getItem(int arg0) {
        return list.get(arg0);
    }

    @Override
    public int getCount() {
        return list != null && !list.isEmpty() ? list.size() : 0;
    }

    public ArrayList getList() {
        return list;
    }

    public void setList(ArrayList list) {
        this.list = list;
    }

}
Android中fragment切换标题操作_第3张图片
3.Activity类
public class MyAccountActivity extends FragmentActivity implements View.OnClickListener,ViewPager.OnPageChangeListener {
   private View view;
   private String url;
   private MyAccountPageAdapter myAccountPageAdapter;
   private ArrayList fragmentLists;
   private ArrayList textview;

   private TextView tv_msg;//保底金账户text
   private TextView tv_msg2;//收入账户text
   private TextView sign_hotoriginal;//保底金账户下边的横线
   private TextView sign_popular;//收入账户下边的横线
   private ViewPager viewPager;
   protected Button leftBtn;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.my_account_layout);
      inite();

      tv_msg.setOnClickListener(this);
      tv_msg2.setOnClickListener(this);
      textview = new ArrayList();
      textview.add(tv_msg);
      textview.add(tv_msg2);
      textview.add(sign_hotoriginal);
      textview.add(sign_popular);

      viewPager.setOnPageChangeListener(this);
      viewPager.setOffscreenPageLimit(3);
      fragmentLists = new ArrayList();
      fragmentLists.add(EndOfAccountFragment.getInstance(""));
      fragmentLists.add(ReceiptsAccountFragment.getInstance(""));
      myAccountPageAdapter = new MyAccountPageAdapter(getSupportFragmentManager(), fragmentLists);

      viewPager.setOnPageChangeListener(this);
      viewPager.setOffscreenPageLimit(2);
      viewPager.setAdapter(myAccountPageAdapter);

      //使标题的字体颜色变成默认
      for (int i = 0; i < textview.size(); i++) {
         textview.get(i).setTextColor(getResources().getColor(R.color.black));
      }
      //使选中的标题的字体颜色变成选中时的颜色
      textview.get(Constant.CURRENT_PAGE).setTextColor(getResources().getColor(R.color.calendar_close_color));
   }

   private void inite() {

      leftBtn = (Button) findViewById(R.id.left_back_btn);
      leftBtn.setOnClickListener(this);

      tv_msg = (TextView) findViewById(R.id.tv_msg);//保底金账户
      sign_hotoriginal = (TextView) findViewById(R.id.sign_hotoriginal);
      tv_msg2 = (TextView) findViewById(R.id.tv_msg2);//收入账户
      sign_popular = (TextView) findViewById(R.id.sign_popular);
      viewPager = (ViewPager) findViewById(R.id.cartoon_viewpager);
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
         Util.finishActivityWithAnmationType(this, Util.ActivityFinishAnmationType.ANMATION_LEFT_IN_RIGHT_OUT);
         return true;
      }
      return super.onKeyDown(keyCode, event);
   }

   @Override
   public void onClick(View view) {
      switch (view.getId()) {
         case R.id.tv_msg:
            viewPager.setCurrentItem(0);
            break;
         case R.id.tv_msg2:
            viewPager.setCurrentItem(1);
            break;
         case R.id.left_back_btn:
            Util.finishActivityWithAnmationType(this, Util.ActivityFinishAnmationType.ANMATION_LEFT_IN_RIGHT_OUT);
            break;
      }
   }

   @Override
   public void onPageScrollStateChanged(int arg0) {

   }

   @Override
   public void onPageScrolled(int arg0, float arg1, int arg2) {

   }

   @Override
   public void onStart() {
      super.onStart();

   }

   /**
    * viewpager点击的效果
    */
   @Override
   public void onPageSelected(int position) {
      //当滑动viewpager时改变上边的白线与text的颜色
      for (int i = 0; i < 2; i++) {
         textview.get(i).setTextColor(getResources().getColor(R.color.black));
      }
      textview.get(position).setTextColor(getResources().getColor(R.color.calendar_close_color));
      if (position == 0) {
         textview.get(2).setVisibility(View.VISIBLE);
         textview.get(3).setVisibility(View.INVISIBLE);
      } else {
         textview.get(3).setVisibility(View.VISIBLE);
         textview.get(2).setVisibility(View.INVISIBLE);
      }
   }
}
 
   
4.保证金账户fragment  收入账户fragment一样的道理
 
   
public class EndOfAccountFragment extends Fragment implements View.OnClickListener{
    private View view;
    private String url;
    private double secureDeposit = 0f;
    protected CLoadingDialog dlg;
    private int usagetype = 0;

    private ListView rechargeListV;
    private AccountRechargeItemAdapter adapter;
    private EditText payEdit;
    private Float money;
    private static IWXAPI api;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.account_endofaccount_layout, null);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString("url");
    }

    public static Fragment getInstance(String url){
        EndOfAccountFragment endOfAccountFragment = new EndOfAccountFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        endOfAccountFragment.setArguments(bundle);
        return endOfAccountFragment;
    }



你可能感兴趣的:(Android中fragment切换标题操作)