ViewPager+Fragment练习first

来源:互联网 发布:网络扑克 编辑:程序博客网 时间:2024/06/03 20:16

理解ViewPager+Fragment

  1、什么是ViewPager?翻译(多面控件)ViewPager:它是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。  2、什么是Fragment?  引用fragment的目的在于适应众多的分辨率,可以在不同屏幕上动态管理UI.可以将一个activty分成不同的区块来现实,大屏小屏实现很好的兼容 。  它相当于Activity中的一个模块,它继承了Activity的大部分特点,有自己的生命周期,自己的输入输出,同时在运行的时候Activity可以加载和移除它。

步骤

 1建一个主activity和对应的布局(listView或者RecyclerView或者数组+viewPager) 2 多个fragment,对应的多个fragment 3适配器实现:FragmentPagerAdapter 4 fragment与activity联系 5 构建一个fragments的ArrayList集合 6 初始化fragment(例如:List<Fragment> fragments = new ArrayList<Fragment>();) 7 放入多个fragment 8 构建adapter,初始化adapter---(getSupportFragmentManager(),list集合) 9 初始化viewPager 10 adapter与viewPager联系

例子

activity截图

          需要创建的activity和fragment

布局截图

          需要创建的xml

这里写图片描述

TestActivity:

import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.view.ViewPager;import com.joke.mtdz.android.R;import java.util.ArrayList;import java.util.List;...........    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.testactivity);        List<Fragment> fragments = new ArrayList<Fragment>(); //fragment初始化        fragments.add(new FragmentOne());        fragments.add(new FragmentTwo());        fragments.add(new FragmentThree());        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);/*** getSupportFragmentManager();方法的调用Activity必须继承** FragmentActivity*/        ViewPager viewPager = (ViewPager) findViewById(R.id.vp_test);        viewPager.setAdapter(fragmentAdapter);    }

FragmentOne:

import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.joke.mtdz.android.R;import com.joke.mtdz.android.utils.ToastUtil;public class FragmentOne extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view= inflater.inflate(R.layout.test_fragment_one,container,false);        TextView textView= (TextView) view.findViewById(R.id.tv_test_one);        textView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                ToastUtil.showToast("点击了fragmentone");            }        });        return view;    }}

FragmentTwo与FragmentThree 与One基本相同


testactivtity.xml对应(TestActivity):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <android.support.v4.view.ViewPager        android:id="@+id/vp_test"        android:layout_width="match_parent"        android:layout_height="match_parent">    </android.support.v4.view.ViewPager></LinearLayout>

test_fragment_one对应(FragmentOne):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:background="#028f99"(背景颜色自己改变)    android:gravity="center"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/tv_test_one"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragmentone"        android:textColor="#000000"        android:textSize="25sp" /></LinearLayout>

test_fragment_two与test_fragment_three与One基本相同

0 0
原创粉丝点击