您好,登錄后才能下訂單哦!
這篇文章主要介紹android ViewPager如何實(shí)現(xiàn)一個(gè)無限輪播圖,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
首先我們需要建一個(gè)包,然后新建一個(gè)java類,名字隨便起
這個(gè)類我們需要隨便繼承自一個(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)的布局寫進(jìn)去
因?yàn)槲覀兪菫閂iewPager實(shí)現(xiàn)一個(gè)無限輪播的輪播圖,首先當(dāng)然是寫一個(gè)ViewPager,然后是一個(gè)上方的標(biāo)題,我們寫一個(gè)textview,因?yàn)橄胍捅閰^(qū)分開來,我們給背景設(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"//背景設(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 "//背景設(shè)為白色 android:textAlignment="center"//居中 /> <LinearLayout android:layout_centerHorizontal="true"//設(shè)為居中 android:layout_alignParentBottom="true"//設(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)效果就是這樣的
接下來就是把我們寫好的自定義布局綁定我們的自定義的類,因?yàn)槲覀兿胍獰o論調(diào)那個(gè)構(gòu)造方法最后像都讓他去調(diào)我們寫綁定的方法,所以我們要把其他方法里面的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)前類,this:當(dāng)前類,ture:確定綁定 LayoutInflater.from(context).inflate(R.layout.looper_pager,this,true); } }
下一步就是實(shí)驗(yàn)我們的自定義控件有沒有成功啦,
重新創(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里面綁定剛才寫好的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)了
以上是“android ViewPager如何實(shí)現(xiàn)一個(gè)無限輪播圖”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。