viewpager实现画廊(中间图片全部显示,左右显示一部分b布局)无限轮播效果「建议收藏」

viewpager实现画廊(中间图片全部显示,左右显示一部分b布局)无限轮播效果「建议收藏」一、布局xmlversion=”1.0″encoding=”utf-8″?>RelativeLayoutxmlns:android=”http://schemas.android.com/apk/res/android”    android:id=”@+id/pager_layout”    android:layout_width=”match_

大家好,又见面了,我是你们的朋友全栈君。

一、布局

<LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:orientation="vertical"  android:layout_marginTop="10dp"  android:gravity="center_horizontal"  android:background="#FFFFFF"  android:clipChildren="false"  android:layerType="software"  >

<android.support.v4.view.ViewPager  android:id="@+id/vp"  android:layout_width="300dp"  android:layout_height="220dp"  android:layout_gravity="center"  android:clipChildren="false"  >

</android.support.v4.view.ViewPager>
</LinearLayout>

发现上面的RelativeLayout(可以用其它layout替换)和ViewPager的android:clipChildren都设置为了false。

android:clipChildren表示是否限制子View在其范围内,在animations动画以及本文的情况下可以发挥很大的作用。默认为true。

二、适配器

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.baway.chilijie.bean.Data;
import com.bumptech.glide.Glide;

import java.util.List;

/**  * Created by MacBook- on 2017/4/13.  */ public class MyViewpagerAdapter extends PagerAdapter {
    private List<Data.MyInfo.MyInfoList> myInfoLists;
    private Context context;
    private int xinWidth;
    private int xinHeight;

    public MyViewpagerAdapter(List<Data.MyInfo.MyInfoList> myInfoLists, Context context) {
       this.context=context;
        this.myInfoLists=myInfoLists;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView vp_iv= new ImageView(context);
        Glide.with(context).load(myInfoLists.get(position%myInfoLists.size()).getActivityImg()).into(vp_iv);
        container.addView(vp_iv);
        return vp_iv;
    }

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

三、自定义
PageTransformer类

import android.support.v4.view.ViewPager;
import android.view.View;

/**  * Created by MacBook- on 2017/4/14.  */ public class MyGallyPageTransformer implements ViewPager.PageTransformer {
    private static final float min_scale = 0.85f;
    @Override
    public void transformPage(View page, float position) {
        float scaleFactor = Math.max(min_scale, 1 - Math.abs(position));
        if (position < -1) {
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        } else if (position < 0) {
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        } else if (position >= 0 && position < 1) {
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        } else if (position >= 1) {
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        }
    }
}

四、Activity

public class MainActivity extends AppCompatActivity{
  
  
private ViewPager vp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
vp.setOffscreenPageLimit(3);
int pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels * 3.0f / 5.0f);
ViewGroup.LayoutParams lp = vp.getLayoutParams();
if (lp == null) {
    lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
} else {
    lp.width = pagerWidth;
}
vp.setLayoutParams(lp);
//setPageMargin表示设置图片之间的间距 vp.setPageMargin(getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin));
vp.setPageTransformer(true,new MyGallyPageTransformer());
vp.setAdapter(new MyViewpagerAdapter(data.getActivityInfo().getActivityInfoList(),getActivity()));
vp.setCurrentItem(2000);

} }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134031.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JPA 逻辑删除

    JPA 逻辑删除为了避免每次在查询的时候加上是否删除字段,做了一下封装。@NoRepositoryBean@RepositoryRestResource(exported=false)publicinterfaceBaseRepository<TextendsBaseEntity,IdTextendsLong>extendsJpaRepository<T…

    2022年6月2日
    295
  • VMware安装win10反复出现 BootManager,vmware无法安装win10,无法引导

    VMware安装win10反复出现 BootManager,vmware无法安装win10,无法引导VMwareworkstation分为pro版本和player版本,电脑一直用VMwareworkstationplayer,轻便免费很不错,但是今天准备安装win10系统,竟安装不上,反复出现BootManager。以为是软件版本的问题,从vmware15升级到16问题仍然存在。经过查询别人通过修改固件类型为bios可以解决这个问题(40条消息)vmware安装win10镜像出现bootManager_☆☆★☆☆的博客-CSDN博客但是我的版本是VMwareworkst

    2022年6月18日
    173
  • qtcpsocket write_c文件读写

    qtcpsocket write_c文件读写QTcpSocket的读写函数一般是异步的,即write函数执行后,其实不一定会把数据写入socket,可能要等到事件循环(main函数的returna.exec())才会写入。如果需要立即写入,要执行QTcpSocket的flush函数。QTcpSocket的读函数也类似,我们定义一个槽函数voidslot_readyRead响应信号readyRead,槽函数中,能够读取的数据,也只有这

    2025年10月15日
    5
  • 制作简单的贺卡_bootfs和rootfs

    制作简单的贺卡_bootfs和rootfs分析简单的根文件系统中所必须的文件1.1在嵌入式系统中的根文件系统与桌面版的根文件系统文件基本上类似,所以用Ubuntu中根文件系统问模板,进行分析:1.1.1、bin sbin  linuxrc  是必须的,但是这三个目录以及里面的文件都是移植busybox并安装的时候由busybox生成的1.1.2、etc  是很关键很重要的一个。目录中的文件都是运行时配置文件,都是直接…

    2022年10月6日
    3
  • 微表情识别

    表情识别2019.12更新了仓库依赖。简介使用卷积神经网络构建整个系统,在尝试了Gabor、LBP等传统人脸特征提取方式基础上,深度模型效果显著。在FER2013、JAFFE和CK+三个表情识别数据集上进行模型评估。环境部署基于Python3和Keras2(TensorFlow后端),具体依赖安装如下(推荐使用conda或者venv虚拟环境)gitclonehttp…

    2022年4月9日
    61
  • 通达OA工作流-流程设计

    通达OA工作流-流程设计2.2    流程设计  2.2.1    流程分类  在工作流工作流设置分类设置可以为系统添加流程分类。流程分类更方便了流程的管理,把不同性质的流程放在不同的分类下,也方便了流程的查找。 同时根据流程分类的所属部门,实现了流程分类按部门进行独立管理的目的。

    2022年6月23日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号