Android Viewpager 内外间距

Android使用Viewpager_内外边距


代码:

1、adapter:

java 复制代码
<?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="@drawable/button_bg"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <ImageView
        android:id="@+id/ivImg"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:textSize="24dp"
        android:textStyle="bold" />

</LinearLayout>
java 复制代码
public class VpAdapter extends PagerAdapter {

    private Context mContext;
    private List<VpModel> mList;

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

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

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

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

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_vp, container, false);
        ImageView img = itemView.findViewById(R.id.ivImg);
        TextView name = itemView.findViewById(R.id.tvName);
        Glide.with(mContext).load(mList.get(position).getUrl()).into(img);
        name.setText(mList.get(position).getName());
        container.addView(itemView);
        return itemView;
    }
}

2、UI

java 复制代码
<?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:clipChildren="false"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="VP Test"
        android:textSize="30dp" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="30dp"
        android:clipChildren="false" />

</LinearLayout>
java 复制代码
public class VpActivity extends ComponentActivity {

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

        List<VpModel> list = new ArrayList<>();
        list.add(new VpModel("https://st-gdx.dancf.com/gaodingx/1942/articles/0/20201117-160009-ff34.png","哦莫"));
        list.add(new VpModel("https://pic0.sucaisucai.com/11/50/11050520_2.jpg","伸手让世界闭嘴"));
        list.add(new VpModel("https://cdn.pixabay.com/photo/2015/03/18/11/02/sunset-679122_1280.jpg","街边的服务站"));
        ViewPager viewPager = findViewById(R.id.vp);
        viewPager.setOffscreenPageLimit(list.size());
        viewPager.setPageMargin(30);
        viewPager.setAdapter(new VpAdapter(this, list));
    }
}

3、数据类

java 复制代码
public class VpModel {

    private String url;
    private String name;

    public VpModel(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone4 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc4 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo4 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077004 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼4 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone4 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen4 天前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone4 天前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui