2019-08-20 app常用模块布局之viewpager和f

2019-08-20  本文已影响0人  xiaohuage

布局效果:

布局代码:

<?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">

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:layout_marginTop="1dp"

        android:background="@android:color/white"

        android:baselineAligned="false"

        android:gravity="center_vertical"

        android:orientation="horizontal"

        android:paddingBottom="5dp"

        android:paddingTop="5dp">

            android:id="@+id/item_detail"

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_weight="1"

            android:gravity="center_horizontal|center_vertical"

            android:text="@string/detail_tab"

            android:textColor="@color/main_tab_text_color"

            android:textSize="20dp"/>

            android:id="@+id/item_category_report"

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:layout_weight="1"

            android:gravity="center_horizontal|center_vertical"

            android:text="@string/category_report_tab"

            android:textColor="@color/main_tab_text_color"

            android:textSize="20dp"/>

        android:id="@+id/mainViewPager"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1"/>

</LinearLayout>

思路:

viewpager控件上用适配器加载fragment:

private OneFragment  oneFragment;

private TwoFragment  twoFragment;

private ViewPager vp;

private FragmentAdapter  mFragmentAdapter;

private List  mFragmentList =new ArrayList();

vp = (ViewPager) findViewById(R.id.mainViewPager);

oneFragment =new OneFragment();

twoFragment =new TwoFragment();

//给FragmentList添加数据

mFragmentList.add(oneFragment);

mFragmentList.add(twoFragment);

mFragmentAdapter =new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);

vp.setAdapter(mFragmentAdapter);

OneFragment.java代码:

package com.hyl.acccountbookdemo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

/**

* @programName: OneFragment.java

* @programFunction:

* @createDate: 2018/09/25

* @author: AnneHan

* @version:

* xx.  yyyy/mm/dd  ver    author    comments

* 01.  2018/09/25  1.00  AnneHan  New Create

*/

public class OneFragmentextends Fragment {

@Override

    public ViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_one, container, false);

    }

}

fragment_one.xml布局代码:

<?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:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:gravity="center"

        android:text="@string/detail_tab"

        android:textSize="25sp"/>

TwoFragment.java代码:

package com.hyl.acccountbookdemo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

/**

* @programName: TwoFragment.java

* @programFunction:

* @createDate: 2018/09/25

* @author: AnneHan

* @version:

* xx.  yyyy/mm/dd  ver    author    comments

* 01.  2018/09/25  1.00  AnneHan  New Create

*/

public class TwoFragmentextends Fragment {

@Override

    public ViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_two, container, false);

    }

}

fragment_two.xml代码:

<?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:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:gravity="center"

        android:text="@string/category_report_tab"

        android:textSize="25sp"/>

public class FragmentAdapter  extends FragmentPagerAdapter {

ListfragmentList =new ArrayList();

    public FragmentAdapter(FragmentManager fm, List fragmentList) {

super(fm);

        this.fragmentList = fragmentList;

    }

@Override

    public FragmentgetItem(int position) {

return fragmentList.get(position);

    }

@Override

    public int getCount() {

return fragmentList.size();

    }

}

实现效果:

上一篇下一篇

猜你喜欢

热点阅读