TIPS:樓主作為一個android初學(xué)者恰梢,對于安卓一些開源的控件都不太熟悉唉擂,在百度的過程中谭胚,發(fā)現(xiàn)一個網(wǎng)站maven center repository可以查詢各種你需要的jar包,很實用仙蚜。
博主在上篇Fragment實現(xiàn)類似頭條新聞顯示效果(Fragment內(nèi)嵌套Fragment)中實現(xiàn)了類似頭條新聞的界面切換匙姜,但是資訊界面是利用fragment+radiogroup實現(xiàn)的頁面切換脂倦,只能通過點擊radiobutton切換頁面番宁,不符合用戶使用習(xí)慣,也不符合現(xiàn)在主流app的形式赖阻。為了使得界面可以實現(xiàn)左右滑動切換蝶押,我改用了viewpager+viewpagerindicator+fragment的實現(xiàn)形式。其中樓主踩了不少坑火欧,這里主要解決這些坑棋电。
具體的實現(xiàn)樣例和代碼,可以看看陳利健開源控件ViewPagerIndicator的使用的文章苇侵,介紹的很詳細赶盔。
博主遇到的最大的坑就是引入viewpagerindicator的包時,系統(tǒng)編譯會報錯榆浓,顯示v4包已存在(第三方包引用了v4包于未,和本地依賴包沖突),在陳利健的文章里也說明了一種解決方式陡鹃,但這種解決方式只適合通過Library手動加載依賴包的方式烘浦,你可以通過手動在文件里查找到v4包并刪除它。但這種解決方法顯然不適合我們通過implementation來引入依賴包萍鲸,因為你沒法在本地libs中找到v4包并刪除它闷叉。
博主最終在stackoverflow上找到解決方法,即在gradle的依賴中加入configurations.all {exclude group: 'com.android.support', module: 'support-v4'}
猿推,關(guān)于這么做的具體原因片习,可以參考Controlling specific dependencies,這里詳細介紹了關(guān)于gradle的知識蹬叭,很有學(xué)習(xí)價值。
這里貼一下博主的dependencise聲明
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//v7包中已經(jīng)引用了v4包状知,因此這個依賴會和下面的viewpagerindicator產(chǎn)生v4沖突
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//.....其他依賴
//ViewPagerIndicator中引用了v4沖突秽五,和上面的com.android.support:appcompat-v7:28.0.0會產(chǎn)生沖突
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
//通過加入這段代碼,可以解決沖突
configurations.all {exclude group: 'com.android.support', module: 'support-v4'}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}