我正在使用android应用程序,该应用程序使用viewpager从URL加载的图像创建滑块。一切正常。
但是现在我需要改进它,不仅添加图像,而且还添加来自URL的视频。
这是当前viewpager的代码:
片段中:
$empty_stars = 5 - ($full_stars + $half_star);
echo str_repeat('<i class="far fa-star"></i>', $empty_stars);
还有适配器:
final String url_postmedia = "https://...postsmedia/";
String mediapostfile []= mediafile.split(",");
String mediaposttype []= mediatype.split(",");
urls = new String[mediapostfile.length];
for (int i = 0; i < mediapostfile.length; i++) {
if (mediaposttype[i].equals("1")){
urls[i] = url_postmedia+mediapostfile[i];
}
}
holder.mPager.setAdapter(new SlidingImage_Adapter(context,urls));
holder.indicator.setViewPager(holder.mPager);
//Set circle indicator radius
NUM_PAGES = urls.length;
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
holder.mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 3000, 3000);
// Pager listener over indicator
holder.indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
我从服务器收到两个字符串数组:public class SlidingImage_Adapter extends PagerAdapter {
private String[] urls;
private LayoutInflater inflater;
private Context context;
public SlidingImage_Adapter(Context context, String[] urls) {
this.context = context;
this.urls = urls;
inflater = LayoutInflater.from(context);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return urls.length;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.slidingimages_layout, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout
.findViewById(R.id.image);
imageLayout.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//this will log the page number that was click
}
});
Glide.with(context)
.load(urls[position])
.into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
}
(包含文件名)和mediapostfile[]
(包含文件类型),“ 1”表示图像,“ 2”表示视频。
使用当前代码将视频添加到滑块时应该考虑什么?