Android Beginner 82nd Day

Source: Internet
Author: User

Android Beginner 82nd Day

20_mvvm

Code Beatboxbeatboxfragment.java
Package Com.bignerdranch.android.beatbox;import Android.databinding.DataBindingUtil;import Android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import Android.support.v7.widget.GridLayoutManager;import Android.support.v7.widget.RecyclerView;import Android.view.LayoutInflater;import Android.view.View;import Android.view.ViewGroup;import com.bignerdranch.android.beatbox.databinding.FragmentBeatBoxBinding;import com.bignerdranch.android.beatbox.databinding.ListItemSoundBinding;import java.util.List; Public classBeatboxfragmentextendsFragment {PrivateBeatbox Mbeatbox; Public StaticBeatboxfragmentnewinstance() {return New beatboxfragment(); }@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {fragmentbeatboxbinding binding = Databindingutil.Inflate(Inflater, R.Layout.Fragment_beat_box, Container,false); Binding.Recyclerview.Setlayoutmanager(New Gridlayoutmanager(getactivity(),3)); Binding.Recyclerview.Setadapter(New Soundadapter(Mbeatbox.Getsounds()));returnBinding.Getroot(); }@Override     Public void onCreate(@NullableBundle savedinstancestate) {Super.onCreate(savedinstancestate); Mbeatbox =New Beatbox(getactivity()); }Private classSoundholderextendsRecyclerview.Viewholder{PrivateListitemsoundbinding mbinding;Private Soundholder(Listitemsoundbinding binding) {Super(Binding.Getroot());            Mbinding = binding; Mbinding.Setviewmodel(New Soundviewmodel(Mbeatbox)); } Public void Bind(Sound sound) {mbinding.Getviewmodel().Setsound(sound); Mbinding.executependingbindings(); }    }Private classSoundadapterextendsRecyclerview.Adapter<SoundHolder> {PrivateList<sound> Msounds; Public Soundadapter(List<sound> sounds)        {msounds = sounds; }@Override         PublicSoundholderOncreateviewholder(ViewGroup parent,intViewType) {Layoutinflater inflater = Layoutinflater. from(getactivity()); listitemsoundbinding binding = Databindingutil.Inflate(Inflater, R.Layout.List_item_sound, parent,false);return New Soundholder(binding); }@Override         Public void Onbindviewholder(Soundholder holder,intPosition) {Sound sound = msounds.Get(position); Holder.Bind(sound); }@Override         Public int GetItemCount() {returnMsounds.size(); }    }}

Android Beginner 82nd Day

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.