写一个自定义android类上面放一个viewpage左右滑动显示图片下面放一个RecyclerView横向滚动显示viewpage的缩略图的例子
时间: 2024-02-12 12:09:21 浏览: 71
好的,以下是一个自定义的 Android 类示例,其中包含一个 ViewPager 和一个 RecyclerView,可以左右滑动显示图片,并在底部显示 ViewPager 的缩略图。
首先,在 XML 布局文件中定义自定义类 CustomView:
```xml
<com.example.myapplication.CustomView
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
然后,在 CustomView 类中添加 ViewPager 和 RecyclerView:
```java
public class CustomView extends FrameLayout {
private ViewPager viewPager;
private RecyclerView recyclerView;
public CustomView(@NonNull Context context) {
super(context);
init();
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
View view = LayoutInflater.from(getContext()).inflate(R.layout.custom_view, this, true);
viewPager = view.findViewById(R.id.view_pager);
recyclerView = view.findViewById(R.id.recycler_view);
// 设置 ViewPager 的 Adapter
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getContext());
viewPager.setAdapter(pagerAdapter);
// 设置 RecyclerView 的 Adapter 和 LayoutManager
MyRecyclerViewAdapter recyclerViewAdapter = new MyRecyclerViewAdapter(getContext());
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(layoutManager);
}
}
```
接下来,定义 MyPagerAdapter 和 MyRecyclerViewAdapter 类:
```java
public class MyPagerAdapter extends PagerAdapter {
private Context context;
public MyPagerAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.image1 + position);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return 5;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private Context context;
public MyRecyclerViewAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(R.drawable.image1 + position);
}
@Override
public int getItemCount() {
return 5;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
}
}
}
```
最后,在 XML 布局文件中定义 ViewPager 和 RecyclerView 的布局:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
```
这样,CustomView 类就包含了一个 ViewPager 和一个 RecyclerView,可以左右滑动显示图片,并在底部显示 ViewPager 的缩略图。
阅读全文