public class Adapter extends RecyclerView.Adapter<MyViewHolder> { private List<ContactInfoModel> mAllContactNamesList; /** * Here data will be passed to mNames by Activity. * @param mNames will be filled/assigned by activity, and mNames will be stored in global variable * to be used for getItemCount(),setting values for each row in onBindViewHolder. */ Adapter(List<ContactInfoModel> mNames) { this.mAllContactNamesList = mNames; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // One Model row used to generate multiple rows based on list size. Then inside views will be initialized by MyViewHolder. //ఒక లేఔట్ లాగా చాలా రోస్ క్రియేట్ అవుతాయి row_layout లేయవుట్ తో . లేఔట్ లో ఉన్న వ్యూస్ మీవ్యూహోల్డర్ లో ఇనిషిలైజ్ అవుతాయి. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent,false); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { //set names for each row based on position. /* ఇక్కడ పోసిషన్ తో క్రియేట్ అయినా రోస్ కి వాల్యూ ఇవ్వబడుతుంది */ holder.mTVContactName.setText(mAllContactNamesList.get(position).getName()); } @Override public int getItemCount() { // Based on list size , recyclerview creates rows. return mAllContactNamesList.size(); }}public class ContactInfoModel { String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}public class MyViewHolder extends RecyclerView.ViewHolder { protected TextView mTVContactName; public MyViewHolder(View itemView) { super(itemView); mTVContactName = itemView.findViewById(R.id.tv_contact_name); }}activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- RecyclerView--> <android.support.v7.widget.RecyclerView android:id="@+id/contacts_rv" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>row_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- Above, height should be wrap_content, otherwise one row will be shown like very big as whole screen.--> <TextView android:id="@+id/tv_contact_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="22sp"/></LinearLayout>/** * Created by Lokesh. * There are 4 classes to setup recyclerview, Adapter, ContactInfoModel,MyViewHolder and MainActivity. */public class MainActivity extends AppCompatActivity { private RecyclerView mRVContacts; private List<ContactInfoModel> mContactModelList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRVContacts = findViewById(R.id.contacts_rv); createDummyContactsList(); setUpRecycleList(); } private void createDummyContactsList() { mContactModelList = new ArrayList<>(); ContactInfoModel member1Model = new ContactInfoModel(); member1Model.setName("Rama"); mContactModelList.add(member1Model); ContactInfoModel member2Model = new ContactInfoModel(); member2Model.setName("Krishna"); mContactModelList.add(member2Model); ContactInfoModel member3Model = new ContactInfoModel(); member3Model.setName("Hanuman"); mContactModelList.add(member3Model); //// TODO: 5/16/2018 go on for more rows.. } private void setUpRecycleList() { Adapter adapter = new Adapter(mContactModelList); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); mRVContacts.setLayoutManager(layoutManager); mRVContacts.setAdapter(adapter); }}implementation 'com.android.support:recyclerview-v7:27.1.1'