Created
March 15, 2017 05:40
-
-
Save JaydipZala/cd0b98ed2986bd8ee3a19d420fcf1aaa to your computer and use it in GitHub Desktop.
RecyclerView Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.rv.RecyclerViewActivity"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/rvList" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="8dp" | |
android:layout_marginLeft="8dp" | |
android:layout_marginRight="8dp" | |
android:layout_marginTop="8dp" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/imgStudent" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginLeft="16dp" | |
android:layout_marginTop="16dp" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:src="@mipmap/ic_launcher" | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:layout_marginBottom="16dp" /> | |
<TextView | |
android:id="@+id/txtName" | |
android:layout_width="wrap_content" | |
android:layout_height="0dp" | |
android:layout_marginBottom="0dp" | |
android:layout_marginLeft="20dp" | |
android:layout_marginTop="16dp" | |
android:gravity="center_vertical" | |
app:layout_constraintBottom_toBottomOf="@+id/imgStudent" | |
app:layout_constraintLeft_toRightOf="@+id/imgStudent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:text="Student 1" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rv; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class RecyclerViewActivity extends AppCompatActivity { | |
private RecyclerView rvList; | |
private StudentListAdapter _adapter; | |
private ArrayList<Student> arrStudents = new ArrayList<>(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
initUI(); | |
setUpList(); | |
} | |
private void initUI() { | |
rvList = (RecyclerView) findViewById(R.id.rvList); | |
LinearLayoutManager lm = new LinearLayoutManager(this); | |
lm.setOrientation(LinearLayoutManager.VERTICAL); | |
rvList.setLayoutManager(lm); | |
_adapter = new StudentListAdapter(this, arrStudents); | |
_adapter.setRecyclerViewListener(new StudentListAdapter.RecyclerViewListener() { | |
@Override | |
public void onClick(View v, int position) { | |
Toast.makeText(RecyclerViewActivity.this, "position : " + position, Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
rvList.setAdapter(_adapter); | |
} | |
private void setUpList() { | |
arrStudents.clear(); | |
for (int i = 0; i < 11; i++) { | |
Student student = new Student(); | |
student.name = "Student " + i; | |
student.imgResource = i % 2 == 0 ? R.mipmap.ic_launcher : R.mipmap.ic_launcher_round; | |
arrStudents.add(student); | |
} | |
_adapter.notifyDataSetChanged(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rv; | |
/** | |
* Created by Jaydip Zala | |
*/ | |
public class Student { | |
public String name = ""; | |
public int imgResource = R.mipmap.ic_launcher; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rv; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
/** | |
* Created by Jaydip Zala | |
*/ | |
public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ItemViewHolder> { | |
private ArrayList<Student> _items; | |
private Context context; | |
private RecyclerViewListener recyclerViewListener; | |
public StudentListAdapter(Context c, ArrayList<Student> _items) { | |
context = c; | |
this._items = _items; | |
} | |
public void setRecyclerViewListener(RecyclerViewListener recyclerViewListener) { | |
this.recyclerViewListener = recyclerViewListener; | |
} | |
@Override | |
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_student, parent, false); | |
return new ItemViewHolder(itemView); | |
} | |
@Override | |
public void onBindViewHolder(final ItemViewHolder _holder, int position) { | |
Student student = _items.get(position); | |
Log.e("position", "position : " + position); | |
_holder.txtName.setText(student.name); | |
_holder.imgStudent.setImageResource(student.imgResource); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public int getItemCount() { | |
Log.e("position", "size : " + _items.size()); | |
return _items.size(); | |
} | |
class ItemViewHolder extends RecyclerView.ViewHolder | |
implements View.OnClickListener { | |
TextView txtName; | |
ImageView imgStudent; | |
ItemViewHolder(View itemView) { | |
super(itemView); | |
itemView.setOnClickListener(this); | |
txtName = (TextView) itemView.findViewById(R.id.txtName); | |
imgStudent = (ImageView) itemView.findViewById(R.id.imgStudent); | |
} | |
@Override | |
public void onClick(View v) { | |
if (recyclerViewListener != null) | |
recyclerViewListener.onClick(v, this.getAdapterPosition()); | |
} | |
} | |
public interface RecyclerViewListener { | |
void onClick(View v, int position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment