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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值