Search code examples
androidorientationgridlayoutmanager

How Can I Change orientation of RecyclerView?


I used RecyclerView like this,

mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false);

mRecyclerView.setLayoutManager(mLayoutManager);

mAdapter = new GridAdapter((getActivity()), getGridItems());
mAdapter.setOnItemClickListener(new GridAdapter.OnItemClickListener() {

        @Override
        public void onItemClick(View itemView, int position) {
            Toast.makeText(getContext(), getGridItems().get(position).mtitle, Toast.LENGTH_SHORT).show();
        }
});

As you see, RecycleView has GridLayoutManager. but when I call Toast 2nd, 3rd item's title are changed each other.

I think RecycleView's Orientation is issue.

How Can I set orientation of ReecycleView?


Solution

  • Why you set listner to the Adapter , set listner on recyclerview itself ,use this class first :

            import android.content.Context;
        import android.support.v7.widget.RecyclerView;
        import android.view.GestureDetector;
        import android.view.MotionEvent;
        import android.view.View;
    
           public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
    
        private OnItemClickListener mListener;
        public interface OnItemClickListener {
            public void onItemClick(View view, int position);
        }
        GestureDetector mGestureDetector;
        public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
            mListener = listener;
            mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }
            });
        }
        @Override
        public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
            View childView = view.findChildViewUnder(e.getX(), e.getY());
            if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
                mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
            }
            return false;
        }
    
        @Override
        public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
        }
    
        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    
          }
        }
    

    Then u can use it like this :

       recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), new   RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    // TODO Handle item click
                    Log.e("Position = ",""+position);
                }
            })
    );