activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.srtpl.recycleandgriddemo.MainActivity">
<Button
android:id="@+id/bt_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LIST"/>
<Button
android:id="@+id/bt_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt_list"
android:text="GRID"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle"
android:layout_below="@+id/bt_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_grid"
android:layout_below="@+id/bt_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
</RelativeLayout>
Employee.java
package com.srtpl.recycleandgriddemo;
public class Employee {
String name;
String position;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
EmployeeRowHolder.java
public class EmployeeRowHolder extends RecyclerView.ViewHolder {
public TextView mTextViewOne;
public TextView mTextViewTwo;
public EmployeeRowHolder(View convertView) {
super(convertView);
this.mTextViewOne = (TextView) convertView.findViewById(R.id.txt_1);
this.mTextViewTwo = (TextView) convertView.findViewById(R.id.txt_2);
}
}
MainActivity.java
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
RecyclerView mRecyclerViewGrid;
ArrayList<Employee> mArrayListEmployees;
EmployeeRecyclerAdapter mEmployeeRecyclerAdapter;
Button mButtonList;
Button mButtonGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycle);
mRecyclerViewGrid = (RecyclerView) findViewById(R.id.recycle_grid);
mButtonList = (Button) findViewById(R.id.bt_list);
mButtonGrid = (Button) findViewById(R.id.bt_grid);
mArrayListEmployees = new ArrayList<>();
Employee mEmployee = new Employee();
mEmployee.setName("EMp 1");
mEmployee.setPosition("Team Leader");
mArrayListEmployees.add(mEmployee);
Employee mEmployee1 = new Employee();
mEmployee1.setName("EMp 2");
mEmployee1.setPosition("Developer");
mArrayListEmployees.add(mEmployee1);
Employee mEmployee2 = new Employee();
mEmployee2.setName("EMp 3");
mEmployee2.setPosition("Tester");
mArrayListEmployees.add(mEmployee2);
Employee mEmployee3 = new Employee();
mEmployee3.setName("EMp 4");
mEmployee3.setPosition("Support");
mArrayListEmployees.add(mEmployee3);
Employee mEmployee4 = new Employee();
mEmployee4.setName("EMp 5");
mEmployee4.setPosition("Designer");
mArrayListEmployees.add(mEmployee4);
Employee mEmployee5 = new Employee();
mEmployee5.setName("EMp 6");
mEmployee5.setPosition("BDM");
mArrayListEmployees.add(mEmployee5);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerViewGrid.setLayoutManager(new GridLayoutManager(this, 2));
mEmployeeRecyclerAdapter = new EmployeeRecyclerAdapter(MainActivity.this, mArrayListEmployees);
mRecyclerView.setAdapter(mEmployeeRecyclerAdapter);
mRecyclerViewGrid.setAdapter(mEmployeeRecyclerAdapter);
mButtonList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.setVisibility(View.VISIBLE);
mRecyclerViewGrid.setVisibility(View.GONE);
}
});
mButtonGrid.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerView.setVisibility(View.GONE);
mRecyclerViewGrid.setVisibility(View.VISIBLE);
}
});
}
public class EmployeeRecyclerAdapter extends RecyclerView.Adapter<EmployeeRowHolder> {
private List<Employee> list;
private Context mContext;
public EmployeeRecyclerAdapter(Context context, List<Employee> feedItemList) {
this.list = feedItemList;
this.mContext = context;
}
@Override
public EmployeeRowHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_item, null);
EmployeeRowHolder mh = new EmployeeRowHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final EmployeeRowHolder queryListRowHolder, final int i) {
queryListRowHolder.mTextViewOne.setText(list.get(i).getName());
queryListRowHolder.mTextViewTwo.setText(list.get(i).getPosition());
}
@Override
public int getItemCount() {
return (null != list ? list.size() : 0);
}
}
}