The RecyclerView latest version of listview and improved
performance and benefits. RecyclerView provide flexible view with large data
set. Recycle view support features made available on newer version of the
platform. For example Android lower than 5.0(API level 21) .that relies on framework classes cannot
display material design elements.
First open build.gradle and add recyclerView dependency.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
}
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
}
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
......................................................................................................................................................................
Create Item Class and add the getter/setter method.
StudentClassification.java.
......................................................................................................................................................................
public class StudentClasification {
private String RollNumber, StudentName, PassoutYear;
public StudentClasification() {
}
public StudentClasification(String RollNumber, String StudentName, String PassoutYear) {
this.RollNumber = RollNumber;
this.StudentName = StudentName;
this.PassoutYear = PassoutYear;
}
public String getRollNumber() {
return RollNumber;
}
public void setRollNumber(String rollNumber) {
RollNumber = rollNumber;
}
public String getStudentName() {
return StudentName;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}
public String getPassoutYear() {
return PassoutYear;
}
public void setPassoutYear(String passoutYear) {
PassoutYear = passoutYear;
}
}
......................................................................................................................................................................
Create new list_row.xml layout and used in recyclerview row and display Student Detail in single row.
......................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/RollNumber"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/StudentName"
android:layout_below="@id/RollNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
......................................................................................................................................................................
Create StudentAdapter.java RecyclerView Adapter.
......................................................................................................................................................................
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
private List<StudentClasification> StudentClasificationList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView RollNumber, year, StudentName;
public MyViewHolder(View view) {
super(view);
RollNumber = (TextView) view.findViewById(R.id.RollNumber);
StudentName = (TextView) view.findViewById(R.id.StudentName);
year = (TextView) view.findViewById(R.id.year);
}
}
public StudentAdapter(List<StudentClasification> StudentClasificationList) {
this.StudentClasificationList = StudentClasificationList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(StudentAdapter.MyViewHolder holder, int position) {
StudentClasification values = StudentClasificationList.get(position);
holder.RollNumber.setText(values.getRollNumber());
holder.StudentName.setText(values.getStudentName());
holder.year.setText(values.getPassoutYear());
}
@Override
public int getItemCount() {
return StudentClasificationList.size();
}
}
......................................................................................................................................................................
create new DividerItemDecoration.java Decorate class via this class set RecyclerView Divider/ separator.
.....................................................................................................................................................................
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
private Drawable mDivider;
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
mDivider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}
public DividerItemDecoration(Context context, int resId) {
mDivider = ContextCompat.getDrawable(context, resId);
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
......................................................................................................................................................................
MainActivity.Java.
......................................................................................................................................................................
public class MainActivity extends AppCompatActivity {
private List<StudentClasification> StudentList = new ArrayList<>();
private RecyclerView recyclerView;
private StudentAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
StudentList.add(new StudentClasification("ROLL NO.: AB2345","STUDENT NAME : Biertny","PASS YEAR: 2000"));
StudentList.add(new StudentClasification("ROLL NO.: AB2678","STUDENT NAME : Johan","PASS YEAR: 2005"));
StudentList.add(new StudentClasification("ROLL NO.: AB2675","STUDENT NAME : Jaison","PASS YEAR: 2007"));
StudentList.add(new StudentClasification("ROLL NO.: AB2976","STUDENT NAME : Watson","PASS YEAR: 2009"));
StudentList.add(new StudentClasification("ROLL NO.: AB2080","STUDENT NAME : Parank","PASS YEAR: 2010"));
StudentList.add(new StudentClasification("ROLL NO.: AB2670","STUDENT NAME : Jojab","PASS YEAR: 2013"));
StudentList.add(new StudentClasification("ROLL NO.: AB2640","STUDENT NAME : Boss","PASS YEAR: 2003"));
StudentList.add(new StudentClasification("ROLL NO.: AB0876","STUDENT NAME : Jon","PASS YEAR: 2014"));
StudentList.add(new StudentClasification("ROLL NO.: AB2005","STUDENT NAME : Rock","PASS YEAR: 2015"));
StudentList.add(new StudentClasification("ROLL NO.: AB2000","STUDENT NAME : Brock","PASS YEAR: 2016"));
recyclerView.addItemDecoration(new DividerItemDecoration(this));
mAdapter=new StudentAdapter(StudentList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
}
......................................................................................................................................................................
0 comments