Android ViewPager 미끄럼 표시 줄 기능 구현

9735 단어 Android슬라이더
배치
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자바 코드
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);

    /**
     *           
     */
    //       
    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            
    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;
    }

  }
}
효과.
在这里插入图片描述
안 드 로 이 드 뷰 퍼 가 슬라이더 기능 을 수행 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 슬라이더 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기