Уже был такой
вопрос
А то что ниже, все же оставлю здесь
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar mToolBar;
private ViewPager mViewPager;
private TabLayout mTabLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mTabLayout = (TabLayout) v.findViewById(R.id.tablayout);
mToolBar = (Toolbar) v.findViewById(R.id.toolbar);
mViewPager = (ViewPager) v.findViewById(R.id.viewpager);
((AppCompatActivity) getActivity()).setSupportActionBar(mToolBar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.add(new MyFragment(), "First fragment"));
adapter.add(new MyFragment(), "Second fragment"));
}
}
MyFragment.java
public class MyFragment extends Fragment {
private RecyclerView mRecyclerView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
mRecyclerView =(RecyclerView) view.findViewById(R.id.my_fragment);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</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"/>
</android.support.design.widget.CoordinatorLayout>
my_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
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"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>