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

Android ViewPager實現(xiàn)滑動指示條功能

 更新時間:2020年10月21日 10:28:54   作者:Red&&Black  
這篇文章主要介紹了Android-ViewPager實現(xiàn)滑動指示條功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

布局

activity_main.xml

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


  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="48dp"
      tools:ignore="MissingConstraints" android:id="@+id/linearLayout">

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="黃色"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/tv_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="紅色"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/tv_three"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:gravity="center"
        android:text="綠色"
        android:textColor="#000000"/>
  </LinearLayout>

  <ImageView
      android:id="@+id/img_cursor"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:scaleType="matrix"
      android:src="@mipmap/line"
      />

  <android.support.v4.view.ViewPager
      android:id="@+id/vp_show"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" tools:ignore="MissingConstraints"/>

</LinearLayout>

view1.xml view2.xml… 基本相同

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:background="@color/colorAccent"
       android:layout_height="match_parent">
  <View
      android:id="@+id/vp_text"
      android:text="page 2"

      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>

素材line.png

在這里插入圖片描述

注意,由于api版本不同,android.support.v4.view.ViewPager可能報錯,
更換為androidx.viewpager.widget.ViewPager

java代碼

MyPagerAdapter類

package com.demo;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

public class MyPagerAdapter extends PagerAdapter {

  private ArrayList<View> viewLists;


  public MyPagerAdapter() {
  }

  public MyPagerAdapter(ArrayList<View> viewLists) {
    this.viewLists = viewLists;
  }

  @Override
  public int getCount() {
    return viewLists.size();
  }

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

  @NonNull
  @Override
  public Object instantiateItem(@NonNull ViewGroup container, int position) {

    container.addView(viewLists.get(position));
    return viewLists.get(position);
  }

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

MainActivity類

package com.demo;

import android.annotation.SuppressLint;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,
    ViewPager.OnPageChangeListener{

  private ViewPager pager1;
  private ArrayList<View> list;
  private MyPagerAdapter myPagerAdapter;


  private ImageView img_cursor;
  private TextView tv_one;
  private TextView tv_two;
  private TextView tv_three;
  private int offSet = 0; //移動條圖片的偏移量
  private int currIndex = 0; //當前頁
  private int imageWidth = 0; //移動條圖片的寬度
  private int one = 0;  //移動條滑動一頁的距離
  private int two = 0;  //移動條滑動兩頁的距離

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    init();

  }

  private void init() {
    tv_one = findViewById(R.id.tv_one);
    tv_two = findViewById(R.id.tv_two);
    tv_three = findViewById(R.id.tv_three);
    img_cursor =findViewById(R.id.img_cursor);

    /**
     * 下劃線動畫的相關(guān)設置
     */
    //獲取圖片的寬度
    imageWidth = BitmapFactory.decodeResource(getResources(),R.mipmap.line).getWidth();


    DisplayMetrics dm = new DisplayMetrics();
    //獲取手機屏幕寬度
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenW = dm.widthPixels;
    offSet = (screenW/3 - imageWidth) /2 ; //計算偏移量

    Matrix matrix = new Matrix();
    matrix.postTranslate(offSet,0);

    img_cursor.setImageMatrix(matrix);//設置動畫初始位置

    one = offSet *2 +imageWidth;
    two = one*2;

    pager1 = findViewById(R.id.vp_show);

    //往ViewPager填充View,同時設置點擊事件與頁面切換事件
    list = new ArrayList<>();
    LayoutInflater li = getLayoutInflater();

    list.add(li.inflate(R.layout.view3,null,false));
    list.add(li.inflate(R.layout.view1,null,false));
    list.add(li.inflate(R.layout.view2,null,false));

    myPagerAdapter= new MyPagerAdapter(list);
    pager1.setAdapter(myPagerAdapter);
    pager1.setCurrentItem(0);     //設置ViewPager當前頁,從0開始算

    tv_one.setOnClickListener(this);
    tv_two.setOnClickListener(this);
    tv_three.setOnClickListener(this);

    pager1.addOnPageChangeListener(this);

  }

  @Override
  public void onPageScrolled(int i, float v, int i1) {

  }

  @Override
  public void onPageSelected(int i) {
    Animation animation = null;
    switch (i){

      case 0:
        if (currIndex==1) {
          animation = new TranslateAnimation(one,0,0,0);
        }
        else if (currIndex==2) {
          animation = new TranslateAnimation(two,0,0,0);
        }
        break;
      case 1:
        if (currIndex==0) {
          animation = new TranslateAnimation(offSet,one,0,0);
        }
        else if (currIndex==2) {
          animation = new TranslateAnimation(two,one,0,0);
        }
        break;
      case 2:
        if (currIndex==1) {
          animation = new TranslateAnimation(one,two,0,0);
        }
        else if (currIndex==0) {
          animation = new TranslateAnimation(offSet,two,0,0);
        }
        break;
    }

    currIndex = i;
    animation.setFillAfter(true); // true表示圖片停在動畫結(jié)束位置
    animation.setDuration(300); //設置動畫時間
    img_cursor.startAnimation(animation); //開始動畫

  }

  @Override
  public void onPageScrollStateChanged(int i) {

  }

  @Override
  public void onClick(View view) {

    switch (view.getId()) {
      case R.id.tv_one: pager1.setCurrentItem(0);break;
      case R.id.tv_two: pager1.setCurrentItem(1);break;
      case R.id.tv_three: pager1.setCurrentItem(2);break;
    }

  }
}

效果

在這里插入圖片描述

到此這篇關(guān)于Android ViewPager實現(xiàn)滑動指示條功能的文章就介紹到這了,更多相關(guān)Android滑動指示條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android使用GestureOverlayView控件實現(xiàn)手勢識別

    Android使用GestureOverlayView控件實現(xiàn)手勢識別

    這篇文章主要為大家詳細介紹了Android使用GestureOverlayView控件實現(xiàn)手勢識別,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android ProgressBar組件使用教程

    Android ProgressBar組件使用教程

    Android ProgressBar分為水平進度條和圓形進度條, 看官方的劃分是Indeterminate Progress(不確定的進度) 和 Determinate Progress(決定進度)下面有2個demo一個是圓形的進度條和一個水平的進度條
    2022-11-11
  • Android實現(xiàn)帶附件的郵件發(fā)送功能

    Android實現(xiàn)帶附件的郵件發(fā)送功能

    這篇文章主要介紹了Android實現(xiàn)帶附件的郵件發(fā)送功能的相關(guān)資料,android發(fā)送郵件有兩種方式,本文重點介紹基于JMail實現(xiàn)郵件發(fā)送功能,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 實例解析Android ImageView的scaleType屬性

    實例解析Android ImageView的scaleType屬性

    通過本文給大家介紹ImageView這個控件的一些使用方法,以及其最重要的一個屬性: scaleType,對imageview的scaletype相關(guān)知識感興趣的朋友一起學習吧
    2016-01-01
  • Android仿網(wǎng)易嚴選底部彈出菜單效果

    Android仿網(wǎng)易嚴選底部彈出菜單效果

    這篇文章主要為大家詳細介紹了Android仿網(wǎng)易嚴選底部彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android使用AudioRecord實現(xiàn)錄音功能

    Android使用AudioRecord實現(xiàn)錄音功能

    這篇文章主要為大家詳細介紹了Android使用AudioRecord實現(xiàn)錄音功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android注解框架對比分析

    Android注解框架對比分析

    這篇文章主要為大家詳細對比分析了Android注解框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • Android學習教程之高仿安卓微信6.0(2)

    Android學習教程之高仿安卓微信6.0(2)

    這篇文章主要為大家詳細介紹了Android學習教程之高仿安卓微信6.0的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實現(xiàn)微信支付功能

    Android實現(xiàn)微信支付功能

    這篇文章主要介紹了Android實現(xiàn)微信支付功能,微信支付功能,幾乎成為很多APP付款的主要方式之一,本文講解Android APP微信支付功能開發(fā),感興趣的小伙伴們可以參考一下
    2016-02-02
  • RecyclerView中監(jiān)聽EditText變化的BUG的解決方法

    RecyclerView中監(jiān)聽EditText變化的BUG的解決方法

    本篇文章主要介紹了RecyclerView中監(jiān)聽EditText變化的BUG的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論

米林县| 盐城市| 湘乡市| 珠海市| 康平县| 四平市| 平乡县| 漠河县| 五河县| 三明市| 南陵县| 靖安县| 余姚市| 朝阳县| 湘西| 揭东县| 钟山县| 许昌市| 土默特左旗| 临清市| 天镇县| 宜宾县| 津南区| 花垣县| 红河县| 平定县| 肥东县| 玛多县| 贵溪市| 林周县| 上栗县| 彝良县| 福鼎市| 五华县| 灵石县| 大庆市| 天等县| 镇原县| 满城县| 密山市| 平泉县|