Android使用Viewpager_内外边距
代码:
1、adapter:
<?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>
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
<?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>
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、数据类
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;
}
}