VerticalTabLayout的使用

宇文和昶
2023-12-01
导入依赖
compile 'q.rorbin:VerticalTabLayout:1.2.5'

布局文件
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
<q.rorbin.verticaltablayout.VerticalTabLayout
    android:layout_width="100dp"
    android:id="@+id/vtb"
    android:layout_height="match_parent">
</q.rorbin.verticaltablayout.VerticalTabLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/vtbvp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
Activity
package com.example.view;

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.view.View;

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

import q.rorbin.verticaltablayout.VerticalTabLayout;

public class MainActivity extends AppCompatActivity {
    private View inflate;
    private VerticalTabLayout vtb;
    private List<String> datas = new ArrayList<String>();
    private ViewPager vtbvp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vtb = (VerticalTabLayout) findViewById(R.id.vtb);
        vtbvp = (ViewPager) findViewById(R.id.vtbvp);
        datas.add("推荐");
        datas.add("要闻");
        datas.add("娱乐");
        datas.add("科技");
        datas.add("汽车");
        datas.add("体育");
//适配器
        vpsp vpsp = new vpsp(getSupportFragmentManager());
        vtbvp.setAdapter(vpsp);
进行关联
        vtb.setupWithViewPager(vtbvp);
    }

    class vpsp extends FragmentPagerAdapter {

        public vpsp(FragmentManager fm) {
            super(fm);
        }
        //返回选项卡的文本 ,,,添加选项卡
        @Override
        public CharSequence getPageTitle(int position) {
            return datas.get(position);
        }
        //创建fragment对象并返回
        @Override
        public Fragment getItem(int position) {
            vcount vcount = new vcount();
            Bundle bundle = new Bundle();
            bundle.putString("name",datas.get(position));
            vcount.setArguments(bundle);
            return vcount;        }
        //返回数量
        @Override
        public int getCount() {
            return datas.size();
        }
    }
}
package com.example.view;

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

/**
 * Created by 老北城 on 2017/10/25.
 */

public class vcount extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = View.inflate(getActivity(), R.layout.vcountlayout, null);
        TextView cte = (TextView) inflate.findViewById(R.id.oo);
        Bundle arguments = getArguments();
        String name = arguments.getString("name");
        Log.e("chen", "onCreateView: ------" + name);
        cte.setText(name);
        return inflate;
    }
}
布局
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/oo"
    />
剩下的可以参考tablayout的做法   他们两个是一样的
 类似资料: