Android初學第82天

來源:互聯網
上載者:User

標籤:abi   box   package   new   TE   .data   cycle   cut   tla   

Android初學第82天

20_MVVM

代碼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 class BeatBoxFragment extends Fragment {    private BeatBox mBeatBox;    public static BeatBoxFragment newInstance() {        return new BeatBoxFragment();    }    @Override    public View onCreateView(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()));        return binding.getRoot();    }    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mBeatBox = new BeatBox(getActivity());    }    private class SoundHolder extends RecyclerView.ViewHolder {        private ListItemSoundBinding 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 class SoundAdapter extends RecyclerView.Adapter<SoundHolder> {        private List<Sound> mSounds;        public SoundAdapter(List<Sound> sounds) {            mSounds = sounds;        }        @Override        public SoundHolder onCreateViewHolder(ViewGroup parent, int viewType) {            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, int position) {            Sound sound = mSounds.get(position);            holder.bind(sound);        }        @Override        public int getItemCount() {            return mSounds.size();        }    }}

Android初學第82天

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.