viewpager一屏显示多个view 并且可以预览下一个view

最近工作需要一个效果 就是需要展示一屏多个view  使用viewpager实现这个功能 可以预览下一个view的一部分 效果图如下:




代码如下:先上activity 

public class MyViewPagerActivity extends BaseActivity {

    private ViewPager viewPager;
    private MyViewPagerAdapter adapter;
    private List<Integer> list;
    private Context context;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        context = this;
        addData();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_magin)); //显示viewpager间距
        viewPager.setOffscreenPageLimit(3);
        adapter = new MyViewPagerAdapter(context, list);
        viewPager.setAdapter(adapter);
    }

    /***
     * 填充数据
     */
    private void addData() {
        list = new ArrayList<>();
        list.add(R.color.red);
        list.add(R.color.blue);
        list.add(R.color.white);
        list.add(R.color.colorPrimary);
    }

接着适配器:

package com.example.administrator.demo.viewpager;

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

import com.example.administrator.demo.R;

import java.util.List;

/**
 * Created by Administrator on 2017/6/7 0007.
 */

public class MyViewPagerAdapter extends PagerAdapter {
    private Context mContext;
    private List<Integer> mList;

    public MyViewPagerAdapter(Context context, List<Integer> list) {
        this.mContext = context;
        this.mList = list;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_viewpager, null);
        container.addView(view);  //这一步很关键 把当前的imageview添加到适配器上
        ImageView ivViewPager = (ImageView) view.findViewById(R.id.iv_viewpager);
        ivViewPager.setBackgroundResource(mList.get(position));
        Log.d("TAG", "position ==== " +mList.get(position));
        return view;
    }

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

    @Override
    public float getPageWidth(int position) {
        return (float) 1;
    }
}

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">


    <FrameLayout
        android:layout_marginTop="@dimen/margin_size_50"
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:layerType="software"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:clipChildren="false" />
    </FrameLayout>
</LinearLayout>


item布局:

<?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:orientation="vertical"
    >

    <ImageView
        android:id="@+id/iv_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/corners_bg"
        />

</LinearLayout>

done!!!!!!!!!!






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值