How to stack overflow recyclerview (Java Programming Language)

Asked by: King Gerstel
Edit Question Icon
Edit
Follow Icon
Report Icon
Report

Explain Below What Do You Want to report!


Available Answers: 5

Dom Merry (THROWER Revenue Growth, Analyze Data, Matrix) From: Thiruvananthapuram/India
1
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
2
    private List<String> mData;
3
    private LayoutInflater mInflater;
4
    private ItemClickListener mClickListener;
5
    // data is passed into the constructor
6
    MyRecyclerViewAdapter(Context context, List<String> data) {
7
        this.mInflater = LayoutInflater.from(context);
8
        this.mData = data;
9
    }
10
    // inflates the row layout from xml when needed
11
    @Override
12
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
13
        View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
14
        return new ViewHolder(view);
15
    }
16
    // binds the data to the TextView in each row
17
    @Override
18
    public void onBindViewHolder(ViewHolder holder, int position) {
19
        String animal = mData.get(position);
20
        holder.myTextView.setText(animal);
21
    }
22
    // total number of rows
23
    @Override
24
    public int getItemCount() {
25
        return mData.size();
26
    }
27
    // stores and recycles views as they are scrolled off screen
28
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
29
        TextView myTextView;
30
        ViewHolder(View itemView) {
31
            super(itemView);
32
            myTextView = itemView.findViewById(R.id.tvAnimalName);
33
            itemView.setOnClickListener(this);
34
        }
35
        @Override
36
        public void onClick(View view) {
37
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
38
        }
39
    }
40
    // convenience method for getting data at click position
41
    String getItem(int id) {
42
        return mData.get(id);
43
    }
44
    // allows clicks events to be caught
45
    void setClickListener(ItemClickListener itemClickListener) {
46
        this.mClickListener = itemClickListener;
47
    }
48
    // parent activity will implement this method to respond to click events
49
    public interface ItemClickListener {
50
        void onItemClick(View view, int position);
51
    }
52
}
Upvote Icon10
Comment IconComment
Follow Icon
Report IconReport

Explain Below What Do You Want to report!


Explain Below What Do You Want to comment!


Arpit nbvxlwt (SHEEP SHEARER Financial Analysis, Strategic Planning, Consulting Experience) From: Chennai/India
1
<?xml version=1.0 encoding=utf-8?>
2
<RelativeLayout
3
    xmlns:android=http://schemas.android.com/apk/res/android
4
    android:layout_width=match_parent
5
    android:layout_height=match_parent>
6
    <android.support.v7.widget.RecyclerView
7
        android:id=@+id/rvAnimals
8
        android:layout_width=match_parent
9
        android:layout_height=match_parent/>
10
</RelativeLayout>
Upvote Icon10
Comment IconComment
Follow Icon
Report IconReport

Explain Below What Do You Want to report!


Explain Below What Do You Want to comment!


Verger Nirmal (IRRIGATOR HEAD Human Resources, Health, Quality Management, Html5) From: Newport/Wales
1
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
2
    MyRecyclerViewAdapter adapter;
3
    @Override
4
    protected void onCreate(Bundle savedInstanceState) {
5
        super.onCreate(savedInstanceState);
6
        setContentView(R.layout.activity_main);
7
        // data to populate the RecyclerView with
8
        ArrayList<String> animalNames = new ArrayList<>();
9
        animalNames.add(Horse);
10
        animalNames.add(Cow);
11
        animalNames.add(Camel);
12
        animalNames.add(Sheep);
13
        animalNames.add(Goat);
14
        // set up the RecyclerView
15
        RecyclerView recyclerView = findViewById(R.id.rvAnimals);
16
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
17
        adapter = new MyRecyclerViewAdapter(this, animalNames);
18
        adapter.setClickListener(this);
19
        recyclerView.setAdapter(adapter);
20
    }
21
    @Override
22
    public void onItemClick(View view, int position) {
23
        Toast.makeText(this, You clicked  + adapter.getItem(position) +  on row number  + position, Toast.LENGTH_SHORT).show();
24
    }
25
}
Upvote Icon9
Comment IconComment
Follow Icon
Report IconReport

Explain Below What Do You Want to report!


Explain Below What Do You Want to comment!


Waleed viszl Oishi (BREAK OFF WORKER Computer Applications, Lan) From: Chicago/United States
1
<?xml version=1.0 encoding=utf-8?>
2
<LinearLayout
3
    xmlns:android=http://schemas.android.com/apk/res/android
4
    android:layout_width=match_parent
5
    android:layout_height=wrap_content
6
    android:orientation=horizontal
7
    android:padding=10dp>
8
    <TextView
9
        android:id=@+id/tvAnimalName
10
        android:layout_width=wrap_content
11
        android:layout_height=wrap_content
12
        android:textSize=20sp/>
13
</LinearLayout>
Upvote Icon2
Comment IconComment
Follow Icon
Report IconReport

Explain Below What Do You Want to report!


Explain Below What Do You Want to comment!


Seguin Shashikant (ULTRASOUND TECHNOLOGIST Merchandising, Sap) From: Meerut/India
1
implementation 'com.android.support:appcompat-v7:28.0.0'
2
implementation 'com.android.support:recyclerview-v7:28.0.0'
Upvote Icon1
Comment IconComment
Follow Icon
Report IconReport

Explain Below What Do You Want to report!


Explain Below What Do You Want to comment!




More Questions


Would you like to join Ask Sawal!

Why?

  1. Ask Sawal is a fast growing question and answer discussion forum.

  2. 15 lakh+ questions were answered by our Ask Sawal Members.

  3. Each day 1000s of questions asked & 1000s of questions answered.

  4. Ask any question and get answer from 2.5 Lakh+ Ask Sawal Members.

  5. Constant moderation and reporting option makes questions and answers spam free.

  6. And also, we have free blogging platform. Write an article on any topic.

  7. We have 10000+ visitors each day. So a beneficial platform for link building.

  8. We are allowing link sharing. Create backlinks to your blog site or any site.

  9. Gain extra passive income by sharing your affiliate links in articles and answers.