Without any further adooooo let's create a recycler view in android studio!
To create a recycler view in android we have to declare it in our layout file like this:
activity_main.xml
<RelativeLayout 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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Recycler View need an adapter class to bind our data with the defined layout in our main layout :P
The Adapter class looks like this:
RecyclerAdpater.class
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context mContext;
List<String> listItems;
public RecyclerAdapter(Context mContext, List<String> listItems) {
this.mContext = mContext;
this.listItems = listItems;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflator = LayoutInflater.from(mContext);
View view = inflator.inflate(R.layout.recycler_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bind(listItems,position);
}
@Override
public int getItemCount() {
return listItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
public void bind(List<String> listItems, int position) {
textView.setText(listItems.get(position));
}
}
}
Here we are binding a list of String with textviews which will get reflect on our main layout.
The data contain in the list :
listItems.add("Item 1");
listItems.add("Item 2");
listItems.add("Item 3");
listItems.add("Item 4");
listItems.add("Item 5");
listItems.add("Item 6");
listItems.add("Item 7");
listItems.add("Item 8");
listItems.add("Item 9");
listItems.add("Item 10");
listItems.add("Item 11");
listItems.add("Item 12");
listItems.add("Item 13");
listItems.add("Item 14");
listItems.add("Item 15");
listItems.add("Item 16");
listItems.add("Item 17");
listItems.add("Item 18");
listItems.add("Item 19");
listItems.add("Item 20");
Now in our main Activity we have to define the recyclerView and adapter along with the data we have to show in our recycler View, like this:
MainActivity.class
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerAdapter adapter;
List<String> listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
listItems = new ArrayList<>();
listItems.add("Item 1");
listItems.add("Item 2");
listItems.add("Item 3");
listItems.add("Item 4");
listItems.add("Item 5");
listItems.add("Item 6");
listItems.add("Item 7");
listItems.add("Item 8");
listItems.add("Item 9");
listItems.add("Item 10");
listItems.add("Item 11");
listItems.add("Item 12");
listItems.add("Item 13");
listItems.add("Item 14");
listItems.add("Item 15");
listItems.add("Item 16");
listItems.add("Item 17");
listItems.add("Item 18");
listItems.add("Item 19");
listItems.add("Item 20");
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerAdapter(getApplicationContext(),listItems);
recyclerView.setAdapter(adapter);
}
}
That's how we create a recycler View in android studio :)
Comments
Post a Comment