实现要点
上面大图使用ImageSwitcher
调用setFactory(ViewFactory factory)
下面小图显示使用HorizontalScrollView和LinearLayout
gallery
已经被遗弃
实例
xml
<ImageSwitcher
android:layout_marginTop="5pt"
android:id="@+id/fragment_video_imageswitcher"
android:layout_width="match_parent"
android:layout_height="150pt">
</ImageSwitcher>
<HorizontalScrollView
android:id="@+id/fragment_video_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fragment_video_headline"
android:scrollbars="none">
<LinearLayout
android:id="@+id/fragment_video_linearlayout"
android:layout_width="wrap_content"
android:layout_height="30pt"
android:orientation="horizontal"
android:gravity="center"
android:padding="2pt">
</LinearLayout>
</HorizontalScrollView>
java
private LinearLayout linearLayout;
private ImageSwitcher imageSwitcher;
//原图
private Integer[] images = {R.drawable.video_greenhouse1, R.drawable.video_greenhouse2, R.drawable.video_greenhouse3, R.drawable.video_greenhouse4};
//缩略图
private Integer[] sImages = {R.drawable.video_greenhouse_1, R.drawable.video_greenhouse_2, R.drawable.video_greenhouse_3, R.drawable.video_greenhouse_4};
//获得控件
linearLayout = (LinearLayout) getActivity().findViewById(R.id.fragment_video_linearlayout);
imageSwitcher = (ImageSwitcher) getActivity().findViewById(R.id.fragment_video_imageswitcher);
imageSwitcher.setFactory(this); //设置工厂
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in)); //设置淡入的动画
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out)); //设置淡出的动画
imageSwitcher.setImageResource(images[0]); //默认选择题一个图片
for(int i = 0; i < sImages.length; i++)
{
linearLayout.addView(getImageView(i));
}
getImageView
//得到缩略图片,并且添加监听器
private ImageView getImageView(int i){
final ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(sImages[i]); //设置缩略图
imageView.setId(i); //设置ID
imageView.setPadding(2,2,2,2);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
imageSwitcher.setImageResource(images[v.getId()]); //设置资源 //v.getId()获取编号ID
Toast.makeText(getActivity(), "当前视频:大棚"+v.getId(), Toast.LENGTH_SHORT).show();
}
});
return imageView;
}
因为要设置工厂,所以该Activity要实现ViewFactory接口
@Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());//得到ImageView
return imageView;
}