試試這個,你可以使用android.support.v4.view.ViewPager
佈局管理器,允許用戶翻轉升通過網頁的數據。您提供了一個PagerAdapter的實現來生成該視圖顯示的頁面。
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
演示代碼
ViewPager viewPager;
ArrayList<String> imageArray;
imageArray = new ArrayList<>();
viewPager = findViewById(R.id.cspl_viewPager);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
imageArray.add(R.drawable.bg);
ImageAdapter adapter = new ImageAdapter(this, imageArray);
viewPager.setAdapter(adapter);
現在創建ImageAdapter
這樣
public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> imageArray;
public ImageAdapter(Context context, ArrayList<String> imageArray) {
this.context = context;
this.imageArray = imageArray;
}
@Override
public int getCount() {
return imageArray.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, 50);
imageView.setLayoutParams(layoutParams);
int padding = context.getResources().getDimensionPixelSize(R.dimen.font_size_10);
imageView.setPadding(padding, padding, padding, padding);
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Glide.with(ProfileActivity.this)
.load(R.drawable.bg)
.into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
謝謝你:) – MDP
最受歡迎@MDP –