android TableLayout和Fragment复用

            Android是在Android 3.0 (API level 11)开始引入Fragment的。Fragment可以使你能够将activity分离成多个可重用的组件,每个都有它自己的生命周期和UI。那我们应该怎么去创建fragment呢?Google已经考虑到这种情况了,所以推荐我们使用newInstance()的方式来创建,下面让我们看一下到底怎么使用吧!也是很简单的!

例,我们要实现的效果图,

这里写图片描述

MainActivity中:


public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private TabLayout tabLayout;
    private MainAdapter mainAdapter;
    private List fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        tabLayout = (TabLayout) findViewById(R.id.tab);

        mainAdapter = new MainAdapter(getSupportFragmentManager());
        fragments.add(MyFragment.newInstance("第一个fragment"));
        fragments.add(MyFragment.newInstance("第二个fragment"));
        fragments.add(MyFragment.newInstance("第三个fragment"));
        mainAdapter.setFragments(fragments);
        vp.setAdapter(mainAdapter);
        //设置tabLayout
        tabLayout.setupWithViewPager(vp);
        //设置文字的颜色
        tabLayout.setTabTextColors(Color.GRAY, Color.BLUE);
        //设置下划线的颜色
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
    }

}

MainAdapter中:


/**
 * Created by mac on 16-8-5.
 */
public class MainAdapter extends FragmentPagerAdapter {

    private List fragments;
    private String[] titles = {"一页", "二页", "三页"};


    public MainAdapter(FragmentManager fm) {
        super(fm);
    }

    public void setFragments(List fragments) {
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

}

MyFragment中:


/**
 * Created by mac on 16-8-6.
 */
public class MyFragment extends Fragment {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            String name = bundle.get("name").toString();
            Log.d("MyFragment", name);
        }
    }

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

    public static MyFragment newInstance(String name) {
        Bundle args = new Bundle();
        args.putString("name", name);
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }
}

MainActivity的布局文件:



<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"
    tools:context="com.example.mac.fragmentdemo.MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

MyFragment的布局文件:



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
LinearLayout>

运行程序后,我们会发现控制台出现信息如下,

这里写图片描述

在这种情况下,newInstance()方法是一种“静态工厂方法”,让我们在初始化和设置一个新的fragment的时候省去调用它的构造方法和额外的setter方法。为你的Fragment提供静态工厂方法是一种好的做法,因为它封装和抽象了在客户端构造对象所需的步骤,因此Google推荐使用,所我们就使用吧!

你可能感兴趣的:(android TableLayout和Fragment复用)