底布局的--首页---布局+方法---横向滑动菜单+页面滑动

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"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="#f00"
        android:id="@+id/lii">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="今日头条"
            android:textStyle="bold"
            android:textSize="22sp"
            android:layout_gravity="center_vertical"
            android:textColor="#fff"
            android:layout_marginLeft="30dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="hei***我的小可爱"
            android:layout_marginTop="10dp"
            android:background="@drawable/ed_border"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"/>
    LinearLayout>
<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabLayout"
    app:tabGravity="center"
    app:tabIndicatorColor="#fff"
    app:tabSelectedTextColor="#ff00"
    app:tabMode="scrollable"
    >android.support.design.widget.TabLayout>

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

LinearLayout>

--------------------------------------------------------------------------

package com.example.com.xiangmu_lmh;

import android.os.Bundle;
import android.support.annotation.NonNull;
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.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * Created by linmeihui on 2018/1/8.
 */

public class Fragment01 extends Fragment{

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List list;


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

        View v=inflater.inflate(R.layout.fragment01,container,false);

        tabLayout=v.findViewById(R.id.tabLayout);
        viewPager=v.findViewById(R.id.viewPager);


        list = new ArrayList<>();

        list.add("推荐");
        list.add("视频");
        list.add("热点");
        list.add("北京");
        list.add("娱乐");
        list.add("图片");
        list.add("汽车");
        list.add("体育");
        list.add("财经");
        list.add("国际");
        list.add("健康");


        viewPager.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager()));

        tabLayout.setupWithViewPager(viewPager);

        return v;
    }


    private class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager supportFragmentManager) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {

            Fra_tuijian fra_tuijian = new Fra_tuijian();

            Bundle bundle = new Bundle();
            if (list.get(position).equals("头条")){
                bundle.putString("type","top");
            }else if (list.get(position).equals("社会")){
                bundle.putString("type","shehui");
            }else if (list.get(position).equals("国内")){
                bundle.putString("type","guonei");
            }else if (list.get(position).equals("国际")){
                bundle.putString("type","guoji");
            }else if (list.get(position).equals("娱乐")){
                bundle.putString("type","yule");
            }else if (list.get(position).equals("体育")){
                bundle.putString("type","tiyu");
            }else if (list.get(position).equals("军事")){
                bundle.putString("type","junshi");
            }else if (list.get(position).equals("科技")){
                bundle.putString("type","keji");
            }else if (list.get(position).equals("财经")){
                bundle.putString("type","caijing");
            }else if (list.get(position).equals("时尚")){
                bundle.putString("type","shishang");
            }

            fra_tuijian.setArguments(bundle);


            return fra_tuijian;
        }

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

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {

            return list.get(position);
        }
    }
}


你可能感兴趣的:(底布局的--首页---布局+方法---横向滑动菜单+页面滑动)