新闻展示的view 库(tablayout+viewpager)

来源:互联网 发布:c语言论坛 编辑:程序博客网 时间:2024/05/21 22:58

https://github.com/xiuergit/EasyTabLayoutDemo.git

Easytablayout

一个简易的显示 新闻的库的UI组件,目前开发者只需要设置titles、与fragment就可以显示分页了,使用简单;
有三种样式可以设置,如下图;

效果图

这里写图片描述

使用方式

1、Easytablayout 的创建

支持两种方式
1)在布局文件里放置

 <com.bigdata.easytablayout.View.EasyTabLayout        android:id="@+id/easy"        app:Style="@integer/Normal"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>

2)在代码中创建

 EasyTabLayout easyTabLayout=new EasyTabLayout(this);

2、设置显示的标题、与Fragment

public void setData(ArrayList<String> titles,ArrayList<Fragment> fragments);        //例如    easyTabLayout.setData(DataToolTest.titles(),DataToolTest.fragments(DataToolTest.titles().size()));

3、设置样式

 //方式1:在代码中  easyTabLayout.setStyle(EasyTabLayout.INDICATOR); //三种样式:    public static  int   NORMAL=0;    public  static  int  INDICATOR=1;    public  static  int  LIGHT=2;// 方式2:      app:Style="@integer/Normal"

4、Easytablayout 部分代码

package com.bigdata.easytablayout.View;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Color;import android.os.Build;import android.support.annotation.Nullable;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.view.ViewPager;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;import com.bigdata.easytablayout.Adapter.EasyViewpageAdapter;import com.bigdata.easytablayout.R;import java.util.ArrayList;/** * Created by xiuer on 17/5/26. * * @emil: 15536849144@163.com * @desc: */public class EasyTabLayout extends LinearLayout{        private  String TAG="MarketView";        private  ArrayList<String>titles;        private TabLayout mTabLayout;        private ViewPager mViewPager;        private ArrayList<Fragment> fragments;        private FragmentManager mFragmentManger;        public static  int   NORMAL=0;        public  static  int  INDICATOR=1;        public  static  int  LIGHT=2;       public void setData(ArrayList<String> titles,ArrayList<Fragment> fragments) {           initView(titles,fragments);       }         private  int mStyle;        public EasyTabLayout(Context context) {                this(context,null);        }        public EasyTabLayout(Context context, @Nullable AttributeSet attrs) {               this(context,attrs,0);        }        public EasyTabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {                super(context, attrs, defStyleAttr);            TypedArray array=context.getTheme().obtainStyledAttributes(attrs,                    R.styleable.EasyTabLayout,defStyleAttr,0);            LayoutInflater.from(context).inflate(R.layout.view_easytablayout, this, true);             mTabLayout= (TabLayout) findViewById(R.id.easy_tab);            mViewPager=(ViewPager)findViewById(R.id.easy_tab_viewPager);                mFragmentManger= ((FragmentActivity)context).getSupportFragmentManager();                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {                  mTabLayout.setLayoutMode(TabLayout.MODE_SCROLLABLE);               }                 if(titles!=null&&fragments!=null){                     initView(titles,fragments);                 }            setStyle(array.getInteger(R.styleable.EasyTabLayout_Style,0));        }        public  void   setStyle(int styleCode){            if(styleCode==NORMAL){                mTabLayout.setTabTextColors(Color.WHITE,Color.RED);                mTabLayout.setSelectedTabIndicatorColor(Color.BLACK);                mTabLayout.setBackgroundColor(Color.BLACK);            }            else if(styleCode==LIGHT) {                mTabLayout.setTabTextColors(Color.BLACK,Color.RED);                mTabLayout.setSelectedTabIndicatorColor(Color.WHITE);                mTabLayout.setBackgroundColor(Color.WHITE);            }else if(styleCode==INDICATOR){                mTabLayout.setTabTextColors(Color.BLACK,Color.RED);                mTabLayout.setSelectedTabIndicatorColor(Color.RED);                mTabLayout.setBackgroundColor(Color.WHITE);            }        }        public   void  initView(ArrayList<String> titles,ArrayList<Fragment>fragments){            mViewPager.setAdapter(new EasyViewpageAdapter(mFragmentManger,fragments,titles));            mTabLayout.setupWithViewPager(mViewPager);        }}

5、总结:目前这个组件,fragment 需要开发者自己定义,之后,我会定义出常见的1、2个模板,以及这个库的使用暂时不支持 gradle,需要module 依赖,后续,我会支持gradle;暂代更新,请看后续;

https://github.com/xiuergit/EasyTabLayoutDemo.git

原创粉丝点击