1. 程式人生 > >佈局FrmeLayout+button;TabLayout+ViewPager

佈局FrmeLayout+button;TabLayout+ViewPager

在這裡插入圖片描述
//**佈局

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:text="文章"
    android:id="@+id/tv"
    android:textColor="#fff"
    android:textSize="24dp"
    android:background="@color/colorPrimary"
    android:gravity="center"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/FrameLayout"></FrameLayout>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:text="文章"
        android:id="@+id/wenzhang"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/zhandian"
        android:text="站點"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="60dp"
        android:id="@+id/zhuti"
        android:text="主題"/>
</LinearLayout>
//***MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Fragment afragment;
private Fragment bFragment;
private Fragment cfragment;
private Button button1;
private Button button2;
private Button button3;
private FragmentManager manager;

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


private void initData() {
    afragment = new AFragment();
    bFragment = new BFragment();
    cfragment = new CFragment();
}

private void initView() {
    button1 = findViewById(R.id.wenzhang);
    button1.setOnClickListener(this);
    button2 = findViewById(R.id.zhandian);
    button2.setOnClickListener(this);
    button3 = findViewById(R.id.zhuti);
    button3.setOnClickListener(this);
    manager = getSupportFragmentManager();
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.wenzhang:
            manager.beginTransaction().replace(R.id.FrameLayout,afragment).commit();

            break;
        case R.id.zhandian:
            manager.beginTransaction().replace(R.id.FrameLayout,bFragment).commit();
            break;
        case R.id.zhuti:
            manager.beginTransaction().replace(R.id.FrameLayout,cfragment).commit();
            break;
    }
}

}
//*TabLayout+ViewPager//
在這裡插入圖片描述

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/TabLayout"
    ></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ViewPager"
    ></android.support.v4.view.ViewPager>
//***Frgment裡

public class AFragment extends Fragment {

private TabLayout tabLayout;
private ViewPager viewPager;
private List<Fragment> list;
private List<String> title;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.afragment,null);
    initData();
    initView(view);

    return view;
}

private void initData() {
    list = new ArrayList<>();
    title = new ArrayList<>();
    list.add(new Fragment_a());
    list.add(new Fragment_b());
    title.add("哈哈哈");
    title.add("呵呵呵");

}

private void initView(View view) {
    tabLayout = view.findViewById(R.id.TabLayout);
    viewPager = view.findViewById(R.id.ViewPager);
    viewPager.setAdapter(new FragPageAdapter(getChildFragmentManager(),title,list));
    tabLayout.setupWithViewPager(viewPager);
}

}
//*介面卡
public class FragPageAdapter extends FragmentPagerAdapter {
private List mtitle;
private List mlist;

public FragPageAdapter(FragmentManager fm, List<String> mtitle, List<Fragment> mlist) {
    super(fm);
    this.mtitle = mtitle;
    this.mlist = mlist;
}

@Override
public Fragment getItem(int i) {
    return mlist.get(i);
}

@Override
public int getCount() {
    return mlist.size();
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return mtitle.get(position);
}