Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Horizontal RecyclerView in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 640
    Comment on it

    In the below example I have created a Horizontal RecyclerView.

    Here first I have added RecyclerView dependency to Grandle file.
    In second step I have added a RecyclerView and Set RecyclerView  gravity (center_horizontal). Now see In MainActivity code  LinearLayoutManager's Orientation to HORIZONTAL.
    You can see below example code it clearly describes How to make "horizontal RecyclerView in android".

    Step(1)-I have added first RecyclerView and CardView dependency to Grandle file -
          

          

     compile 'com.android.support:recyclerview-v7:21.0.+'
           compile 'com.android.support:cardview-v7:21.0.+'

     Step(2)-activity_main.xml layout-

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider_one"
            android:layout_marginTop="15dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tv_members"
    
                android:text="Top Profils"
                android:textSize="17dp"
                android:textColor="#1b1a1a"
                android:textStyle="bold"
                android:layout_marginLeft="15dp"/>
    
            <!--  Recycler View  -->
    
    
            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="30dp"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/recycler_view"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_gravity="center_horizontal"/>
        </RelativeLayout>
    
    
    </LinearLayout>    


    Step(3)-Created a picture_row.xml layout-

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardUseCompatPadding="true">
    
        <!--   Recycler View Item Row   -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:orientation="vertical"
            android:id="@+id/main"
            android:layout_height="fill_parent">
    
    
            <ImageView
                android:id="@+id/image"
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop" />
    
    
        </LinearLayout>
    
    </android.support.v7.widget.CardView>

    Step(4)-Created a new class Picture_data-

    public class Picture_data {
    
    
        private int image;
    
        public Picture_data(int image) {
            this.image = image;
        }
    
        public int getImage() {
            return image;
        }
    }


    Step(5)-Created a new class Picture_RecyclerView_Adapter -

    public class Picture_RecyclerView_Adapter extends RecyclerView.Adapter<Picture_RecyclerView_Adapter.MyViewHolder> {
    
        private List<Picture_data>data;
    
        public Picture_RecyclerView_Adapter(List<Picture_data> data){
            this.data = data;
        }
    
    
        public class MyViewHolder extends RecyclerView.ViewHolder  {
    
            public ImageView imageView;
            private LinearLayout main;
    
            public MyViewHolder(final View parent) {
                super(parent);
                imageView = (ImageView) parent.findViewById(R.id.image);
                main = (LinearLayout) parent.findViewById(R.id.main);
                main.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(itemView.getContext(), "Position:" + Integer.toString(getPosition()), Toast.LENGTH_SHORT).show();
    
                    }
                });
            }
        }
    
    
        @Override
        public Picture_RecyclerView_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pictures_row,parent,false);
            return new MyViewHolder(itemView);
        }
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
    
            Picture_data row = data.get(position);
    
    
            holder.imageView.setImageResource(row.getImage());
    
        }
        @Override
        public int getItemCount() {
            return data.size();
        }
    }
    
    


    Step(6)- MainActivity-
     

     public class MainActivity extends AppCompatActivity {
        private List<Picture_data>data=new ArrayList<>();
        private RecyclerView recyclerView;
        private Picture_RecyclerView_Adapter picture_recyclerView_adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
            setData();
             picture_recyclerView_adapter= new Picture_RecyclerView_Adapter(data);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.HORIZONTAL,false);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(picture_recyclerView_adapter);
    
    
        }
    
        private void setData() {
            Picture_data item = new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
            item =new Picture_data(R.drawable.people);
            data.add(item);
    
        }
    
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: