stack overflow recyclerview
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.recyclerview_row, 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);
}
}DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);<?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">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvAnimals"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>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("Horse");
animalNames.add("Cow");
animalNames.add("Camel");
animalNames.add("Sheep");
animalNames.add("Goat");
// 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();
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/tvAnimalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
Also in Java:
- Title
- what is java plug-in
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- What is returned by a producer.send() call in the Java API? A boolean indicating if the call succeeded Future object Future object Unit
- Category
- Java
- Title
- java arraylist
- Category
- Java
- Title
- determine if a given binary tree is a valid bst
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- java lerp
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- remove extra blank spaces from string in java
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- java parse json
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- java random seed
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- how to loop through an array
- Category
- Java
- Title
- how to create a Rectangle in java
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- how to return the first character in an array from a method java
- Category
- Java
- Title
- java find duplicates in array
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- java how to print a string[]
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- spigot title
- Category
- Java
- Title
- java get keys from hashmap
- Category
- Java
- Title
- java find biggest number in array
- Category
- Java
- Title
- java mysql springboot jpa stackoverflow
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- how to create a 2d arraylist java
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- linux command to see all the java version installed
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- count word in string no matter the delimiter java
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- java script find screen size of device
- Category
- Java
- Title
- java dictionary
- Category
- Java
- Title
- how to create a JFrame in java
- Category
- Java
- Title
- how to find the total of the products added to the shopping cart in java program
- Category
- Java
- Title
- java write arraylist of objects to file
- Category
- Java
- Title
- string to long java
- Category
- Java
- Title
- long input in JAVA
- Category
- Java
- Title
- print map java
- Category
- Java
- Title
- taking string input in java
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- nth prime number java
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- initialize arraylist
- Category
- Java
- Title
- transition java fx
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- double parse jtextfield
- Category
- Java
- Title
- teimpo en segundos java
- Category
- Java
- Title
- como detener un void java
- Category
- Java
- Title
- indexof java
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- how to find the intersection of two rectangles in java
- Category
- Java
- Title
- How to loop through objects in java using streams
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- add element to stack java
- Category
- Java
- Title
- java stack empty
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- split by new line java
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- store string elements in character array in java
- Category
- Java
- Title
- java jagged array days and months
- Category
- Java
- Title
- how to get orientation lock to portrait android stackoverflow
- Category
- Java
- Title
- java find item in list by property
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- insert element into arraylist java
- Category
- Java
- Title
- java new string with values
- Category
- Java
- Title
- treeset java descending order using comparator
- Category
- Java
- Title
- bukkit java get max players
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- how to change maven java version in po,
- Category
- Java
- Title
- char array to arraylist java
- Category
- Java
- Title
- partial view tiles spring
- Category
- Java
- Title
- java android build secret keys
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- get method of a class which I only have string to
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java
- Title
- java syntax
- Category
- Java
- Title
- coding with WDSL spring
- Category
- Java
- Title
- switch statement in apex
- Category
- Java
- Title
- string to int java
- Category
- Java
- Title
- list java replace
- Category
- Java
- Title
- java boolean even number
- Category
- Java