RecyclerView和Admob项目:onClickListener的问题

时间:2017-01-04 20:11:36

标签: android android-recyclerview admob

我在RecyclerView ItemList中添加了NativeExpressAdView,但现在onClickListener正在干扰NativeExpressAdView。我的“普通”项目由文本和一个较大的图像组成。单击较大的Image时,将打开DialogFragment并显示Item的详细信息。但是,NativeExpressAdView不应该遇到此onClickListener。该应用程序不断崩溃,因为它试图打开NativeExpressAdView ...

的不存在的详细视图

MainActivity

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ......

    recyclerView.addOnItemTouchListener(new ItemAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new ItemAdapter.ClickListener() {
        @Override
        public void onClick(View view, final int position) {
            final Bundle bundle = new Bundle();
            bundle.putSerializable("events", events);
            bundle.putInt("position", position);
            final Event event = events.get(position);

            detailView = (ImageView) view.findViewById(R.id.item_thumbnail);
            detailView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
                    newFragment.setArguments(bundle);
                    newFragment.setCancelable(true);
                    newFragment.show(ft, "slideshow");
                }
            });
           ....
           some other button onClickListener()...
           ....
        }

        @Override
        public void onLongClick(View view, int position) {
        }
    }));

ItemAdapter

public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private List<Event> events;
private Context mContext;

public ItemAdapter(Context context, List<Event> events){
    mContext = context;
    this.events = events;
}

public static class MyViewHolder extends RecyclerView.ViewHolder{ // implements View.OnClickListener {
    public ImageView thumbnail;
    public TextView event_name;
    public TextView event_date;
    public TextView event_descr;
    public TextView event_attending;
    public TextView event_maybe;

    public MyViewHolder(android.view.View view){
        super(view);
        thumbnail = (ImageView) view.findViewById(R.id.item_thumbnail);
        event_name = (TextView) view.findViewById(R.id.tV_event_name);
        event_date = (TextView) view.findViewById(R.id.tV_event_date);
        event_descr = (TextView) view.findViewById(R.id.tV_event_descr);
        event_attending = (TextView) view.findViewById(R.id.tV_attending_count);
        event_maybe = (TextView) view.findViewById(R.id.tV_maybe_count);
    }
}

public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
    public NativeExpressAdView nativeExpressAdView;
    public ViewHolderAdMob(View view) {
        super(view);
        nativeExpressAdView = (NativeExpressAdView) view.findViewById(R.id.ad_nativeAdOne);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("228****************************")
                .build();
        nativeExpressAdView.loadAd(adRequest);
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    RecyclerView.ViewHolder viewHolder = null;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    //View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_items, parent, false);

    switch (viewType){
        case 1:{
            View itemView = inflater.inflate(R.layout.rv_items, parent, false);
            viewHolder = new MyViewHolder(itemView);
            break;
        }
        case 2:{
            View itemView = inflater.inflate(R.layout.native_ad_one, parent, false);
            viewHolder = new ViewHolderAdMob(itemView);
            break;
        }
    }

    return viewHolder;
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position){
    Event event = events.get(position);

    switch (holder.getItemViewType()){
        case 1:{
            MyViewHolder viewHolder = (MyViewHolder) holder;
            viewHolder.event_name.setText(event.getEventName());
            viewHolder.event_date.setText(event.getEventDate());
            viewHolder.event_descr.setText(event.getEventDescription());
            viewHolder.event_attending.setText(event.getEventAttending());
            viewHolder.event_maybe.setText(event.getEventMaybeComming());

            Glide.with(mContext).load(event.getEventImage())
                    .thumbnail(0.9f)
                    .crossFade()
                    //.diskCacheStrategy(DiskCacheStrategy.ALL)
                    .fitCenter()
                    .into(viewHolder.thumbnail);
            break;
        }
        case 2:{
            break;
        }
    }


}

@Override
public int getItemCount(){
    return events.size();
}

@Override
public int getItemViewType(int position) {
    return  events.get(position).getViewType();
}

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ItemAdapter.ClickListener clickListener;

    public RecyclerTouchListener(final Context context, final RecyclerView recyclerView, final ItemAdapter.ClickListener clickListener){
        this.clickListener = clickListener;

        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
            @Override
            public boolean onSingleTapUp(MotionEvent e){
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e){

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)){
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e){

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept){

    }
}
}

我有点看到问题源于何处,因为RecyclerView正在监听输入并自然地对它作出反应但我认为通过在我的MainActivity中定义detailView = (ImageView) view.findViewById(R.id.item_thumbnail);它只会对“正常”作出反应“R.id.item_thumbnail以来的项目是我的”普通“项xml的元素,而NativeExpressAdView没有这样的元素。我该如何解决这个问题并获得正确的功能?

2 个答案:

答案 0 :(得分:1)

根据视图类型在适配器中的onBindViewHolder中设置单击侦听器。

public static class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final MyListClickListener clickListener;

    public ItemAdapter(MyListClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == ADVERT_VIEW_TYPE) {
            // do whatever you do for adverts
        } else {
            // do what you do for items and set the click listener
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickListener.onShowDetailViewClicked(); // pass the parameters you need to identify this particular item
                }
            });
        }
    }

    public interface MyListClickListener {

        void onShowDetailViewClicked();
    }
}

答案 1 :(得分:0)

您正在将onItemTouchListener {...}设置为整个RecyclerView。它假定无论您在哪里触摸,都有详细视图。但是您的RecyclerView中有不同类型的行。最好的选择是为ViewHolder类中的每个项添加触摸侦听器。