如何在Fragment Class中添加ViewPager?

时间:2017-08-09 13:01:14

标签: android android-viewpager pagerslidingtabstrip

我正在尝试在Fragment Class中添加View Pager。我已经尝试了所有的东西。

public class MessageFragment extends android.support.v4.app.Fragment{

private ViewPager viewPager;
private TabLayout tabLayout;
public static MessageFragment newInstance() {
    MessageFragment fragment = new MessageFragment();
    Log.d("Click", "newInstance: ");
    return fragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_message, container, false);
    ViewPager viewPager=(ViewPager)view.findViewById(R.id.viewpager);
    if(viewPager!=null)
    {
        setUpViewPager(viewPager);
    }
    TabLayout tabLayout = (TabLayout)view. findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabTextColors(getResources().getColorStateList(R.color.colorToolbar));
    return view;

}
private void setUpViewPager(ViewPager viewPager)
{
    Adapter adapter=new Adapter(getActivity().getSupportFragmentManager());
    adapter.addFragment(new TabTaskFragment(),"Tasks");
    adapter.addFragment(new TabChatFragment(),"Chat");
    viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();
    public Adapter(FragmentManager fm) {
        super(fm);
    }
    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }
    @Override
    public int getCount() {
        return mFragments.size();
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

} 这是我的xml文件,我在这里添加了所有viewpager代码 请找到 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:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabIndicatorColor="@color/colorToolbar"
        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
        app:tabTextColor="@color/colorToolbar" />
</android.support.design.widget.AppBarLayout>

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

enter image description here

我该如何添加?

我无法修复它。

标签会在第一时间显示,但是当用户点击任何底部标签时,标签说明已消失且显示空白屏幕。

我终于把所有的东西都弄失了。

1 个答案:

答案 0 :(得分:0)

你的班级文件应该是这样的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/content_layout"
    android:layout_height="match_parent"
    android:background="@color/primary">


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@color/white"
            app:tabIndicatorHeight="2dp"
            app:tabMode="scrollable" />

        <android.support.v4.view.ViewPager
            android:id="@+id/transactions_recharge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tabs"
            android:background="@android:color/transparent">

        </android.support.v4.view.ViewPager>
</RelativeLayout>

你的XML应该像我这样。

// in view controller, in viewDidLoad function
let draggableView = DraggableView(frame: CGRect(x: 0, y: 30, width: 260, height: 260))    
draggableView.frame.origin.x = view.bounds.midX - draggableView.bounds.midX