public class TabFragment extends Fragment {
RecyclerView recyclerView;
ViewPager viewPager;
ArrayList list = new ArrayList();
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_tab,container,false);
initView();
initData();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// initData();
}
public void initView() {
recyclerView= (RecyclerView)view.findViewById(R.id.recyclerView);
viewPager= (ViewPager)view.findViewById(R.id.viewPager);
}
public void initData() {
list.add("tab1");
list.add("tab2");
list.add("tab3");
list.add("tab4");
LinearLayoutManager ms= new LinearLayoutManager(getActivity());
ms.setOrientation(LinearLayoutManager.HORIZONTAL);//
recyclerView.setLayoutManager(ms);
recyclerView.setAdapter(new TabAdapter(getActivity(),list));
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
// // 当不滑动时
// if (newState == RecyclerView.SCROLL_STATE_IDLE) {
// // 获取最后一个完全显示的itemPosition
// int lastItemPosition = manager.findLastCompletelyVisibleItemPosition();
// int itemCount = manager.getItemCount();
//
//
// // 判断是否滑动到了最后一个item,并且是向左滑动
// if (lastItemPosition == (itemCount - 1) && isSlidingToLeft) {
//
// // 加载更多
// }
// }
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
MyPagerAdapter myPageAdapter = new MyPagerAdapter(getFragmentManager());
myPageAdapter.setData(FragmentFactory.createFragment());
viewPager.setAdapter(myPageAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
recyclerView.smoothScrollToPosition(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
R.layout.fragment_tab
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android.support.v7.widget.RecyclerView>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
public class FragmentFactory {
static ArrayList datas =new ArrayList();
public static ArrayList createFragment() {
// for (int i=0;i<4;i++){
// TestFragment mFragment = new TestFragment();
// mFragment.setText("第"+i+"页");
// datas.add(mFragment);
// }
datas.add(new TestFragment());
datas.add(new Test2Fragment());
datas.add(new Test3Fragment());
datas.add(new Test4Fragment());
return datas;
}
}
public class MyPagerAdapter extends FragmentPagerAdapter {
ArrayList datas;
public void setData(ArrayList datas) {
this.datas = datas;
}
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return datas == null ? null : datas.get(position);
}
@Override
public int getCount() {
return datas == null ? 0 : datas.size();
}
}
public class TabAdapter extends RecyclerView.Adapter<TabAdapter.ViewHolder> {
private Context context;
private ArrayList mDatas;
public TabAdapter(Context context, ArrayList mDatas) {
super();
this.context = context;
this.mDatas = mDatas;
}
@Override
public TabAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_recycler_item, parent, false));
return holder;
}
@Override
public void onBindViewHolder(TabAdapter.ViewHolder holder, int position) {
holder.tabLayout.setTextView(mDatas.get(position));
}
@Override
public int getItemCount() {
return mDatas.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TabLayout tabLayout;
public ViewHolder(View itemView){
super(itemView);
tabLayout = (TabLayout)itemView.findViewById(R.id.tablelayout);
}
}
}
public class TestFragment extends Fragment {
View view;
TextView tv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_test,container,false);
initView();
initData();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void initView() {
tv = view.findViewById(R.id.test_tv);setText("第一页");
}
public void setText(String str){
tv.setText(str);
}
private void initData() {
}
}
layout_recycler_item.xml
<LinearLayout
android:id="@+id/item_table"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="37dip">
<com.example.jxl.myapplication.TabLayout
android:id="@+id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
LinearLayout>
layout_table.xml
<LinearLayout
android:id="@+id/item_table"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="37dip">
<com.example.jxl.myapplication.TabLayout
android:id="@+id/tablelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
LinearLayout>