下面是使用Viewpager的我的Image Slider的XML代码。如下所示,我已经在所有4张图像上显示了一个textview。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/image_view"
android:layout_alignTop="@+id/image_view"
android:layout_alignRight="@+id/image_view"
android:layout_alignBottom="@+id/image_view"
android:layout_margin="1dp"
android:gravity="bottom"
android:textSize="30dp"
android:text="Welcome to DriftFan"
android:textStyle="bold"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"/>
</RelativeLayout>
这是我的ViewPager XML代码
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="204dp"
android:scaleType="centerCrop"
></android.support.v4.view.ViewPager>
最后这是我的班级
public class myTimerTask extends TimerTask {
@Override
public void run() {
if (getActivity()== null) {
return;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() == 0) {
viewPager.setCurrentItem(1);
} else if (viewPager.getCurrentItem() == 1) {
viewPager.setCurrentItem(2);
} else if (viewPager.getCurrentItem() == 2) {
viewPager.setCurrentItem(3);
}
}
});
}
}
}
我的java类是否有一些代码可以手动为任何图像分配文本视图?
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(images[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager)container;
View view = (View) object;
vp.removeView(view);
}
}
上面是ViewPager适配器类,它是我的ImageSlidler的基础。
答案 0 :(得分:0)
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4};
private String[] texts= {"text1", "text2","text3","text4"};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
TextView tv= (TextView) view.findViewById(R.id.myImageViewText);
imageView.setImageResource(images[position]);
tv.setText(texts[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager)container;
View view = (View) object;
vp.removeView(view);
}
}
答案 1 :(得分:0)
你有使用ImageView和TextView的布局文件。对于ViewPager的每个项目,您可以使用images阵列中的图像。对TextView执行相同操作 - 创建文本数组,并在适配器中找到TextView并设置正确的文本。 Bosco Sabin 的示例演示了您需要做什么。
答案 2 :(得分:0)
xml:
<com.daimajia.slider.library.SliderLayout
android:id="@+id/product_image"
android:layout_width="fill_parent"
android:layout_height="320dp"
/>
活动文件:
SliderLayout sliderLayout;
HashMap<String,String> Hash_file_maps ;
sliderLayout = (SliderLayout)findViewById(R.id.product_image);
@Override
protected void onStop() {
sliderLayout.stopAutoCycle();
super.onStop();
}
@Override
public void onSliderClick(BaseSliderView slider) {
Toast.makeText(this,slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
Log.d("Slider Demo", "Page Changed: " + position);
}
@Override
public void onPageScrollStateChanged(int state) {}
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("pRoductdetailsResult");
Hash_file_maps = new HashMap<String, String>();
for(int i=0 ;i < jsonArray.length(); i++ ){
JSONObject productDetails = jsonArray.getJSONObject(i);
productModel = new ProductDetailsModel();
productModel.setpId(String.valueOf(productId));
//productModel.setAboutproduct(productDetails.getString("aboutproduct"));
String htmlaboutprouduct=(productDetails.getString("aboutproduct"));
htmlaboutprouduct = htmlaboutprouduct.replaceAll("<(.*?)\\>","");
htmlaboutprouduct = htmlaboutprouduct.replaceAll("<(.*?)\\\n","");
htmlaboutprouduct = htmlaboutprouduct.replaceFirst("(.*?)\\>", "");
htmlaboutprouduct = htmlaboutprouduct.replaceAll(" ","");
htmlaboutprouduct = htmlaboutprouduct.replaceAll("&","");
productModel.setAboutproduct(htmlaboutprouduct);
Log.d("aboutproduct : ",htmlaboutprouduct);
Hash_file_maps.put(productDetails.getString("pName"), productDetails.getString("pImgUrl"));
setDetails(productModel);
}
for(String name : Hash_file_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(ProductDetailActivity.this);
textSliderView
.description(name)
.image(Hash_file_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
textSliderView.bundle(new Bundle());
textSliderView.getBundle()
.putString("extra",name);
sliderLayout.addSlider(textSliderView);
}
sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
sliderLayout.setCustomAnimation(new DescriptionAnimation());
sliderLayout.setDuration(3000);
sliderLayout.addOnPageChangeListener(this);
} catch (JSONException e) {
e.printStackTrace();
}