SHARE
TWEET

Untitled

a guest Nov 12th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ListFragment : Fragment() {
  2.  
  3.     private lateinit var books : Array<Book>
  4.  
  5.     private lateinit var recyclerView: RecyclerView
  6.     private lateinit var viewAdapter: RecyclerView.Adapter<*>
  7.     private lateinit var viewManager: RecyclerView.LayoutManager
  8.     private lateinit var listener: BookOnClickListener
  9.  
  10.     companion object {
  11.         fun newInstance(book : Array<Book>) : ListFragment{
  12.             val myFragment = ListFragment()
  13.  
  14.             val args = Bundle()
  15.             args.putParcelableArray("books", book)
  16.             myFragment.arguments = (args)
  17.  
  18.             return myFragment
  19.         }
  20.     }
  21.  
  22.     override fun onAttach(context: Context?) {
  23.         super.onAttach(context)
  24.         listener= context as BookOnClickListener
  25.     }
  26.  
  27.     override fun onCreateView(
  28.         inflater: LayoutInflater, container: ViewGroup?,
  29.         savedInstanceState: Bundle?
  30.     ): View {
  31.  
  32.         val view = inflater.inflate(R.layout.list_fragment, container, false)
  33.  
  34.         books = arguments?.getParcelableArray("books") as Array<Book>
  35.  
  36.         viewManager = LinearLayoutManager(context)
  37.  
  38.         viewAdapter = BookListAdapter(books, listener::onClick)
  39.         recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView).apply {
  40.             // use this setting to improve performance if you know that changes
  41.             // in content do not change the layout size of the RecyclerView
  42.             setHasFixedSize(true)
  43.  
  44.             // use a linear layout manager
  45.             layoutManager = viewManager
  46.  
  47.             // specify an viewAdapter (see also next example)
  48.             adapter = viewAdapter
  49.         }
  50.  
  51.         return view
  52.     }
  53.  
  54.     override fun onActivityCreated(savedInstanceState: Bundle?) {
  55.         super.onActivityCreated(savedInstanceState)
  56.     }
  57.  
  58.     interface BookOnClickListener {
  59.         fun onClick(book: Book)
  60.     }
  61.  
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top