Recyclerview steps

in the main activity, add recycler view

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView" />

</androidx.constraintlayout.widget.ConstraintLayout>

in the layout file, add a layout to add the items to display item_list, make the layout as MaterialCardView. Add linear layout and add the required stuffs with match parent, gravity center, 

1 textview and 3 imageview with width 0, height 50

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.14" />

<ImageView
android:id="@+id/whatsapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.169"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.286"
tools:srcCompat="@tools:sample/avatars" />

<ImageView
android:id="@+id/copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.943"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499"
tools:srcCompat="@tools:sample/avatars" />

<ImageView
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.116"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.902"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>

create a model class shayariData

private string text

create constructor

create getter and setter

public class ShayariData {

private String text;

public ShayariData(String text) {
this.text = text;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}
}

1. create a adapter

public class ShayariViewHolder {

}

2. create a shayari.ViewHolder inside the adapter

public class ShayariViewHolder extends RecyclerView.ViewHolder {

}

3. extend shayariadapter with Adapter class

public class ShayariAdapter extends RecyclerView.Adapter<ShayariAdapter.ShayariViewHolder> {

4. implement the methods

@NonNull
@Override
public ShayariViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}

@Override
public void onBindViewHolder(@NonNull ShayariViewHolder holder, int position) {

}

@Override
public int getItemCount() {
return 0;
}

5. implement the viewholder to create a constructor

public class ShayariViewHolder extends RecyclerView.ViewHolder {

public ShayariViewHolder(@NonNull View itemView) {
super(itemView);
}
}

6. create variables context, arraylist

private Context context;
private ArrayList<ShayariData> list;

Create a constructor for adapter

public ShayariAdapter(Context context, ArrayList<ShayariData> list) {
this.context = context;
this.list = list;
}

Now, inflate the view in the oncreateViewHolder

public ShayariViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
return new ShayariViewHolder(view);
}

Now initialise the view parameters in view holder

public class ShayariViewHolder extends RecyclerView.ViewHolder {

private ImageView whatsapp, share, copy;
private TextView textview;

public ShayariViewHolder(@NonNull View itemView) {
super(itemView);
whatsapp = itemView.findViewById(R.id.whatsapp);
share = itemView.findViewById(R.id.share);
copy = itemView.findViewById(R.id.copy);
textview = itemView.findViewById(R.id.textView);
}
}

now getItemCount

@Override
public int getItemCount() {
return list.size();
}

Now in onBindViewHolder()

@Override
public void onBindViewHolder(@NonNull ShayariViewHolder holder, int position) {
holder.textview.setText(list.get(position).getText());
}

Final Adapter Class

public class ShayariAdapter extends RecyclerView.Adapter<ShayariAdapter.ShayariViewHolder> {

private Context context;
private ArrayList<ShayariData> list;

public ShayariAdapter(Context context, ArrayList<ShayariData> list) {
this.context = context;
this.list = list;
}

@NonNull
@Override
public ShayariViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
return new ShayariViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ShayariViewHolder holder, int position) {
holder.textview.setText(list.get(position).getText());
}

@Override
public int getItemCount() {
return list.size();
}

public class ShayariViewHolder extends RecyclerView.ViewHolder {

private ImageView whatsapp, share, copy;
private TextView textview;

public ShayariViewHolder(@NonNull View itemView) {
super(itemView);
whatsapp = itemView.findViewById(R.id.whatsapp);
share = itemView.findViewById(R.id.share);
copy = itemView.findViewById(R.id.copy);
textview = itemView.findViewById(R.id.textView);
}
}
}

Now in the activity class,

Add recyclerview and arraylist.

initialise both of them in oncreate()

set the layout manager for the recyclerview

set the adapter to the recyclerview

public class ShayariActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ArrayList<ShayariData> list;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
//Set layout manager
recyclerView.setLayoutManager(new LinearLayoutManager(this));

// Initialise the list
list = new ArrayList<>();
// Load the data to the list
initList();

//set the adapter
recyclerView.setAdapter(new ShayariAdapter(this, list));
}

private void initList() {
list.add(new ShayariData("sdfbjhsdbfjsdbz"));
list.add(new ShayariData("sdfbjhsdbfjsdbz"));
list.add(new ShayariData("sdfbjhsdbfjsdbz"));
}
}









Comments

Popular posts from this blog

Android - Using KeyStore to encrypt and decrypt the data

Stack and Queue

Java Reflection API