是否可以在android中创建一个包含在列表视图中水平对齐的图像集的轮播。而且我想在点击它时突出显示一个图像项目。
答案 0 :(得分:0)
请使用RecyclerView而不是使用ListView。看看这段代码 - Carousel。使用RecyclerView
作为根视图,LinearLayoutManager.HORIZONTAL
作为LayoutManager。
答案 1 :(得分:0)
在布局中查看寻呼机
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
在你的活动中
public class Event_Image_Slider extends Activity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event__image__slider);
viewPager = (ViewPager) findViewById(R.id.viewPager);
CustomAdapter adapter = new CustomAdapter(Event_Image_Slider.this);
viewPager.setAdapter(adapter);
}
}
你的自定义adpter代码,在你的activty声明之前声明ArrayList imagepathArray = new ArrayList();
public class CustomAdapter extends PagerAdapter{
Context context;
public CustomAdapter(Context context){
this.context = context;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View viewItem = inflater.inflate(R.layout.image_item, container, false);
ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView10);
Glide.with(context).load(yourActivty.imagepathArray.get(position)).into(imageView);
((ViewPager)container).addView(viewItem);
return viewItem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return yourActivty.imagepathArray.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((View)object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}}
答案 2 :(得分:0)
您可以在项目中使用此github库作为轮播功能,这里是carouselview library的链接。
应用级Gradle文件(非项目级gradle):
compile 'com.synnapps:carouselview:0.0.9'
在您的布局中包含以下代码:
<com.synnapps.carouselview.CarouselView
android:id="@+id/carouselView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:radius="6dp"
app:slideInterval="3000"
app:strokeColor="#FF777777"
app:strokeWidth="1dp"/>
在您的活动中加入以下代码
public class SampleCarouselViewActivity extends AppCompatActivity {
CarouselView carouselView;
int[] sampleImages = {R.drawable.image_1, R.drawable.image_2, R.drawable.image_3, R.drawable.image_4, R.drawable.image_5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_carousel_view);
carouselView = (CarouselView) findViewById(R.id.carouselView);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
}
ImageListener imageListener = new ImageListener() {
@Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageResource(sampleImages[position]);
}
};
}
您还可以在给定链接上探索额外支持的xml属性。