具有tabLayout和viewpager的Recyclerview

时间:2019-03-01 16:59:03

标签: android-recyclerview android-viewpager android-tablayout

我是编码方面的菜鸟。我有一个来自github的项目,我做了一点修改。我想将RecyclerView包含在TabLayout和ViewPager的主要片段中。

fragment_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<!-- tab Layout -->
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="?attr/actionBarSize"
    android:background="@color/tb_bg"
    android:minHeight="?attr/actionBarSize"
    app:tabIndicatorColor="@color/selectedTab"
    app:tabMode="scrollable"
    app:tabContentStart="60dp"
    app:tabTextColor="@color/unselectedTab"
    app:tabSelectedTextColor="@color/selectedTab"
app:tabTextAppearance="@style/TextAppearance.Design.Tab"/>

<!-- tab Layout content holder -->
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:layout_below="@id/tab_layout"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</RelativeLayout>

activity_awal.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gra_gray"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include layout="@layout/toolbar" />

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:background="@color/nav_bg"
    android:clickable="true"
    android:clipToPadding="false"
    android:layout_gravity="start"
    android:elevation="4dp"
    app:insetForeground="#44383838"
    app:itemBackground="@drawable/menu_background_color"
    app:itemIconTint="@color/menu_icon_color"
    app:itemTextColor="@color/menu_text_color"
    app:menu="@menu/activity_main_drawer"
    app:headerLayout="@layout/header"
    android:paddingLeft="16dp" />

P.S。还有另一个容器活动。

MainFragment.java

package sy.ali.player.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import sy.ali.player.R;


public class MainFragment extends Fragment {

private static ViewPager mPager;
private TabLayout mTabLayout;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    mPager = (ViewPager) view.findViewById(R.id.pager);
    mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);

// linking the tabLayout with the viewpager
    mPager.setAdapter(new TabsAdapter(getChildFragmentManager()));
    mTabLayout.setupWithViewPager(mPager);

    setHasOptionsMenu(true);

    return view;
 }

class TabsAdapter extends FragmentPagerAdapter {

    public TabsAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new TabPlaylists();
            case 1:
                return new TabSongs();
            case 2:
                return new TabAlbums();
            case 3:
                return new TabArtists();

        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Playlists";
            case 1:
                return "Songs";
            case 2:
                return "Albums";
            case 3:
                return "Artists";

        }
        return "";
    }
}
}

如何在不重写整个代码的情况下将Recyclerview仅实现到主要片段xml文件中?预先感谢。

0 个答案:

没有答案