Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The RecyclerView app demonstrates how to use a RecyclerView to display a long scrollable list of words. You create the dataset (the words), the RecyclerView

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

The RecyclerView app demonstrates how to use a RecyclerView to display a long scrollable list of words. You create the dataset (the words), the RecyclerView itself, and the actions the user can take: Tapping a word marks it clicked. Tapping the floating action button (FAB) adds a new word. Resterview Word 9 Word 10 Word 11 Word 12 Word 13 Clicked! Word 14 Word 15 Clicked Word 16 Word 17 Complete the following code content main.xml activity main.xml 1/Line 1: Display RecyclerView Sandroid.support.design.widget. Floating ActionButton endres joueidth= wrap-content android:layout_height="wrap_content android:layout_gravity= bottomleng 2ppareccompat=drawable/ic_add_for_fab constraintLayout wordlist item.Xml I/Line 2: Display the WORD menu main.xml mWordList: private final LayoutInflater minflater private final LinkedList mWordList = new LinkedList0; private RecyclerView mRecyclerView, private WordListAdapter mAdapter class WordViewHolder // Line 4: define super class implements View.OnClickListener { public final TextView wordltemView, final WordListAdapter mAdapter, @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewByld(R.id.toolbar); setSupportActionBartoolbar): public WordViewHolder View itemView, WordListAdapter adapter) { super item View) word temView = itemView.findViewById(R.id.word); this.mAdapter adapter item View.setOnClickListener(this) FloatingActionButton fab = findViewByld(R.id.fab): fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Get the position of the item that was clicked. int Position = getLayout Position: @Override public void onClick(View view) { int wordListSize = mWordList size: // Add a new word to the wordList. mWordlistaddLast(+ Word - wordLisesitze) // Notify the adapter that the data has changed. mRecyclerView.getAdapter() notifyiteminserted (wordListSize // Scroll to the bottom. mRecyclerView.smoothScrollToPosition vordListSize): // Line 5: Use that position to access the affected item in mWordList. 1/ Line 6: Change the clicked word in the mWordList. Put Initia data into the word list. for (int i = 0 20- + -) { mWordList.addLast Word out the water that the data has changed so it can // Line 6: Change the clicked word in the mWordList. // Put initial data into the word list. for (int i = 0; i wordList) { minflater = Layoutinflater from(context); this.mWordList = wordList 1 // Create recycler view. mRecyclerView = findViewByld(R.id recyclerview); // Create an adapter and supply the data to be displayed mAdapter = new WordListAdapter(this, mWordList), // Connect the adapter with the recycler view. mRecyclerView.setAdapter(mAdapter): // Line 7: Give the recycler view a default layout manager. @Override public WordListAdapter WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) // Inflate an item view View mitem View = minflater.inflate R.layout wordlist_item parent false); return new Word View Holder(mitem View, this) 1 override public boolean onCreateOptions Menu(Menu menu) { 1/ line 8: Inflate the menu; this adds items to the action bar if it is present. return true Override public boolean onOptionsitemSelected(Menuitem item) ( public void orbindview Holder(WordListAdapter Wordview Holder holder int position) Retrieve the data for that position String Current mWordList get(position) Add the data to the view holder hede wordt view textim Current) int id - tam gatitemido it (id - Ridaction lettings return true @Override public boolean onOptionsItemSelected(Menultem item) { public void on BindViewHolder(WordListAdapter.WordView Holder holder, int position) { II Retrieve the data for that position. String mCurrent = mWordList.get(position); 11 Add the data to the view holder. holder.worditemView.setText(mCurrent); 3 int id = item.getItemld(); if (id == Rid.action_settings) { return true; 1 public int get temCoun:( return mWordList size: return super.onOptions temSelected item); 3 3 Recyceries you snageren Linear-ayout Vanager(this)); extends RecyclerView.Adapter wordList) { minflater = Layoutinflater from(context); this.mWordList = wordList 1 // Create recycler view. mRecyclerView = findViewByld(R.id recyclerview); // Create an adapter and supply the data to be displayed mAdapter = new WordListAdapter(this, mWordList), // Connect the adapter with the recycler view. mRecyclerView.setAdapter(mAdapter): // Line 7: Give the recycler view a default layout manager. @Override public WordListAdapter WordViewHolder onCreateViewHolder(ViewGroup parent, int viewType) // Inflate an item view View mitem View = minflater.inflate R.layout wordlist_item parent false); return new Word View Holder(mitem View, this) 1 override public boolean onCreateOptions Menu(Menu menu) { 1/ line 8: Inflate the menu; this adds items to the action bar if it is present. return true Override public boolean onOptionsitemSelected(Menuitem item) ( public void orbindview Holder(WordListAdapter Wordview Holder holder int position) Retrieve the data for that position String Current mWordList get(position) Add the data to the view holder hede wordt view textim Current) int id - tam gatitemido it (id - Ridaction lettings return true private final LinkedList mWordList: private final LayoutInflater minflater private final LinkedList mWordList = new LinkedList0; private RecyclerView mRecyclerView, private WordListAdapter mAdapter class WordViewHolder // Line 4: define super class implements View.OnClickListener { public final TextView wordltemView, final WordListAdapter mAdapter, @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewByld(R.id.toolbar); setSupportActionBartoolbar): public WordViewHolder View itemView, WordListAdapter adapter) { super item View) word temView = itemView.findViewById(R.id.word); this.mAdapter adapter item View.setOnClickListener(this) FloatingActionButton fab = findViewByld(R.id.fab): fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Get the position of the item that was clicked. int Position = getLayout Position: @Override public void onClick(View view) { int wordListSize = mWordList size: // Add a new word to the wordList. mWordlistaddLast(+ Word - wordLisesitze) // Notify the adapter that the data has changed. mRecyclerView.getAdapter() notifyiteminserted (wordListSize // Scroll to the bottom. mRecyclerView.smoothScrollToPosition vordListSize): // Line 5: Use that position to access the affected item in mWordList. 1/ Line 6: Change the clicked word in the mWordList. Put Initia data into the word list. for (int i = 0 20- + -) { mWordList.addLast Word out the water that the data has changed so it can The RecyclerView app demonstrates how to use a RecyclerView to display a long scrollable list of words. You create the dataset (the words), the RecyclerView itself, and the actions the user can take: Tapping a word marks it clicked. Tapping the floating action button (FAB) adds a new word. Resterview Word 9 Word 10 Word 11 Word 12 Word 13 Clicked! Word 14 Word 15 Clicked Word 16 Word 17 Complete the following code android.support.constraint ConstraintLayout> wordlist item.xml xml version="1.0" encoding="utf-8"?> Kinessyout ming android http://schems.moroid.com/apk/res/android" android:layout_widn="match_parent" android you hegh wep_content groentet e endoleading top> Line 2 Display the WORD menu main.xml WordListAdapter.java package com.example.android.recyclerview: MainActivity.java package com.example.android.recyclerview, import android.content.Context: import android supportv7.widget RecyclerView: import android view.layoutinflater, import android View import android.view.Vier Group: import android.widget. TextVie import android.os.Bundle import android support design.widget. FloatingActionButton: import android.support.v7.app.AppCompatActivity: import android.support.v7.widgetLinearLayoutManager import android.support.v7.widgetRecderview: import android.support.v7.widget. Toolbar import android.view.Venu: import android.view.Venultem: impoft android.view.View: import java.util.LinkedList Line: define super class public class MainActivity extends 40oComezactivity private final LinkedList mWordList private final LayoutInflater minflater, private final LinkedList mWordList = new LinkedList private RecyclerView mRecyclerView, private WordListAdapter mAdapter, class WordViewHolder 1/ Line 4: define super class implements View.OnClickListener { public final TextView wordltemView; final WordListAdapter Adapter @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity main); Toolbar toolbar = findViewById(R.id.toolbar): setSupportActionBartoolbar): public Word ViewHolder View itemView. WordListAdapter adapter) { super temView) word tem View = itemViewfindViewBy d(R.id.woro); FloatingActionButton fab = findViewById(R.id.fab): fab.setOnClickListener(ne View.OnClickListener ? @Override public void onClick(View view) { int wordistSize = Wordse size public void orciVie vies) { Ger the post on the item that was clicked. int m Besition = get outPosition(); mWord-stadd-355 - Word - |: |: :: 14 Notity the 3039 3: tages mRecyclerView.getAdapter non ten gerordis Size // Scroll to the bottom mRecyclerView.smoniero Toros tortoroz // Line 5. Use that position to access the affected item in mWordList. Il Line 5: Use that position to access the affected item in mWordlist. mRecyclerView.smoothScrollToPosition(wordListSize); } Il Line 6 Change the clicked word in the mWordList. // Put initial data into the word list. for (int i = 0; i wordList) { // Create recycler view. mRecyclerView = findViewByld(R.id.recyclerview): // Create an adapter and supply the data to be displayed. mAdapter = new WordListAdapter this mWordList): 7/ Connect the adapter with the recycler View. mRecyclerView.setAdapter(mAdapter); // Line 7: Give the recycler view a default layout manager. @Override public WordListadapter WordView Holder onCreate View Holder(ViewGroup parent int viewType) Infate an item view. View mitem View = minister inflate! R.layout wordlistjem parent, false) return new Word Viewolderi tem View this); @Override public boolean ondreateOptions Menu(Menu metu) { // Line 8: Inflate the menu: this adds items to the action bar if it is present. return true 2w. this) } return true; } @Override public boolean onOptions temSelected(Menultem item) { public void onBindViewHolder(WordListAdapter.Wordview Holder holder, int position) { // Retrieve the data for that position. String mCurrent = mWordList.get(position): Add the data to the view holder. holder.wordtemView.setText(mCurrent): int id = item.getItemld(); if (id == Rid.action_settings) { return true; override return super.on Options temSelected item)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Population

Answered: 1 week ago

Question

What are some issues surrounding the use of punishment?

Answered: 1 week ago