android viewpagerindicator tab,ViewPager系列之顶部滑动indicator+viewPager

这个小插件用了githup上下载的一个第三方jar包。安装这个jar包就已经搞死我了,githup上下来直接把zip的后缀名改成了jar就复制粘贴到工程的lib下了,然后自己点击了Add as library没有用,还报出一堆缺少这个那个的内部jar包,也是醉了。。。耽误了不少时间,还要严格按照这个作者的引导,在build.gradle下写上规定的几句才能成功导进来。然后开始写这个小玩意,自己也是搞得一头灰,联系了写这个第三方jar包的作者,在他的交流下才能最终做好。。我的感想就是,不要随便使用第三方包,因为就算作者提供了文档,你还真的不一定能自己导入成功,作者提供了联系方式,还得像我遇到这个作者那么耐心引导才可能做得成。

好了,下面先上效果图看看做的小插件是什么样儿的。

0aa436078e8890f3c7e4ffcd5181fa02.png 

a9191ade9e16ca91d42c1e7b71186ebf.png

b613b4fb7431d4d8f30d40e713d5c71e.png 

7c14c38dbca5bd075d0de4a70beeafe5.png

下面是代码:

1. 主界面的布局文件,

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/indicator"

android:layout_width="fill_parent"

android:layout_height="50dp"

/>

android:id="@+id/viewPager"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

主界面的java文件

import android.graphics.Color;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import com.shizhefei.view.indicator.FixedIndicatorView;

import com.shizhefei.view.indicator.IndicatorViewPager;

import com.shizhefei.view.indicator.slidebar.ColorBar;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

FixedIndicatorView indicator;

List list;

ViewPager viewPager;

IndicatorViewPager indicatorViewPager;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//这个FixedindicatorView是平分tab的屏幕长度的

indicator = (FixedIndicatorView) findViewById(R.id.indicator);

viewPager = (ViewPager) findViewById(R.id.viewPager);

list = new ArrayList();

Fragment courseFragment = new CourseFragment();

list.add(courseFragment);

Fragment discussFragment = new DiscussFragment();

list.add(discussFragment);

Fragment makeFriendsFragment = new MakeFriendsFragment();

list.add(makeFriendsFragment);

Fragment personalFragment = new PersonalFragment();

list.add(personalFragment);

Fragment teacherFragment = new TeacherFragment();

list.add(teacherFragment);

indicatorViewPager = new IndicatorViewPager(indicator, viewPager);

indicatorViewPager.setAdapter(adapter);

//设置滑动时的那一项的图形和颜色变化,ColorBar对应的是下划线的形状。

indicator.setScrollBar(new ColorBar(getApplicationContext(), Color.parseColor("#00B2EE"), 5));

viewPager.setOffscreenPageLimit(1);//缓存的左右页面的个数都是1

}

public IndicatorViewPager.IndicatorFragmentPagerAdapter adapter = new IndicatorViewPager.IndicatorFragmentPagerAdapter(getSupportFragmentManager()) {

private String[] tabNames = {"课程选择", "讨论区", "交友区", "个人中心", "教师主页"};

@Override

public int getCount() {

return list.size();

}

@Override

public View getViewForTab(int position, View convertView, ViewGroup container) {

//此方法设置的tab的页面和显示

if (convertView == null) {

convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.tab,

container, false);

}

TextView tv = (TextView) convertView;

tv.setText(tabNames[position]);

return convertView;

}

@Override

public Fragment getFragmentForPage(int position) {

//设置viewpager下的页面

Fragment fragment = list.get(position);

return fragment;

}

};

}

2.ViewPager所包裹的fragment页面的java文件

import android.os.Bundle;

import com.shizhefei.fragment.LazyFragment;

public class CourseFragment extends LazyFragment {

//引用了第三方包提供的LazyFragment类和onCreateViewLazy方法

@Override

protected void onCreateViewLazy(Bundle savedInstanceState) {

super.onCreateViewLazy(savedInstanceState);

setContentView(R.layout.course);

}

}

3.indicator的tab的布局文件的书写

android:id="@+id/tv_Tab"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:textColor="#515151"

android:gravity="center"

android:text="TextView" />

4.导入的第三方jar包时build.gradle的设置

写入dependencies的后面三条

apply plugin: 'com.android.application'

android {

compileSdkVersion 28

defaultConfig {

applicationId "com.viewpagerindicator"

minSdkVersion 18

targetSdkVersion 28

versionCode 1

versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

}

}

}

dependencies {

implementation fileTree(include: ['*.jar'], dir: 'libs')

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

implementation 'com.android.support:appcompat-v7:28.0.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation files('libs/ViewPagerIndicator-master.jar')

implementation 'com.shizhefei:ViewPagerIndicator:1.1.7'

implementation 'com.android.support:support-v4:28.0.0'

implementation 'com.android.support:recyclerview-v7:28.0.0'

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值