如何在Android中的ViewPager中处理多个按钮点击?

时间:2017-06-20 16:29:44

标签: android android-layout android-viewpager onclicklistener

我有一个由外部API支持的ViewPager。每个页面都有三个按钮。单击这些按钮后,应用程序崩溃。我的适配器如下。

 public class ViewPagerAdapter extends PagerAdapter{

    private Activity activity;
    private LayoutInflater inflater;
    private List<NewsModel> newsItems;
    Context context;
    View itemView;
    public ViewPagerAdapter(Activity activity, List<NewsModel> newsItems){
    this.activity = activity;
    this.newsItems = newsItems;
}

@Override
public int getCount() {
    return newsItems.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view ==((LinearLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position){

    TextView mAuthor;
    NetworkImageView networkImageView;
    ImageButton fav, sha, mor;

        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        itemView = inflater.inflate(R.layout.pager_item, container, false);
        imageLoader = AppController.getInstance().getImageLoader();

    mAuthor = (TextView) itemView.findViewById(R.id.nwAuthor);
    fav = (ImageButton) itemView.findViewById(R.id.fav);
    sha = (ImageButton) itemView.findViewById(R.id.sha);
    mor = (ImageButton)itemView.findViewById(R.id.mor);
   networkImageView = (NetworkImageView) itemView.findViewById(R.id.nwImage);

    fav.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            View parentRow = (View) v.getParent();
            ViewPager vp = (ViewPager) parentRow.getParent();
            final int position = vp.getCurrentItem();


    });

  networkImageView.setImageUrl(newsItems.get(position).getUrlToImageNews(), imageLoader);
    mAuthor.setText(newsItems.get(position).getAuthor());
    Log.d("Aseem Savio",newsItems.get(position).getAuthor());

    mTitle.setText(newsItems.get(position).getTitleNews());
    mDesc.setText(newsItems.get(position).getDescription());
    mTime.setText(newsItems.get(position).getPublishedAt());

    container.addView(itemView);

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((LinearLayout) object);
}}

以下是错误日志。

FATAL EXCEPTION: main Process: com.aseemsavio.picks, PID: 11298 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v4.view.ViewPager at com.aseemsavio.picks.ViewPagerAdapter$2.onClick(ViewPagerAdapter.java:123) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22260) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

总体而言,如何在视图寻呼机中执行多个按钮点击?请解释我们如何在View Pager中获取项目的位置。另外,解释如何纠正上述错误。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您发布了适配器~70行代码,但错误在123上 com.aseemsavio.picks.ViewPagerAdapter $ 2.onClick(ViewPagerAdapter.java:123)

看起来像问题在这里:

  ViewPager vp = (ViewPager) parentRow.getParent();

由于:

  

java.lang.ClassCastException:android.widget.LinearLayout不能   强制转换为android.support.v4.view.ViewPager

表示你的parentRow.getParent();重新训练Linearlayout的实例

尝试:     在onClick:

iewPager vp = (ViewPager) findViewPagerParent(v);
if(vp != null) {
   //do your stuff 
}

添加方法

private View findViewPagerParent(View childView) {
    if (childView == null) {
          return null;
    }
    if (childView instanceof ViewPager) {
        return childView;
    } else {
        return findViewPagerParent((View) childView.getParent());
    }
}