android?ViewPager實(shí)現(xiàn)一個(gè)無(wú)限輪播圖
上節(jié)我們實(shí)現(xiàn)了一個(gè)圖片可以無(wú)限滑動(dòng)的ViewPager,這一節(jié)我們需要自定義一個(gè)ViewPager來(lái)實(shí)現(xiàn)我們想要展現(xiàn)的布局
首先我們需要建一個(gè)包,然后新建一個(gè)java類(lèi),名字隨便起
這個(gè)類(lèi)我們需要隨便繼承自一個(gè)viewGroup就行,viewGroup就是可以存放子控件的view,我們的各種layout,比如LinearLayour或者RelativeLayout這種可以在里面放東西的view,而TextView或者ImageView這種只能放內(nèi)容而不能放其他view的就是普通view
然后我們選中三個(gè)構(gòu)造器
package com.example.viewpager.views; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout { public LooperPager(Context context) { super(context); } public LooperPager(Context context,@NonNull AbstractSet abstrs) { super(context, (AttributeSet) abstrs); } public LooperPager(Context context,@NonNull AbstractSet abstrs,int defStyleAttr) { super(context, (AttributeSet) abstrs,defStyleAttr); } }
然后我們?cè)谛陆ㄒ粋€(gè)layout文件把想要實(shí)現(xiàn)的布局寫(xiě)進(jìn)去
因?yàn)槲覀兪菫閂iewPager實(shí)現(xiàn)一個(gè)無(wú)限輪播的輪播圖,首先當(dāng)然是寫(xiě)一個(gè)ViewPager,然后是一個(gè)上方的標(biāo)題,我們寫(xiě)一個(gè)textview,因?yàn)橄胍捅閰^(qū)分開(kāi)來(lái),我們給背景設(shè)定為紅色,標(biāo)題設(shè)定為白色,然后把文字居中,最后因?yàn)槲覀兿胍獔D片在滑動(dòng)時(shí)下方有一排根據(jù)圖片數(shù)量顯示滑動(dòng)時(shí)代表圖片的標(biāo)志的樣式,我們?cè)O(shè)定一個(gè)在控件底部居中顯示的線性布局,然后再線性布局內(nèi)設(shè)定三個(gè)白色大小為5dp前后間隔為5dp的view
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="120dp" android:background="@color/colorAccent"http://背景設(shè)為紅色 android:orientation="vertical"> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是標(biāo)題" android:background="#ffffff "http://背景設(shè)為白色 android:textAlignment="center"http://居中 /> <LinearLayout android:layout_centerHorizontal="true"http://設(shè)為居中 android:layout_alignParentBottom="true"http://設(shè)為底部 android:layout_width="wrap_content" android:layout_height="wrap_content" > <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> </LinearLayout> </RelativeLayout>
實(shí)現(xiàn)效果就是這樣的
接下來(lái)就是把我們寫(xiě)好的自定義布局綁定我們的自定義的類(lèi),因?yàn)槲覀兿胍獰o(wú)論調(diào)那個(gè)構(gòu)造方法最后像都讓他去調(diào)我們寫(xiě)綁定的方法,所以我們要把其他方法里面的supper都改成this
package com.example.viewpager.views; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout { public LooperPager(Context context) { this(context,null); } public LooperPager(Context context,@NonNull AbstractSet abstrs) { this(context, abstrs,0); } public LooperPager(Context context,@NonNull AbstractSet abstrs,int defStyleAttr) { super(context, (AttributeSet) abstrs,defStyleAttr); //自定義布局綁定當(dāng)前類(lèi),this:當(dāng)前類(lèi),ture:確定綁定 LayoutInflater.from(context).inflate(R.layout.looper_pager,this,true); } }
下一步就是實(shí)驗(yàn)我們的自定義控件有沒(méi)有成功啦,
重新創(chuàng)建一個(gè)啟動(dòng)文件然后在再創(chuàng)建一個(gè)lauout文件
,這里我們右鍵剛才的looppager選擇
然后在新建的Layout文件里面粘貼設(shè)定好寬和高
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.viewpager.views.LooperPager android:layout_width="match_parent" android:layout_height="120dp"/> </RelativeLayout>
最后在我們新建的activity里面綁定剛才寫(xiě)好的layout文件
package com.example.viewpager; import android.os.Bundle; import android.os.PersistableBundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class supper_MainActivity extends AppCompatActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.supper_activity_main); } }
效果就實(shí)現(xiàn)了
但剛開(kāi)始寫(xiě)完之后程序打開(kāi)就報(bào)錯(cuò),我從凌晨一點(diǎn)開(kāi)始找錯(cuò)誤,找到兩點(diǎn)半發(fā)現(xiàn)布局文件里面的View寫(xiě)成小寫(xiě)view了,當(dāng)時(shí)的心情不是一般的酸爽.....................
到此這篇關(guān)于android ViewPager實(shí)現(xiàn)一個(gè)無(wú)限輪播圖的文章就介紹到這了,更多相關(guān)android ViewPager輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)隨手指移動(dòng)小球
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)隨手指移動(dòng)小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android利用ViewPager實(shí)現(xiàn)帶小圓球的圖片滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android利用ViewPager實(shí)現(xiàn)帶小圓球的圖片滑動(dòng),并且只有第一次安裝app時(shí)才出現(xiàn)歡迎界面具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android中深入學(xué)習(xí)對(duì)象的四種引用類(lèi)型
這篇文章主要介紹Android中深入學(xué)習(xí)對(duì)象的四種引用類(lèi)型,Java中,一切被視為對(duì)象,引用則是用來(lái)操縱對(duì)象的;在JDK1.2就把對(duì)象引用分為四種級(jí)別,從而使程序能更靈活控制它的生命周期,級(jí)別由高到底依次為強(qiáng)引用、軟引用、弱引用、虛引用,需要的朋友可以參考一下2021-10-10Android 實(shí)現(xiàn)自定義圓形進(jìn)度條的實(shí)例代碼
進(jìn)度條在Android中教程使用到,本文章向大家介紹一下Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,需要的朋友可以參考一下。2016-11-11