Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

recyclerview basic example

3 min read

First, you'll need to define your layout for each item in the RecyclerView. This is typically done using an XML file in the layout directory. Here's an example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"/>

    <TextView
            android:id="@+id/item_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"/>
            
</LinearLayout>

Next, you'll need to create a ViewHolder class to hold references to the views in your layout. Here's an example:

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title;
    public TextView description;

    public MyViewHolder(View view) {
        super(view);
        title = view.findViewById(R.id.item_title);
        description = view.findViewById(R.id.item_description);
    }
}

Then, you'll need to create an Adapter class to manage the data and bind it to the RecyclerView. Here's an example:

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private List<MyItem> items;

    public MyAdapter(List<MyItem> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        MyItem item = items.get(position);
        holder.title.setText(item.getTitle());
        holder.description.setText(item.getDescription());
    }

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

Finally, in your activity or fragment, you can create the RecyclerView and set the adapter. Here's an example:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
MyAdapter adapter = new MyAdapter(items);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

Note: The MyItem class in the MyAdapter example represents the data model for each item in the RecyclerView. You'll need to replace this with your own data model class that represents the data you want to display.

how to add data to the RecyclerView adapter:

  1. Add a method to the adapter class to add new items:
public void addItem(MyItem item) {
    items.add(item);
    notifyItemInserted(items.size() - 1);
}
  1. In your activity or fragment, create a new instance of MyItem and add it to the adapter:
MyItem newItem = new MyItem("New Item Title", "New Item Description");
adapter.addItem(newItem);

This will add a new item to the end of the list in the RecyclerView and notify the adapter that the data set has changed so it can update the UI.

You may like these posts

  • ImageSmoothing in Android import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawab…
  • NoiseRemover in Android import android.graphics.Bitmap; public class NoiseRemover { // Function to remove noise from image public static Bitmap removeNoise(Bitmap b…
  • ImageCompressor in Android import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.util.Log; import java.io.Byte…
  • EdgeDetectionAutoSkewCorrectionView in Android import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.…
  • BlurImageView in Android import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.g…
  • TouchTuner in Android import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Re…

Post a Comment