我正在使用viewpager作为图像滑块。
我的问题是,当用户单击每个页面上的图像时,onclicklistener无法检测到。
这是我的适配器:
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, final int position) {
final 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
Log.d("position","position "+position);//not working
}
});
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;
}
}
编辑 recyclerView适配器中的viewPager代码
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 == urls.length) {
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) {
}
});
答案 0 :(得分:0)
ViewPager没有OnItemClick回调方法。如果要单击每个页面上的事件,则必须将侦听器构建到适配器中的页面内容中。在Inflater
覆盖方法内创建instantiateItem
实例。
类似这样的东西:
@Override
public Object instantiateItem(ViewGroup view, final int position) {
LayoutInflater inflater = (LayoutInflater) view.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final 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
Log.d("position","position "+position);//not working
}
});
Glide.with(context)
.load(urls[position])
.into(imageView);
view.addView(imageLayout, 0);
return imageLayout;
}
答案 1 :(得分:0)
好的,在您的SlidingImage_Adapter
中,我们将对您的instantiateItem
方法进行一些更改。
尝试将您的instantiateItem
方法更改为此:
@Override
public Object instantiateItem(ViewGroup view, final int position) {
//this is the inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//this is the view
final View imageLayout = inflater.inflate(R.layout.slidingimages_layout, null);
//this is the imageview in the view
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
//use glide to load images into the image views
Glide.with(context)
.load(urls[position])
.into(imageView);
//click listener of each image
imageLayout.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//this will log the page number that was click
Log.d("position","position "+position); //should work
}
});
//do this now
ViewPager viewpager = (ViewPager) view;
viewpager.addView(imageLayout, 0);
//return the view
return imageLayout;
}
也将isViewFromObject
方法更改为此:
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
也将destroyItem
方法更改为此:
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager viewpager = (ViewPager) container;
viewpager.removeView((View) object);
}