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;
    }
}
相关推荐
_Shirley1 小时前
鸿蒙设置app更新跳转华为市场
android·华为·kotlin·harmonyos·鸿蒙
hedalei3 小时前
RK3576 Android14编译OTA包提示java.lang.UnsupportedClassVersionError问题
android·android14·rk3576
锋风Fengfeng3 小时前
安卓多渠道apk配置不同签名
android
枫_feng3 小时前
AOSP开发环境配置
android·安卓
叶羽西4 小时前
Android Studio打开一个外部的Android app程序
android·ide·android studio
qq_171538855 小时前
利用Spring Cloud Gateway Predicate优化微服务路由策略
android·javascript·微服务
Vincent(朱志强)6 小时前
设计模式详解(十二):单例模式——Singleton
android·单例模式·设计模式
mmsx7 小时前
android 登录界面编写
android·登录界面
姜毛毛-JYM7 小时前
【JetPack】Navigation知识点总结
android
花生糖@8 小时前
Android XR 应用程序开发 | 从 Unity 6 开发准备到应用程序构建的步骤
android·unity·xr·android xr