Android Studio Recyclerview Example Simple Data



RecyclerView ক্লাসটি ViewGroup ক্লাসকে প্রসারিত করে এবং ScrollingView ইন্টারফেস প্রয়োগ করে। এটি মার্শম্যালোতে চালু করা হয়েছে। এটি উন্নত কর্মক্ষমতা এবং অন্যান্য সুবিধা সহ ListView এর একটি উন্নত সংস্করণ। রিসাইক্লারভিউ বেশিরভাগই অ্যান্ড্রয়েড অ্যাপ্লিকেশনের তালিকা এবং গ্রিডগুলির উপর সূক্ষ্ম-শস্য নিয়ন্ত্রণ সহ ব্যবহারকারীর ইন্টারফেস ডিজাইন করতে ব্যবহৃত হয়।



01 Step ( Add Library For Dependencies) build.gradle

implementation "androidx.recyclerview:recyclerview:1.2.1"implementation "androidx.cardview:cardview:1.0.0"

02 Step ( Create item View Layout) \app\src\main\res\layout\Itemview.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:background="@color/black"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:padding="10dp">    <TextView        android:padding="10dp"        android:id="@+id/tvAnimalName"        android:textColor="@color/white"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="20sp"/></LinearLayout>

03 Step (Create Adapter Class ) \app\src\main\java\com\app\MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {    private List<String> mData;    private LayoutInflater mInflater;    private ItemClickListener mClickListener;    // data is passed into the constructor    MyRecyclerViewAdapter(Context context, List<String> data) {        this.mInflater = LayoutInflater.from(context);        this.mData = data;    }    // inflates the row layout from xml when needed    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view = mInflater.inflate(R.layout.row_design, parent, false);        return new ViewHolder(view);    }    // binds the data to the TextView in each row    @Override    public void onBindViewHolder(ViewHolder holder, int position) {        String animal = mData.get(position);        holder.myTextView.setText(animal);    }    // total number of rows    @Override    public int getItemCount() {        return mData.size();    }    // stores and recycles views as they are scrolled off screen    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {        TextView myTextView;        ViewHolder(View itemView) {            super(itemView);            myTextView = itemView.findViewById(R.id.tvAnimalName);            itemView.setOnClickListener(this);        }        @Override        public void onClick(View view) {            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());        }    }    // convenience method for getting data at click position    String getItem(int id) {        return mData.get(id);    }    // allows clicks events to be caught    void setClickListener(ItemClickListener itemClickListener) {        this.mClickListener = itemClickListener;    }    // parent activity will implement this method to respond to click events    public interface ItemClickListener {        void onItemClick(View view, int position);    }}

04 Step (Layout your View) \app\src\main\res\layout\activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <androidx.recyclerview.widget.RecyclerView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/rvAnimals"/></RelativeLayout>

05 Step (Java Activity your View) \app\src\main\java\com\app\MainActivity.java

public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {    MyRecyclerViewAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // data to populate the RecyclerView with        ArrayList<String> animalNames = new ArrayList<>();        animalNames.add("BD124.com");        animalNames.add("isoftq.com");        animalNames.add("Ohidulfb.com");        animalNames.add("Facebook.com");        animalNames.add("Banglank.com");        animalNames.add("Kotalipara.com");        animalNames.add("codestar.com.bd");        // set up the RecyclerView        RecyclerView recyclerView = findViewById(R.id.rvAnimals);        recyclerView.setLayoutManager(new LinearLayoutManager(this));        adapter = new MyRecyclerViewAdapter(this, animalNames);        adapter.setClickListener(this);        recyclerView.setAdapter(adapter);    }    @Override    public void onItemClick(View view, int position) {        Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();    }}