Git Product home page Git Product logo

android_recyclerview_list's Introduction

Android_RecyclerView_List

Introduction

In this project, we'll be changing some of the scrollviews from previous projects to Recycler Views. This will give you more practice building up the Recycler View and less time building the foundation of the app.

Instructions

Part 1 - Favorite Pictures Gallery

For this project, I'll walk you through the transition

Prep Project

  1. Open project structure under File -> Project Structure
  2. Click "Dependencies"
  3. Click the plus under "All Dependencies" and select "Library Dependency"
  4. Type "recyclerview" in the search box and select "androidx.recyclerview" on the left and the latest version on the right

List Item Layout

  1. Decide what information from the ImageData method would be useful to display in the List Activity
  2. Build a layout to display that content called image_item_layout (this is up to you to decide what you think looks good, have fun and play around with views and attributes)
  3. Give each element in this layout an id to be able to access it in the list adapter

Build List Adapter

  1. Create a class called ImageListAdapter that extends RecyclerView.Adapter<ImageListAdapter.ViewHolder>
  2. Create an Inner Class called ViewHolder which extends RecyclerView.ViewHolder

Use the colon ':' symbol to extend classes in Kotlin

  1. Add a constructor to the ViewHolder that accepts a View object
  2. Add data members to this ViewHolder class for each of the views in your item layout
  3. Pass the View object parameter to the parent class by adding (view) to the extension statement
  4. Attach all the views in the layout to the corresponding data members using view.view_id
  5. Add a constructor to your ImageListAdapter that accepts a MutableList of ImageData objects
  6. Override the onCreateViewHolder method

The signature should look like this override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageListAdapter.ViewHolder

  1. Inflate your item layout to a View object using LayoutInflater.from(context).inflate(R.layout.image_list_view, parent, false);
  2. Pass this View object to a ViewHolder constructor and return the new ViewHolder object
  3. Override the onBindViewHolder method using the signature override fun onBindViewHolder(holder: ImageListAdapter.ViewHolder, position: Int)
  4. Use the passed position parameter to get the ImageData object at that position of the ArrayList
  5. Take that object and use it to populate your layout

Remember, you have already bound the elements of your layout to your ViewHolder object and can now access them through the passed instance of that object, holder.textImageName.text = data.getName()
Be sure to move your OnClickListener from the TextView generator in your MainActivity to this method. Remember to use context.startActivity to start the details activity

Attach List To Activity

Now that we have built the components for our RecyclerView, it's time to bolt it all together

  1. Change your layout xml file to remove the ScrollView and LinearLayout and replace them with a single RecyclerView
  2. Add data members for a RecyclerView, LayoutManager (You can pick what you want, Linear, Grid, or Staggered), and your ImageListAdapter
  3. In your OnCreate method, get a handle to your RecyclerView
  4. Call setHasFixedSize(true) on your RecyclerView object
  5. Create a new LayoutManager of the type you chose before. Check the corresponding documentation for information on the constructors for your chosen manager
  6. Set the layoutManager property of your RecyclerView to your new LayoutManager
  7. Create a new instance of your ImageListAdapter, passing it the list of ImageData objects that you created last time
  8. Set the adapter on your RecyclerView, to the instance of ImageListAdapter

Handle List Updates

Since we are going to be manually adding elements to the list, we'll have to notify the ListAdapter when we do so.

  1. In your onActivityResult method, after adding an image to the list, call notifyItemInserted() on the listadapter and pass it the index of the element you just added (size - 1)
  2. Check to make sure that if there are any other times where you edit elements in the array you are calling either notifyItemInserted() with the affected element or notifyItemInserted() without passing anything

Test Your App

Make sure the images you add are added to the ui and that you can select them to go into the details view

Challenge

Chose another app which we have built or one you build on your own and implement a RecyclerView in it.

android_recyclerview_list's People

Contributors

chancepayne avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.