类似淘宝的商品图片滑动展示效果,viewpager+pageadapter+view。超简单极简版!!!

岁酱吖の 提交于 2020-02-23 04:36:28

灵光一闪,瞎写一通,竟然可以!

先上效果:

部分内容参考博客:

https://blog.csdn.net/u011508817/article/details/80670739

https://www.cnblogs.com/tinyphp/p/3890769.html

太简单懒得解释了,直接上代码,不懂就问。

package com.xxx.yyy.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xxx.library.basecore.base.activity.BaseActivity;
import com.xxx.yyy.R;

import java.util.ArrayList;

public class PicViewerActivity extends BaseActivity{
    private TextView count;
    private ImageView cancel;
    private ViewPager viewPager;
    private ArrayList<View> pageview;
    private int[] imgs = new int[]{R.drawable.a1,R.drawable.personal_info_icon,R.drawable.icon_add_clife};

    public static void startActivity(Context context){
        Intent intent = new Intent(context,PicViewerActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_pic_viewer_layout;
    }

    @Override
    protected void attachWidget() {
        removeTitle();
        count = findViewById(R.id.count);
        cancel = findViewById(R.id.cancel);
        viewPager = findViewById(R.id.viewpager);
        pageview =new ArrayList<View>();
        //查找布局文件用LayoutInflater.inflate
        LayoutInflater inflater =getLayoutInflater();

        for(int i = 0;i<imgs.length;i++){
            View view = inflater.inflate(R.layout.pic_viewer,null);
            ImageView imageView = view.findViewById(R.id.img);
            imageView.setBackgroundResource(imgs[i]);
            pageview.add(imageView);
        }

        //数据适配器
        PagerAdapter mPagerAdapter = new PagerAdapter(){

            @Override
            //获取当前窗体界面数
            public int getCount() {
                // TODO Auto-generated method stub
                return pageview.size();
            }

            @Override
            //断是否由对象生成界面
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return arg0==arg1;
            }
            //是从ViewGroup中移出当前View
            public void destroyItem(View arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView(pageview.get(arg1));
            }

            //返回一个对象,这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
            public Object instantiateItem(View arg0, int arg1){
                ((ViewPager)arg0).addView(pageview.get(arg1));
                return pageview.get(arg1);
            }


        };

        //绑定适配器
        viewPager.setAdapter(mPagerAdapter);

    }

    @Override
    protected void initParams(Bundle bundle) {
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int a = position+1;
                count.setText(a+"/"+imgs.length);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

布局:

<?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:background="@color/black"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="40dp"
        android:background="@mipmap/cancel"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="351dp"
        android:layout_height="351dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="100dp"
        android:layout_gravity="center">
    </android.support.v4.view.ViewPager>

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:text="3/10"
        android:textColor="#ff999999"
        android:textSize="14sp"/>

</LinearLayout>

view布局:

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

</ImageView>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!