TabLayout横向滑动

来源:互联网 发布:淘宝刺客电玩 switch 编辑:程序博客网 时间:2024/05/29 04:23

                                                                            main

    package animtest.com.example.e531.tablelayout_demo2;


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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


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


/**
 * tablayout+viewPager
 */
public class MainActivity extends AppCompatActivity {


    private List<String> datas=new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tabLayout= (TabLayout) findViewById(R.id.tas);
        ViewPager viewPager= (ViewPager) findViewById(R.id.vp);


        datas.add("推荐");
        datas.add("要闻");
        datas.add("娱乐");
        datas.add("科技");
        datas.add("汽车");
        datas.add("体育");
        datas.add("图片");
        datas.add("动漫");
        datas.add("社会");
        datas.add("游戏");


//        tabLayout.addTab();
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));


        //进行关联
        tabLayout.setupWithViewPager(viewPager);


    }




    class MyAdapter extends FragmentPagerAdapter{


        //带参的构造方法
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }


        //返回选项卡的文本 ,,,添加选项卡
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position);
        }


        @Override
        public Fragment getItem(int position) {
            Log.d("zzz","position:"+position);


            //创建fragment对象并返回
            Bundle bundle=new Bundle();
            bundle.putString("key",datas.get(position));


            ContentFragment contentFragment=new ContentFragment();
            contentFragment.setArguments(bundle);


            return contentFragment;
        }


        @Override
        public int getCount() {
            return datas.size();//返回选项卡的数量
        }
    }


}

                    Fragment

    package animtest.com.example.e531.tablelayout_demo2;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


/**
 * Created by e531 on 2017/10/13.
 */
public class ContentFragment  extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(),R.layout.content,null);
        TextView textView= (TextView) view.findViewById(R.id.tv);


        Bundle bundle=getArguments();
        textView.setText(bundle.getString("key"));




        return view;
    }
}

                         Xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="animtest.com.example.e531.tablelayout_demo2.MainActivity">




    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/tas"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/colorPrimary"
        ></android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp"
        android:layout_below="@+id/tas"
        ></android.support.v4.view.ViewPager>




</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:textColor="#f00"/>


</LinearLayout>

     

 





原创粉丝点击