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

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

ViewPager系列之顶部滑动indicator+viewPager-LMLPHP  ViewPager系列之顶部滑动indicator+viewPager-LMLPHP

ViewPager系列之顶部滑动indicator+viewPager-LMLPHP  ViewPager系列之顶部滑动indicator+viewPager-LMLPHP

下面是代码:

1. 主界面的布局文件,

<?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"
    android:orientation="vertical"
    >
    <!-- 引用的第三方jar包的控件,可以自由滑动的项-->
 <com.shizhefei.view.indicator.FixedIndicatorView
     android:id="@+id/indicator"
     android:layout_width="fill_parent"
     android:layout_height="50dp"
     />
 <android.support.v4.view.ViewPager
     android:id="@+id/viewPager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     />

</LinearLayout>

主界面的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<Fragment> 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>();
        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的布局文件的书写

<?xml version="1.0" encoding="utf-8"?>
<TextView  xmlns:android="http://schemas.android.com/apk/res/android"
        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'
}
11-07 17:31