android?ViewPager實現(xiàn)一個無限輪播圖
上節(jié)我們實現(xiàn)了一個圖片可以無限滑動的ViewPager,這一節(jié)我們需要自定義一個ViewPager來實現(xiàn)我們想要展現(xiàn)的布局
首先我們需要建一個包,然后新建一個java類,名字隨便起
這個類我們需要隨便繼承自一個viewGroup就行,viewGroup就是可以存放子控件的view,我們的各種layout,比如LinearLayour或者RelativeLayout這種可以在里面放東西的view,而TextView或者ImageView這種只能放內(nèi)容而不能放其他view的就是普通view
然后我們選中三個構(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); } }
然后我們在新建一個layout文件把想要實現(xiàn)的布局寫進去
因為我們是為ViewPager實現(xiàn)一個無限輪播的輪播圖,首先當(dāng)然是寫一個ViewPager,然后是一個上方的標(biāo)題,我們寫一個textview,因為想要和悲情區(qū)分開來,我們給背景設(shè)定為紅色,標(biāo)題設(shè)定為白色,然后把文字居中,最后因為我們想要圖片在滑動時下方有一排根據(jù)圖片數(shù)量顯示滑動時代表圖片的標(biāo)志的樣式,我們設(shè)定一個在控件底部居中顯示的線性布局,然后再線性布局內(nèi)設(shè)定三個白色大小為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>
實現(xiàn)效果就是這樣的
接下來就是把我們寫好的自定義布局綁定我們的自定義的類,因為我們想要無論調(diào)那個構(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); } }
下一步就是實驗我們的自定義控件有沒有成功啦,
重新創(chuàng)建一個啟動文件然后在再創(chuàng)建一個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); } }
效果就實現(xiàn)了
但剛開始寫完之后程序打開就報錯,我從凌晨一點開始找錯誤,找到兩點半發(fā)現(xiàn)布局文件里面的View寫成小寫view了,當(dāng)時的心情不是一般的酸爽.....................
到此這篇關(guān)于android ViewPager實現(xiàn)一個無限輪播圖的文章就介紹到這了,更多相關(guān)android ViewPager輪播圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android利用ViewPager實現(xiàn)帶小圓球的圖片滑動
這篇文章主要為大家詳細介紹了Android利用ViewPager實現(xiàn)帶小圓球的圖片滑動,并且只有第一次安裝app時才出現(xiàn)歡迎界面具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Android中深入學(xué)習(xí)對象的四種引用類型
這篇文章主要介紹Android中深入學(xué)習(xí)對象的四種引用類型,Java中,一切被視為對象,引用則是用來操縱對象的;在JDK1.2就把對象引用分為四種級別,從而使程序能更靈活控制它的生命周期,級別由高到底依次為強引用、軟引用、弱引用、虛引用,需要的朋友可以參考一下2021-10-10