// Step 1: Add RecyclerView dependency in your build.gradle file
dependencies {
implementation 'androidx.recyclerview:recyclerview:1.2.1'
}
// Step 2: Create a layout for RecyclerView in XML (res/layout/activity_main.xml)
// Step 3: Create a ViewHolder class to hold the view elements
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
// Step 4: Create an Adapter class for RecyclerView
public class MyAdapter extends RecyclerView.Adapter {
private List dataList;
public MyAdapter(List dataList) {
this.dataList = dataList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(dataList.get(position));
}
@Override
public int getItemCount() {
return dataList.size();
}
}
// Step 5: Setup RecyclerView in your Activity (MainActivity.java)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
List myData = new ArrayList<>();
myData.add("Item 1");
myData.add("Item 2");
myData.add("Item 3");
MyAdapter adapter = new MyAdapter(myData);
recyclerView.setAdapter(adapter);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?