Hii All.. this example is how to use RecyclerView in android studio.Here we will use Model class with setter and getter of all inputs. For this create new Project.
Step 1:-Create a model class inside your package where MainActivity class is there.Here we will show image,name and phone number.So model will be like following :-
Step 1:-Create a model class inside your package where MainActivity class is there.Here we will show image,name and phone number.So model will be like following :-
public class ModelClass { int image; String name; String phpneNo; public ModelClass(int image, String name, String phpneNo) { this.image = image; this.name = name; this.phpneNo = phpneNo; } public int getImage() { return image; } public void setImage(int image) { this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhpneNo() { return phpneNo; } public void setPhpneNo(String phpneNo) { this.phpneNo = phpneNo; } }Step 2:-Create another Adapter class to add each row with data.
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewolder> { ArrayList<ModelClass> modelClassList; MainActivity mainActivity; public AdapterClass(ArrayList<ModelClass> modelClassList, MainActivity mainActivity) { this.modelClassList = modelClassList; this.mainActivity = mainActivity; } //this is for adding view..
@Override
public AdapterClass.MyViewolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false); MyViewolder viewolder=new MyViewolder(mainActivity,modelClassList,view); return viewolder; } @Override
public void onBindViewHolder(AdapterClass.MyViewolder holder, int position) { ModelClass modelClass=modelClassList.get(position); holder.img.setImageResource(modelClass.getImage()); holder.name.setText(modelClass.getName()); holder.phone.setText(modelClass.getPhpneNo()); } @Override
public int getItemCount() { return modelClassList.size(); } //class for view holding the view.. public class MyViewolder extends RecyclerView.ViewHolder{ ImageView img; TextView name,phone; ArrayList<ModelClass> list; Context c; public MyViewolder(Context ctx,ArrayList<ModelClass> list,View itemView) { super(itemView); this.list=list; this.c=ctx; img=(ImageView)itemView.findViewById(R.id.image); name=(TextView)itemView.findViewById(R.id.name); phone=(TextView)itemView.findViewById(R.id.phone); } } }Step 3-row_item.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"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"> <ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="70dp"
android:layout_weight="1"
android:paddingRight="20dp" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:orientation="vertical"> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" /> <TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+9876567" /> </LinearLayout> </LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimaryDark"></View> </LinearLayout>Step 4:- main_activity.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"> <android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"></android.support.v7.widget.RecyclerView> </LinearLayout>Step 5:-MainActivity.java:-
public class MainActivity extends AppCompatActivity { RecyclerView mRrecycler; int[] imageStr={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher}; String[] strName={"Supriya","Navin","Saurabh","Ankit","manju","Rahul","Radha"}; String[] strNumber={"+91111111","+911111112","+911111113","+911111114","+911111115","+911111116","+91111117"}; ArrayList<ModelClass> arrayList=new ArrayList<ModelClass>(); RecyclerView.LayoutManager manager; AdapterClass adapterClass; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRrecycler=(RecyclerView)findViewById(R.id.recycler); //add data to the model class.
int i=0; for(String s:strName){ ModelClass modelClass=new ModelClass(imageStr[i],strName[i],strNumber[i]); arrayList.add(modelClass); i++; } manager=new LinearLayoutManager(this); mRrecycler.setLayoutManager(manager); adapterClass=new AdapterClass(arrayList,this); mRrecycler.setAdapter(adapterClass); mRrecycler.setHasFixedSize(true); } }Step 6:- Screen Shorts:-
No comments:
Post a Comment