TabLayout横排滑动
来源:互联网 发布:淘宝注册公司靠谱吗 编辑:程序博客网 时间:2024/06/08 19:30
我以前有写过Tablayout横排滑动,不过相对有些幼稚,而且跟不上一些节奏了。
首先是导入依赖
compile 'com.android.support:design:23.3.0'
在导入依赖方面可要注意如果你的appcompat版本过高可能会报
android.support.v7.widget.TintManager
如果出现这个错误可要适当的降低你的依赖。
废话不多说上代码
先说布局吧
activity
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" > <android.support.design.widget.TabLayout android:id="@+id/main_tab" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabSelectedTextColor="@android:color/white" app:tabTextColor="#000000" app:tabIndicatorColor="#FF4081" app:tabGravity="fill" app:tabBackground="@android:color/holo_blue_bright"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/main_viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </android.support.v4.view.ViewPager></LinearLayout>
fragment
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_ll" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:gravity="center"> <TextView android:id="@+id/fragment_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="@android:color/white" android:text="Tab1"/></LinearLayout>
ViewpagerFragment
public class ViewpagerFragment extends Fragment { private int mTitle; private int mColor; private TextView mTextView; private LinearLayout mLinear; public static ViewpagerFragment newInstance(int title,int color){ ViewpagerFragment fragment=new ViewpagerFragment(); Bundle bundle=new Bundle(); bundle.putInt("title",title); bundle.putInt("color",color); fragment.setArguments(bundle); return fragment; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTitle=getArguments().getInt("title"); mColor=getArguments().getInt("color"); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_layout,container,false); mTextView= (TextView) view.findViewById(R.id.fragment_textView); mTextView.setText("Page"+(mTitle+1)); mLinear= (LinearLayout) view.findViewById(R.id.fragment_ll); mLinear.setBackgroundResource(mColor); return view; }}
MyAdapter
public class MyAdapter extends FragmentPagerAdapter { private int mCount=3; private int[] mColors=new int[]{android.R.color.holo_orange_dark,android.R.color.holo_green_dark,android.R.color.holo_red_dark}; public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return ViewpagerFragment.newInstance(position,mColors[position]); } @Override public int getCount() { return mCount; } @Override public CharSequence getPageTitle(int position) { return "Page"+(position+1); } }
activity
private ViewPager mViewPager; private TabLayout mTabLayout; private MyAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mAdapter=new MyAdapter(getSupportFragmentManager()); mViewPager= findViewById(R.id.main_viewPager); mViewPager.setAdapter(mAdapter); mTabLayout= findViewById(R.id.main_tab); //将ViewPager与TabLayout进行关联 mTabLayout.setupWithViewPager(mViewPager); //设置是固定的,还可以设置为TabLayout.MODE_SCROLLABLE, //可滚动的,用于多个Tab mTabLayout.setTabMode(TabLayout.MODE_FIXED); }
阅读全文
0 0
- TabLayout横排滑动
- TabLayout滑动
- 仿今日的横排滑动
- PullAndTab组合横排滑动上落下提
- TabLayout 滑动导航栏
- 横向滑动Tablayout
- TabLayout+ViewPager禁止滑动
- TabLayout+ViewPager禁止滑动
- TabLayout+滑动+网络展示
- TabLayout滑动导航
- 横向滑动 TabLayout
- TabLayout 资讯首页滑动
- TabLayout横向滑动
- TabLayout滑动效果显示
- 横向滑动菜单TabLayout
- tablayout横向滑动
- TabLayout滑动固定顶部
- TabLayout横向滑动菜单
- 将sql文件导入数据库中
- iOS 手势密码锁
- java json编程
- av_read_frame()
- Jboss与com.sun.jersey.guice冲突
- TabLayout横排滑动
- Git shell
- (三)java数据库篇笔记库(32)
- python实现删除文件与目录的方法
- 使用AnyProxy自动抓取微信公众号数据-包括阅读数和点赞数
- 快速教程:使用Cython来扩展Python/NumPy库
- SVN中Branch/tag的比较
- 译文 | 与TensorFlow的第一次接触 第六章:并发
- 使用scikit-learn解释随机森林算法