77import android .view .LayoutInflater ;
88import android .view .ViewGroup ;
99
10- import com .waynell .videolist .demo .model .VideoListItem ;
11- import com .waynell .videolist .visibility .calculator .DefaultSingleItemCalculatorCallback ;
10+ import com .waynell .videolist .demo .model .VideoItem ;
1211import com .waynell .videolist .visibility .calculator .SingleListViewItemActiveCalculator ;
13- import com .waynell .videolist .visibility .scroll .ItemsPositionGetter ;
12+ import com .waynell .videolist .visibility .items .ListItem ;
13+ import com .waynell .videolist .visibility .scroll .ItemsProvider ;
1414import com .waynell .videolist .visibility .scroll .RecyclerViewItemPositionGetter ;
1515
1616import java .util .ArrayList ;
@@ -26,12 +26,7 @@ public class MainActivity extends AppCompatActivity {
2626
2727 private int mScrollState ;
2828
29- private ItemsPositionGetter mItemsPositionGetter ;
30-
31- private List <VideoListItem > mListItems = new ArrayList <>();
32-
33- private SingleListViewItemActiveCalculator mCalculator = new SingleListViewItemActiveCalculator (new
34- DefaultSingleItemCalculatorCallback (), mListItems );
29+ private SingleListViewItemActiveCalculator mCalculator ;
3530
3631 private static final String url = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" ;
3732
@@ -52,65 +47,94 @@ protected void onCreate(Bundle savedInstanceState) {
5247 setContentView (R .layout .activity_main );
5348 ButterKnife .bind (this );
5449
55- mListItems .add (new VideoListItem (url , purl1 ));
56- mListItems .add (new VideoListItem (url2 , purl2 ));
57- mListItems .add (new VideoListItem (url3 , purl3 ));
58- mListItems .add (new VideoListItem (url4 , purl4 ));
59- mListItems .add (new VideoListItem (url , purl1 ));
60- mListItems .add (new VideoListItem (url2 , purl2 ));
61- mListItems .add (new VideoListItem (url3 , purl3 ));
62- mListItems .add (new VideoListItem (url4 , purl4 ));
63- mListItems .add (new VideoListItem (url , purl1 ));
64- mListItems .add (new VideoListItem (url2 , purl2 ));
65- mListItems .add (new VideoListItem (url3 , purl3 ));
66- mListItems .add (new VideoListItem (url4 , purl4 ));
67- mListItems .add (new VideoListItem (url , purl1 ));
68- mListItems .add (new VideoListItem (url2 , purl2 ));
69- mListItems .add (new VideoListItem (url3 , purl3 ));
70- mListItems .add (new VideoListItem (url4 , purl4 ));
71- mListItems .add (new VideoListItem (url , purl1 ));
72- mListItems .add (new VideoListItem (url2 , purl2 ));
73- mListItems .add (new VideoListItem (url3 , purl3 ));
74- mListItems .add (new VideoListItem (url4 , purl4 ));
75-
76- LinearLayoutManager layoutManager = new LinearLayoutManager (this );
50+ final VideoListAdapter adapter = new VideoListAdapter ();
51+ final LinearLayoutManager layoutManager = new LinearLayoutManager (this );
52+
53+ mCalculator = new SingleListViewItemActiveCalculator (adapter ,
54+ new RecyclerViewItemPositionGetter (layoutManager , mRecyclerView ));
55+
7756 mRecyclerView .setLayoutManager (layoutManager );
78- mRecyclerView .setAdapter (new VideoListAdapter ());
79- mItemsPositionGetter = new RecyclerViewItemPositionGetter (layoutManager , mRecyclerView );
57+ mRecyclerView .setAdapter (adapter );
8058 mRecyclerView .addOnScrollListener (new RecyclerView .OnScrollListener () {
8159 @ Override
8260 public void onScrollStateChanged (RecyclerView recyclerView , int newState ) {
8361 mScrollState = newState ;
84- if (newState == RecyclerView .SCROLL_STATE_IDLE && ! mListItems . isEmpty () ){
85- mCalculator .onScrollStateIdle (mItemsPositionGetter );
62+ if (newState == RecyclerView .SCROLL_STATE_IDLE && adapter . getItemCount () > 0 ){
63+ mCalculator .onScrollStateIdle ();
8664 }
8765 }
8866
8967 @ Override
9068 public void onScrolled (RecyclerView recyclerView , int dx , int dy ) {
91- mCalculator .onScrolled (mItemsPositionGetter , mScrollState );
69+ mCalculator .onScrolled (mScrollState );
9270 }
9371 });
9472 }
9573
96- private class VideoListAdapter extends RecyclerView .Adapter <VideoViewHolder > {
74+ private class VideoListAdapter extends RecyclerView .Adapter <VideoViewHolder > implements ItemsProvider {
75+
76+ private List <VideoItem > mListItems = new ArrayList <>();
77+
78+ public VideoListAdapter () {
79+ generateMockData ();
80+ }
81+
82+ private void generateMockData () {
83+ mListItems .add (new VideoItem (url , purl1 ));
84+ mListItems .add (new VideoItem (url2 , purl2 ));
85+ mListItems .add (new VideoItem (url3 , purl3 ));
86+ mListItems .add (new VideoItem (url4 , purl4 ));
87+ mListItems .add (new VideoItem (url , purl1 ));
88+ mListItems .add (new VideoItem (url2 , purl2 ));
89+ mListItems .add (new VideoItem (url3 , purl3 ));
90+ mListItems .add (new VideoItem (url4 , purl4 ));
91+ mListItems .add (new VideoItem (url , purl1 ));
92+ mListItems .add (new VideoItem (url2 , purl2 ));
93+ mListItems .add (new VideoItem (url3 , purl3 ));
94+ mListItems .add (new VideoItem (url4 , purl4 ));
95+ mListItems .add (new VideoItem (url , purl1 ));
96+ mListItems .add (new VideoItem (url2 , purl2 ));
97+ mListItems .add (new VideoItem (url3 , purl3 ));
98+ mListItems .add (new VideoItem (url4 , purl4 ));
99+ mListItems .add (new VideoItem (url , purl1 ));
100+ mListItems .add (new VideoItem (url2 , purl2 ));
101+ mListItems .add (new VideoItem (url3 , purl3 ));
102+ mListItems .add (new VideoItem (url4 , purl4 ));
103+ }
97104
98105 @ Override
99106 public VideoViewHolder onCreateViewHolder (ViewGroup parent , int viewType ) {
100107 return new VideoViewHolder (LayoutInflater .from (parent .getContext ())
101108 .inflate (R .layout .video_list_item , parent , false ));
102109 }
103110
111+ public VideoItem getItem (int position ) {
112+ return mListItems .get (position );
113+ }
114+
104115 @ Override
105116 public void onBindViewHolder (VideoViewHolder holder , int position ) {
106- holder .bind (position , mListItems . get (position ));
117+ holder .bind (position , getItem (position ));
107118 }
108119
109120 @ Override
110121 public int getItemCount () {
111122 return mListItems .size ();
112123 }
113124
125+ @ Override
126+ public ListItem getListItem (int position ) {
127+ RecyclerView .ViewHolder holder = mRecyclerView .findViewHolderForAdapterPosition (position );
128+ if (holder instanceof ListItem ) {
129+ return (ListItem ) holder ;
130+ }
131+ return null ;
132+ }
133+
134+ @ Override
135+ public int listItemSize () {
136+ return getItemCount ();
137+ }
114138 }
115139
116140}
0 commit comments