今天的任务为进行简单的数据绑定,代码如下:
app/build.gradle:
apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.bignerdranch.android.beatbox" minSdkVersion 19 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dataBinding{ enabled=true } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation 'com.android.support:recyclerview-v7:27.1.1' }
fragment_beat_box.xml:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </layout>
BeatBoxFragment.java:
package com.bignerdranch.android.beatbox; import com.bignerdranch.android.beatbox.databinding.FragmentBeatBoxBinding; import com.bignerdranch.android.beatbox.databinding.ListItemSoundBinding; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.annotation.NonNull; 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 java.util.List; public class BeatBoxFragment extends Fragment { private BeatBox mBeatBox; public static BeatBoxFragment newInstance(){ return new BeatBoxFragment(); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); mBeatBox=new BeatBox(getActivity()); } @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()); binding.recyclerView.setAdapter(new SoundAdapter(mBeatBox.getSounds())); return binding.getRoot(); } private class SoundHolder extends RecyclerView.ViewHolder{ private ListItemSoundBinding mBinding; public SoundHolder(ListItemSoundBinding binding) { super(binding.getRoot()); mBinding=binding; } } 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) { } @Override public int getItemCount() { return mSounds.size(); } } }
list_item_sound.xml:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <Button android:layout_width="match_parent" android:layout_height="120dp" tools:text="Sound name"/> </layout>