30
30
31
31
import android .support .v7 .widget .RecyclerView ;
32
32
import android .util .Log ;
33
+ import android .view .LayoutInflater ;
34
+ import android .view .View ;
35
+ import android .view .ViewGroup ;
33
36
34
37
import com .firebase .client .ChildEventListener ;
35
38
import com .firebase .client .DataSnapshot ;
36
39
import com .firebase .client .Firebase ;
37
40
import com .firebase .client .FirebaseError ;
38
41
import com .firebase .client .Query ;
39
42
43
+ import java .lang .reflect .Constructor ;
44
+ import java .lang .reflect .InvocationTargetException ;
40
45
import java .util .ArrayList ;
41
46
import java .util .HashMap ;
42
47
import java .util .List ;
51
56
*/
52
57
public abstract class FirebaseRecyclerViewAdapter <T , VH extends RecyclerView .ViewHolder > extends RecyclerView .Adapter <VH > {
53
58
54
- FirebaseArray mSnapshots ;
55
59
Class <T > mModelClass ;
60
+ protected int mModelLayout ;
61
+ Class <VH > mViewHolderClass ;
62
+ FirebaseArray mSnapshots ;
56
63
protected RecyclerViewClickListener clickListener ;
57
64
58
65
@@ -61,8 +68,10 @@ public abstract class FirebaseRecyclerViewAdapter<T, VH extends RecyclerView.Vie
61
68
* combination of <code>limit()</code>, <code>startAt()</code>, and <code>endAt()</code>,
62
69
* @param modelClass Firebase will marshall the data at a location into an instance of a class that you provide
63
70
*/
64
- public FirebaseRecyclerViewAdapter (Query ref , Class <T > modelClass ) {
71
+ public FirebaseRecyclerViewAdapter (Query ref , Class <T > modelClass , int modelLayout , Class < VH > viewHolderClass ) {
65
72
mModelClass = modelClass ;
73
+ mModelLayout = modelLayout ;
74
+ mViewHolderClass = viewHolderClass ;
66
75
mSnapshots = new FirebaseArray (ref );
67
76
68
77
mSnapshots .setOnChangedListener (new FirebaseArray .OnChangedListener () {
@@ -114,6 +123,30 @@ public void setClickListener(RecyclerViewClickListener clickListener) {
114
123
this .clickListener = clickListener ;
115
124
}
116
125
126
+ @ Override
127
+ public VH onCreateViewHolder (ViewGroup parent , int viewType ) {
128
+ ViewGroup view = (ViewGroup ) LayoutInflater .from (parent .getContext ()).inflate (mModelLayout , parent , false );
129
+ try {
130
+ Constructor <VH > constructor = mViewHolderClass .getConstructor (View .class );
131
+ return constructor .newInstance (view );
132
+ } catch (NoSuchMethodException e ) {
133
+ throw new RuntimeException (e );
134
+ } catch (InvocationTargetException e ) {
135
+ throw new RuntimeException (e );
136
+ } catch (InstantiationException e ) {
137
+ throw new RuntimeException (e );
138
+ } catch (IllegalAccessException e ) {
139
+ throw new RuntimeException (e );
140
+ }
141
+ }
142
+ @ Override
143
+ public void onBindViewHolder (VH viewHolder , int i ) {
144
+ T model = getItem (i );
145
+ populateViewHolder (viewHolder , model );
146
+ }
147
+
148
+ abstract public void populateViewHolder (VH viewHolder , T model );
149
+
117
150
public interface RecyclerViewClickListener {
118
151
public void onItemClicked (int position );
119
152
}
0 commit comments