如何在具有不同ViewPager的布局上创建android编程

时间:2018-04-17 18:14:49

标签: java android tabs android-viewpager fragment

如何创建android编程,如果我点击,

选项卡1 - ViewPager1出现Fragment1和ViewPager2出现Fragment1A

和 如果我点击,

选项卡2 - ViewPager1显示Fragmen2和ViewPager2显示Fragmen2A。

PagerAdapter.java

package com.papaozi.ground;

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

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            Satu tab1 = new Satu();
            return tab1;

        case 1:
            Dua tab2 = new Dua();
            return tab2;

        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}

ProfilActivity.java

package com.papaozi.ground;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.support.annotation.DrawableRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

public class ProfilActivity extends AppCompatActivity {

ViewPager simpleViewPager;
private TabLayout tabLayout, tabLayout2;
private ViewPager viewPager, viewPager2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profil);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    createViewPager(viewPager);
    viewPager2 = (ViewPager) findViewById(R.id.viewpager2);
    createViewPager2(viewPager2);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout2 = (TabLayout) findViewById(R.id.tabs);
    tabLayout2.setupWithViewPager(viewPager2);
    createTabIcons();
}

private void createTabIcons() {
LinearLayout tabLinearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabOne = (TextView) tabLinearLayout.findViewById(R.id.tabContent);
tabOne.setText("Paslon 1");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icontoast, 0, 0, 0);
tabLayout2.getTabAt(0).setCustomView(tabOne);
tabLayout.getTabAt(0).setCustomView(tabOne);

    LinearLayout tabLinearLayout2 = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    TextView tabTwo = (TextView) tabLinearLayout2.findViewById(R.id.tabContent);
    tabTwo.setText("Paslon 2");
    tabTwo.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icontoast, 0, 0, 0);
    tabLayout2.getTabAt(1).setCustomView(tabTwo);
    tabLayout.getTabAt(1).setCustomView(tabTwo);
}

private void createViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFrag(new SatuA(), "Tab 1");
    adapter.addFrag(new DuaA(), "Tab 2");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();


    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);

    }
}
private void createViewPager2(ViewPager viewPager2) {
ViewPagerAdapter2 adapter = new ViewPagerAdapter2(getSupportFragmentManager());
adapter.addFrag(new Satu(), "Tab 1");
adapter.addFrag(new Dua(), "Tab 2");
viewPager2.setAdapter(adapter);
}

class ViewPagerAdapter2 extends FragmentPagerAdapter {
    private final List<Fragment> mSilit1 = new ArrayList<>();
    private final List<String> mSempak1 = new ArrayList<>();


    public ViewPagerAdapter2(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFrag(Fragment fragment, String title) {
        mSilit1.add(fragment);
        mSempak1.add(title);

    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mSempak1.get(position);

    }
}
}

fragmen1.java

package com.papaozi.ground;

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

public class Satu extends Fragment {

public Satu() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
// Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_dua, container, false);
}

}

fragmen2.java

    package com.papaozi.ground;

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

public class Dua extends Fragment {

public Dua() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
// Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_dua, container, false);
}

}

activity_profil.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedscrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/backgroudscrollgrid_color"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="206dip"
        android:background="@color/background"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/putih"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginBottom="20dp"
        app:expandedTitleMarginEnd="64dp">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <TextView
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@android:color/darker_gray"
        app:tabIndicatorColor="#f00"
        app:tabSelectedTextColor="#f00"
        app:tabTextColor="#000" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

custom_tab.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:orientation="vertical"
android:gravity="center">
<TextView
    android:id="@+id/tabContent"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:gravity="center"/>
</LinearLayout>

fragmen1.xml fragmen2.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"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Satu Fragment"
    android:textColor="@color/putih"
    android:textSize="40dp"
    android:textStyle="bold" />
</RelativeLayout>
谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

有一个简单的教程可供您使用。您可以查看this link.

但是当点击正常按钮时它会替换碎片。您只需将代码移动到标签的点击事件即可。就像那样:See the link

  

如果您已经将fragment1和fragment2添加到viewpager1并将fragment1A和fragment2A添加到viewpager2中,并且如果您只想在单击选项卡时以编程方式切换片段,则可以使用viewpager1.setCurrentItem(1)   标签中的viewpager1.setCurrentItem(0)&#39;点击活动。