欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 滑動(dòng)小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程

 更新時(shí)間:2021年11月09日 08:49:06   作者:彬sir哥  
Viewpager,視圖翻頁工具,提供了多頁面切換的效果。Android 3.0后引入的一個(gè)UI控件,位于v4包中。低版本使用需要導(dǎo)入v4包,現(xiàn)在我們一般不再兼容3.0及以下版本,另外使用Android studio開發(fā),默認(rèn)導(dǎo)入v7包,v7包含了v4,所以不用導(dǎo)包,越來越方便了

第一種方法:

一、測(cè)試如下,直接設(shè)置小圓點(diǎn)不是圖標(biāo)

二、準(zhǔn)備工作

1.在drawable創(chuàng)建dot.xml,設(shè)置小圓點(diǎn),比較方便

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape android:shape="oval">
            <solid android:color="@color/black" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <item android:state_selected="false">
        <shape android:shape="oval">
            <solid android:color="@color/white" />
            <corners android:radius="8dp" />
        </shape>
    </item>
</selector>

2.布局小圓點(diǎn)的狀態(tài)可以被左右滑動(dòng)dotview.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <ImageView
        android:id="@+id/v_dot"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/dot"/>
</LinearLayout>

3.分頁適配器自定義ViewPagerAdapter.java

public class ViewPagerAdapter extends PagerAdapter {
    private List<View> mViewList;

    public ViewPagerAdapter(List<View> mViewList) {
        this.mViewList = mViewList;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mViewList.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mViewList.get(position));
        return (mViewList.get(position));
    }

    @Override
    public int getCount() {
        if (mViewList == null)
            return 0;
        return mViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}

三、使用工作:

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D4D3D3">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:id="@+id/ll_dots"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:orientation="horizontal" />
    </RelativeLayout>
</LinearLayout>

分析下布局結(jié)構(gòu):
(1)首先是一個(gè)ViewPager,用于結(jié)合GridView實(shí)現(xiàn)左右滑動(dòng)菜單
(2)下面是一個(gè)LinearLayout,有多少個(gè)ViewPager的頁面就會(huì)inflate出多少個(gè)小圓點(diǎn),并且在ViewPager翻頁時(shí),也就是說在左右滑動(dòng)時(shí),下面小圓點(diǎn)的狀態(tài)也要相應(yīng)地做出改變
2.MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ViewPager mPager;
    private LinearLayout mLlDots;
    private LayoutInflater inflater;
    private List<View> mPagerList;
    private int pageCount = 3;//默認(rèn)三個(gè)小點(diǎn)
    /**
     * 當(dāng)前顯示的是第幾頁
     */
    private int curIndex = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ViewPager) findViewById(R.id.viewpager);
        mLlDots = (LinearLayout) findViewById(R.id.ll_dots);
        inflater = LayoutInflater.from(this);
        mPagerList = new ArrayList<View>();
        //#FF9800:黃,#4CAF50:綠,#2196F3:藍(lán)
        String[] colors = {"#FF9800", "#4CAF50", "#2196F3"};
        for (int i = 0; i < pageCount; i++) {
            LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);
            mLL.setBackgroundColor(Color.parseColor(colors[i]));
            mPagerList.add(mLL);
        }
        //設(shè)置適配器
        mPager.setAdapter(new ViewPagerAdapter(mPagerList));
        //設(shè)置圓點(diǎn)
        setDotLayout();
    }

    /**
     * 設(shè)置圓點(diǎn)
     */
    public void setDotLayout() {
        for (int i = 0; i < pageCount; i++) {
            mLlDots.addView(inflater.inflate(R.layout.dotview, null));
        }
        // 默認(rèn)顯示第一頁
        mLlDots.getChildAt(0).setSelected(true);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 取消原點(diǎn)選中
                mLlDots.getChildAt(curIndex).setSelected(false);
                // 原點(diǎn)選中
                mLlDots.getChildAt(position).setSelected(true);
                curIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

這代碼中一句,布局LinearLayout隨著左右滑動(dòng)小圓點(diǎn)翻頁

LinearLayout mLL = (LinearLayout) inflater.inflate(R.layout.linearlayout, mPager, false);

布局linearlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

第二種方法:

一、測(cè)試如下,小圓點(diǎn)圖標(biāo)

二、dotview.xml

......
<ImageView
        android:id="@+id/v_dot"
        android:layout_width="10dp"
        android:layout_height="10dp"/>
......

三、設(shè)置二個(gè)小圓點(diǎn)圖標(biāo)(黑白)

點(diǎn)擊鏈接:二個(gè)小圓點(diǎn)圖標(biāo).zip

        // 默認(rèn)顯示第一頁
        mLlDots.getChildAt(0).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_normal);

        mLlDots.getChildAt(1).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);

        mLlDots.getChildAt(2).findViewById(R.id.v_dot)
                .setBackgroundResource(R.drawable.dot_selected);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageSelected(int position) {
                // 取消圓點(diǎn)選中
                mLlDots.getChildAt(curIndex)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_selected);
                // 圓點(diǎn)選中
                mLlDots.getChildAt(position)
                        .findViewById(R.id.v_dot)
                        .setBackgroundResource(R.drawable.dot_normal);
                curIndex = position;
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });

到此這篇關(guān)于Android 滑動(dòng)小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程的文章就介紹到這了,更多相關(guān)Android 滑動(dòng)小圓點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論