最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android?ViewPager實現(xiàn)一個無限輪播圖

 更新時間:2022年02月03日 09:24:29   作者:菜的一嘰  
大家好,本篇文章主要講的是android?ViewPager實現(xiàn)一個無限輪播圖,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下

上節(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)文章

最新評論

华池县| 灵台县| 南汇区| 嘉义市| 白银市| 通辽市| 海阳市| 乌兰浩特市| 策勒县| 茶陵县| 宜城市| 平泉县| 平泉县| 武邑县| 石棉县| 濉溪县| 六盘水市| 芜湖县| 库车县| 昌黎县| 长顺县| 射洪县| 尚义县| 海丰县| 乃东县| 如皋市| 罗甸县| 五峰| 鄂托克旗| 涟源市| 苗栗县| 岚皋县| 工布江达县| 普宁市| 鄂州市| 固原市| 屯门区| 平利县| 澳门| 甘洛县| 盐边县|