这个小插件用了githup上下载的一个第三方jar包。安装这个jar包就已经搞死我了,githup上下来直接把zip的后缀名改为了jar就复制粘贴到工程的lib下了,而后本身点击了Add as library没有用,还报出一堆缺乏这个那个的内部jar包,也是醉了。。。耽误了很多时间,还要严格按照这个做者的引导,在build.gradle下写上规定的几句才能成功导进来。而后开始写这个小玩意,本身也是搞得一头灰,联系了写这个第三方jar包的做者,在他的交流下才能最终作好。。个人感想就是,不要随便使用第三方包,由于就算做者提供了文档,你还真的不必定能本身导入成功,做者提供了联系方式,还得像我遇到这个做者那么耐心引导才可能作得成。java
好了,下面先上效果图看看作的小插件是什么样儿的。android
下面是代码:git
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文件app
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文件ide
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的设置gradle
写入dependencies的后面三条ui
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' }