RecyclerView inside of a GridView's item ruins the GridView's OnClickListener

时间:2015-10-31 00:13:15

标签: android gridview android-recyclerview

I have this disposition of layout: which is a GridView with RecyclerViews inside of each item of the GridView. I haven't found anywhere the solution since I think it never happened before. Not found in SO or Google Groups, for example. So, the problem is, when I set a RecyclerView, the method onItemClick of the GridView doesn't get called, but if I don't set the RecyclerView it gets called. These are the two codes (one setting the RecyclerView and one hiding it): SETTING RECYCLERVIEW ON A GRIDVIEW's ITEM @Override public View getView(int position, View convertView, ViewGroup viewGroup) { ViewHolderItem viewHolderItem; if(convertView == null) { convertView = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_mipeticion, viewGroup, false); viewHolderItem = new ViewHolderItem(); viewHolderItem.palabra_TV = (CustomTextView)convertView.findViewById(R.id.palabra_TV); viewHolderItem.recyclerView = (RecyclerView)convertView.findViewById(R.id.recyclerView); convertView.setTag(viewHolderItem); } else { viewHolderItem = (ViewHolderItem)convertView.getTag(); } MyItem item = items.get(position); String textRandom = item.getTexts().get(0); viewHolderItem.palabra_TV.setText(textRandom); ArrayList<Drawable> drawables = item.getDrawables(); final RecyclerViewImagesAdapter mAdapter = new RecyclerViewImagesAdapter(drawables, getActivity()); viewHolderItem.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false)); viewHolderItem.recyclerView.setAdapter(mAdapter); return convertView; } This makes the onClick not getting called for GridView AND THIS IS THE CODE HIDING IT @Override public View getView(int position, View convertView, ViewGroup viewGroup) { ViewHolderItem viewHolderItem; if(convertView == null) { convertView = ((LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_mipeticion, viewGroup, false); viewHolderItem = new ViewHolderItem(); viewHolderItem.palabra_TV = (CustomTextView)convertView.findViewById(R.id.palabra_TV); viewHolderItem.recyclerView = (RecyclerView)convertView.findViewById(R.id.recyclerView); convertView.setTag(viewHolderItem); } else { viewHolderItem = (ViewHolderItem)convertView.getTag(); } MyItem item = items.get(position); String textRandom = item.getTexts().get(0); viewHolderItem.palabra_TV.setText(textRandom); ArrayList<Drawable> drawables = item.getDrawables(); final RecyclerViewImagesAdapter mAdapter = new RecyclerViewImagesAdapter(drawables, getActivity()); viewHolderItem.recyclerView.setVisibility(View.GONE); // just not setting the adapter and hidding it return convertView; } What's wrong with this? Thank you very much in advance. Regards. Rafael. EDIT: What I've discovered that won't work Setting android:clickable="false" to the RecyclerView on the XML. Handle the RecyclerView's click and calling the parent.performClick() Setting an GestureDetector as @Droidekas mentioned handles only the RecyclerView's click, but not the whole item's click (so the onItemClick from GridView)

1 个答案:

答案 0 :(得分:2)

根据我的理解,您希望除了单击以处理您的Recyclerview之外的所有其他操作:

您需要区分手势。 ComboBox的作业。因此请使用GestureDetector.SimpleOnGestureListener

GestureDetector

然后,您可以添加RecyclerView.OnItemTouchListener

final GestureDetector gd = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                //perform parent view click
                return true;
            }
        });

这将确保 recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { @Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return gd.onTouchEvent(e); } @Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } });

处理所有其他触摸事件

PS :请确保内部recyclerview中的itemview没有自己的点击事件