gpt4 book ai didi

android - 如何在 Android 上开发图像轮播?

转载 作者:太空狗 更新时间:2023-10-29 15:33:22 25 4
gpt4 key购买 nike

我正在尝试在 Android 屏幕的上半部分放置一个旋转木马。

我喜欢 Bootstrap 轮播,例如:

http://www.w3schools.com/bootstrap/bootstrap_carousel.asp

我不确定处理此问题的最佳方法?我是否在屏幕的上半部分使用 GridView??

最佳答案

首先,您必须在您的 xml 中创建一个 Viewpager,例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/relativeLayout">


<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

</RelativeLayout>

对于自定义 Adapter,您需要创建一个 res/layout/pager_item.xml,它将用于膨胀并生成 Adapter 的 View :

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />

</LinearLayout>

之后,您可以创建自定义 Adapter 来设置您的 ViewPager:

public class CustomPagerAdapter extends PagerAdapter {

private Context mContext;
private LayoutInflater mLayoutInflater;
private int[] mResources;

public CustomPagerAdapter(Context context, int[] resources) {
mContext = context;
mResources = resources;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return mResources.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);

ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);

container.addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}

}

然后将以下代码放入Activity中的onCreate:

mViewPager = (ViewPager) findViewById(R.id.pager);

// This is just an example. You can use whatever collection of images.
int[] mResources = {
R.drawable.first,
R.drawable.second,
R.drawable.third,
R.drawable.fourth,
R.drawable.fifth,
R.drawable.sixth
};

CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(this, mResources);

mViewPager.setAdapter(mCustomPagerAdapter);

查看以下教程了解更多详情:http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

关于android - 如何在 Android 上开发图像轮播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com