|
| 1 | +package com.codelv.enamlnative.adapters; |
| 2 | + |
| 3 | +import android.support.v7.widget.RecyclerView; |
| 4 | +import android.view.View; |
| 5 | +import android.view.ViewGroup; |
| 6 | +import android.widget.FrameLayout; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | + |
| 10 | +/** |
| 11 | + * Created by jrm on 5/3/18. |
| 12 | + */ |
| 13 | +public class BridgedRecyclerAdapter extends RecyclerView.Adapter<BridgedRecyclerAdapter.ViewHolder> { |
| 14 | + public static final String TAG = "BridgedRecyclerAdapter"; |
| 15 | + int mCount; |
| 16 | + int mRecycleIndex = -1; // Incremented before first use |
| 17 | + RecyclerView mListView; |
| 18 | + BridgedListAdapterListener mListener; |
| 19 | + final ArrayList<View> mRecycleViews = new ArrayList<>(); |
| 20 | + |
| 21 | + // Provide a reference to the views for each data item |
| 22 | + // Complex data items may need more than one view per item, and |
| 23 | + // you provide access to all the views for a data item in a view holder |
| 24 | + public static class ViewHolder extends RecyclerView.ViewHolder { |
| 25 | + // each data item is just a string in this case |
| 26 | + public View mView; |
| 27 | + public int mIndex; |
| 28 | + public ViewHolder(View v, int i) { |
| 29 | + super(v); |
| 30 | + mView = v; |
| 31 | + mIndex = i; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public BridgedRecyclerAdapter(RecyclerView listView) { |
| 36 | + super(); |
| 37 | + mListView = listView; |
| 38 | + } |
| 39 | + |
| 40 | + // Create new views (invoked by the layout manager) |
| 41 | + @Override |
| 42 | + public BridgedRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, |
| 43 | + int viewType) { |
| 44 | + // create a new view |
| 45 | + mRecycleIndex += 1; |
| 46 | + if (mRecycleIndex==mRecycleViews.size()) { |
| 47 | + mRecycleIndex = 0; |
| 48 | + } |
| 49 | + View v = mRecycleViews.get(mRecycleIndex); |
| 50 | + ViewGroup vp = (ViewGroup) v.getParent(); |
| 51 | + if (vp!=null) { |
| 52 | + vp.removeView(v); |
| 53 | + } |
| 54 | + FrameLayout frame = new FrameLayout(parent.getContext()); |
| 55 | + frame.addView(v); |
| 56 | + return new ViewHolder(frame, mRecycleIndex); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + // Replace the contents of a view (invoked by the layout manager) |
| 61 | + @Override |
| 62 | + public void onBindViewHolder(ViewHolder holder, int position) { |
| 63 | + mListener.onRecycleView(holder.mIndex, position); |
| 64 | + } |
| 65 | + |
| 66 | + public void setRecyleListener(BridgedListAdapterListener listener) { |
| 67 | + mListener = listener; |
| 68 | +// mListView.addOnScrollListener(new AbsListView.OnScrollListener() { |
| 69 | +// @Override |
| 70 | +// public void onScrollStateChanged(AbsListView view, int scrollState) { |
| 71 | +// if (mListener!=null) { |
| 72 | +// mListener.onScrollStateChanged(view, scrollState); |
| 73 | +// } |
| 74 | +// } |
| 75 | +// |
| 76 | +// @Override |
| 77 | +// public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { |
| 78 | +// if (mListener!=null) { |
| 79 | +// // Notify if # of visible items changed |
| 80 | +// if (mVisibileCount!=visibleItemCount) { |
| 81 | +// mListener.onVisibleCountChanged(visibleItemCount, totalItemCount); |
| 82 | +// } |
| 83 | +// mVisibileCount = visibleItemCount; |
| 84 | +// } |
| 85 | +// } |
| 86 | +// }); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Set the number of items this adapter has. |
| 91 | + */ |
| 92 | + public void setItemCount(int count) { |
| 93 | + mCount = count; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public int getItemCount() { |
| 98 | + return mCount; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set the views that this adapter will cycle through. |
| 103 | + * @param views |
| 104 | + */ |
| 105 | + public void setRecycleViews(View... views) { |
| 106 | + for (View view: views) { |
| 107 | + mRecycleViews.add(view); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public void addRecyleView(View view) { |
| 112 | + mRecycleViews.add(view); |
| 113 | + } |
| 114 | + |
| 115 | + public void removeRecycleView(View view) { |
| 116 | + mRecycleViews.remove(view); |
| 117 | + } |
| 118 | + |
| 119 | + public void clearRecycleViews() { |
| 120 | + mRecycleViews.clear(); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + interface BridgedListAdapterListener { |
| 125 | + void onRecycleView(int index, int position); |
| 126 | + } |
| 127 | +} |
0 commit comments