写一个自定义android类上面放一个viewpage左右滑动显示图片下面放一个gridview横向滚动显示viewpage的缩略图的例子
时间: 2024-02-12 17:05:25 浏览: 127
好的,这是一个自定义的 Android 类,其中包含一个 ViewPager 和一个 GridView,ViewPager 可以左右滑动显示图片,GridView 可以横向滚动显示 ViewPager 的缩略图。
首先,我们创建一个 XML 布局文件,命名为 custom_view.xml,其中包含一个 ViewPager 和一个 GridView:
```xml
<?xml version="1.0" encoding="utf-8"?>
<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" />
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:padding="10dp"
android:verticalSpacing="10dp" />
</RelativeLayout>
```
然后,我们创建一个 CustomView 类,继承自 RelativeLayout,并在其中初始化 ViewPager 和 GridView:
```java
public class CustomView extends RelativeLayout {
private ViewPager mViewPager;
private GridView mGridView;
public CustomView(Context context) {
super(context);
initView();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.custom_view, this, true);
mViewPager = view.findViewById(R.id.view_pager);
mGridView = view.findViewById(R.id.grid_view);
// 初始化 ViewPager 和 GridView
// ...
}
}
```
接下来,我们可以在 initView() 方法中初始化 ViewPager 和 GridView。首先,我们创建一个 PagerAdapter,用于为 ViewPager 提供数据:
```java
private void initView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.custom_view, this, true);
mViewPager = view.findViewById(R.id.view_pager);
mGridView = view.findViewById(R.id.grid_view);
// 初始化 ViewPager 和 GridView
List<Integer> data = new ArrayList<>();
data.add(R.drawable.image1);
data.add(R.drawable.image2);
data.add(R.drawable.image3);
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(getContext());
imageView.setImageResource(data.get(position));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
};
mViewPager.setAdapter(pagerAdapter);
}
```
然后,我们创建一个 ImageAdapter,用于为 GridView 提供数据:
```java
private void initView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.custom_view, this, true);
mViewPager = view.findViewById(R.id.view_pager);
mGridView = view.findViewById(R.id.grid_view);
// 初始化 ViewPager 和 GridView
List<Integer> data = new ArrayList<>();
data.add(R.drawable.image1);
data.add(R.drawable.image2);
data.add(R.drawable.image3);
PagerAdapter pagerAdapter = new PagerAdapter() {
// ...
};
mViewPager.setAdapter(pagerAdapter);
ImageAdapter imageAdapter = new ImageAdapter(data);
mGridView.setAdapter(imageAdapter);
}
```
最后,我们创建一个 ImageAdapter 类,继承自 BaseAdapter,用于为 GridView 提供数据:
```java
private void initView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.custom_view, this, true);
mViewPager = view.findViewById(R.id.view_pager);
mGridView = view.findViewById(R.id.grid_view);
// 初始化 ViewPager 和 GridView
List<Integer> data = new ArrayList<>();
data.add(R.drawable.image1);
data.add(R.drawable.image2);
data.add(R.drawable.image3);
PagerAdapter pagerAdapter = new PagerAdapter() {
// ...
};
mViewPager.setAdapter(pagerAdapter);
ImageAdapter imageAdapter = new ImageAdapter(data);
mGridView.setAdapter(imageAdapter);
}
private class ImageAdapter extends BaseAdapter {
private List<Integer> mData;
public ImageAdapter(List<Integer> data) {
mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(getContext());
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mData.get(position));
return imageView;
}
}
```
现在,我们就可以在项目中使用 CustomView 类来显示 ViewPager 和 GridView,例如:
```xml
<com.example.myapplication.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
阅读全文