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

android?ViewPager實(shí)現(xiàn)一個(gè)無(wú)限輪播圖

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

上節(jié)我們實(shí)現(xiàn)了一個(gè)圖片可以無(wú)限滑動(dòng)的ViewPager,這一節(jié)我們需要自定義一個(gè)ViewPager來實(shí)現(xiàn)我們想要展現(xiàn)的布局

首先我們需要建一個(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è)無(wú)限輪播的輪播圖,首先當(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"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)效果就是這樣的

 接下來就是把我們寫好的自定義布局綁定我們的自定義的類,因?yàn)槲覀兿胍獰o(wú)論調(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)了 

但剛開始寫完之后程序打開就報(bào)錯(cuò),我從凌晨一點(diǎn)開始找錯(cuò)誤,找到兩點(diǎn)半發(fā)現(xiàn)布局文件里面的View寫成小寫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)文章

最新評(píng)論

南靖县| 延吉市| 中卫市| 平乐县| 三门峡市| 梁河县| 广安市| 巴塘县| 福贡县| 团风县| 滦南县| 门源| 长春市| 忻州市| 金昌市| 杭锦后旗| 上高县| 阳信县| 梁平县| 郓城县| 高雄市| 金门县| 晴隆县| 墨玉县| 兴宁市| 类乌齐县| 广灵县| 渭南市| 永善县| 连江县| 达孜县| 乌审旗| 南丹县| 汉沽区| 沽源县| 无为县| 遂溪县| 周至县| 若尔盖县| 武功县| 越西县|