既可以滑动也可以点击切换(ViewPager跟Fragment)

整体布局思路如图:



1、首先创建一个Activity并且继承FragmentActivity,布局文件中留下一个Framelayout

初始化事务,添加Fragment,提交事务

MainActivity:

package com.zhansy.mytext;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class MainActivity extends FragmentActivity {
    //FragmentTransaction对fragment进行添加,移除,替换,以及执行其他动作
    private FragmentTransaction fragmentTransaction;
    private Fragment myFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (myFragment == null) {
            myFragment = new MyFragment();
            fragmentTransaction.add(R.id.fl,myFragment);
        }else {
            fragmentTransaction.show(myFragment);
        }
        //最后要提交事务
        fragmentTransaction.commit();
    }

}

2、在Activity添加的Fragment中有两个子Fragment

package com.zhansy.mytext;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/10.
 */
public class MyFragment extends Fragment implements ViewPager.OnPageChangeListener{
    private FragmentTabPager pageadapter;
    List<Fragment> fragments;
    ViewPager viewPager;
    TextView mone,mtwo;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.myfragment, container, false);
        fragments = new ArrayList<Fragment>();
        //添加两个碎片
        fragments.add(new OneFragment());
        fragments.add(new TwoFragment());
        pageadapter = new FragmentTabPager(((MainActivity)getActivity()).getSupportFragmentManager());
        pageadapter.setmFragments((ArrayList<Fragment>) fragments);
        //这里花了好长时间,用getActivity()找不到它的ID的,必须用定义的view.findViewById
        viewPager = (ViewPager) view.findViewById(R.id.view_page);
        mone = (TextView) view.findViewById(R.id.mOne);
        mtwo = (TextView) view.findViewById(R.id.mTwo);
        viewPager.setAdapter(pageadapter);
        viewPager.setOnPageChangeListener(this);
        mone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(0);
            }
        });
        mtwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(1);
            }
        });
        return  view;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}

分别为OneFragment和TwoFragment

package com.zhansy.mytext;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/8/10.
 */
public class OneFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return LayoutInflater.from(getActivity()).inflate(R.layout.one, container, false);
    }
}

package com.zhansy.mytext;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2015/8/10.
 */
public class TwoFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return LayoutInflater.from(getActivity()).inflate(R.layout.two, container, false);
    }
}

最后肯定是一个适配器并且继承 FragmentStatePagerAdapter

package com.zhansy.mytext;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/8/10.
 */
public class FragmentTabPager  extends FragmentStatePagerAdapter {
    private ArrayList<Fragment> mFragments =new ArrayList<Fragment>();
    public ArrayList<Fragment> getmFragments() {
        return mFragments;
    }
    public void setmFragments(ArrayList<Fragment> mFragments) {
        this.mFragments = mFragments;
    }
    public FragmentTabPager(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        if(mFragments!=null&&mFragments.size()>0){
            return mFragments.get(i);
        }else{
            return null;
        }
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }
}

布局文件随便一点都可以,如下:

activity_main.xml:

<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=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fl" />

</RelativeLayout>

myfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<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"

    >


    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/my_one"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:tag="0">

            <TextView
                android:id="@+id/mOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="第一个"
                android:textSize="14sp" />


            <View
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"/>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/my_two"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:tag="1">

            <TextView
                android:id="@+id/mTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:text="第二个"
                android:textSize="14sp" />

            <View
                android:layout_width="1dp"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="2dp"
                android:layout_marginTop="5dp" />
        </RelativeLayout>

    </LinearLayout>
    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:layout_below="@id/ll">

        <View
            android:layout_width="fill_parent"
            android:layout_height="1px"
/>


    </RelativeLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/rl" />
</RelativeLayout>

one.xml和two.xml任意布局都可以,就不上图了


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值