這個小插件用了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' }