3
3
import android .content .Intent ;
4
4
import android .net .Uri ;
5
5
import android .os .Bundle ;
6
- import android .support .annotation .Nullable ;
7
- import android .support .design .widget .FloatingActionButton ;
8
- import android .support .design .widget .Snackbar ;
9
- import android .support .design .widget .TabLayout ;
10
- import android .support .v4 .app .Fragment ;
11
- import android .support .v4 .app .FragmentManager ;
12
- import android .support .v4 .app .FragmentStatePagerAdapter ;
13
- import android .support .v4 .view .ViewPager ;
14
- import android .support .v7 .app .AppCompatActivity ;
15
- import android .support .v7 .widget .LinearLayoutManager ;
16
- import android .support .v7 .widget .RecyclerView ;
17
- import android .support .v7 .widget .Toolbar ;
18
6
import android .view .LayoutInflater ;
19
7
import android .view .Menu ;
20
8
import android .view .MenuItem ;
21
9
import android .view .View ;
22
10
import android .view .ViewGroup ;
23
- import android .widget .Toast ;
24
11
12
+ import com .google .android .material .tabs .TabLayout ;
25
13
import com .lb .lollipopcontactsrecyclerviewfastscroller .R ;
26
14
15
+ import androidx .annotation .Nullable ;
16
+ import androidx .appcompat .app .AppCompatActivity ;
17
+ import androidx .appcompat .widget .Toolbar ;
18
+ import androidx .fragment .app .Fragment ;
19
+ import androidx .fragment .app .FragmentManager ;
20
+ import androidx .fragment .app .FragmentStatePagerAdapter ;
21
+ import androidx .recyclerview .widget .LinearLayoutManager ;
22
+ import androidx .recyclerview .widget .RecyclerView ;
23
+ import androidx .recyclerview .widget .RecyclerView .State ;
24
+ import androidx .viewpager .widget .ViewPager ;
25
+
27
26
public class MainActivity extends AppCompatActivity {
28
27
@ Override
29
28
protected void onCreate (Bundle savedInstanceState ) {
30
29
super .onCreate (savedInstanceState );
31
30
setContentView (R .layout .activity_main );
32
- Toolbar toolbar = ( Toolbar ) findViewById (R .id .toolbar );
31
+ Toolbar toolbar = findViewById (R .id .toolbar );
33
32
setSupportActionBar (toolbar );
34
33
//note : part of the design library sample code was taken from : https://github.com/sitepoint-editors/Design-Demo/
35
- FloatingActionButton fab = (FloatingActionButton ) findViewById (R .id .fab );
36
- fab .setOnClickListener (new View .OnClickListener () {
37
- @ Override
38
- public void onClick (View v ) {
39
- Snackbar .make (findViewById (R .id .coordinator ), "I'm a Snackbar" , Snackbar .LENGTH_LONG ).setAction ("Action" , new View .OnClickListener () {
40
- @ Override
41
- public void onClick (View v ) {
42
- Toast .makeText (MainActivity .this , "Snackbar Action" , Toast .LENGTH_LONG ).show ();
43
- }
44
- }).show ();
45
- }
46
- });
47
34
48
35
DesignDemoPagerAdapter adapter = new DesignDemoPagerAdapter (getSupportFragmentManager ());
49
- ViewPager viewPager = ( ViewPager ) findViewById (R .id .viewpager );
36
+ ViewPager viewPager = findViewById (R .id .viewpager );
50
37
viewPager .setAdapter (adapter );
51
- TabLayout tabLayout = ( TabLayout ) findViewById (R .id .tablayout );
38
+ TabLayout tabLayout = findViewById (R .id .tablayout );
52
39
tabLayout .setupWithViewPager (viewPager );
53
40
}
54
41
@@ -58,7 +45,6 @@ public boolean onCreateOptionsMenu(final Menu menu) {
58
45
return super .onCreateOptionsMenu (menu );
59
46
}
60
47
61
- @ SuppressWarnings ("deprecation" )
62
48
@ Override
63
49
public boolean onOptionsItemSelected (final MenuItem item ) {
64
50
String url = null ;
@@ -92,7 +78,9 @@ public DesignDemoPagerAdapter(FragmentManager fm) {
92
78
@ Override
93
79
public Fragment getItem (int position ) {
94
80
final RecyclerViewFragment recyclerViewFragment = new RecyclerViewFragment ();
95
- recyclerViewFragment .numberOfItems = getFragmentItemsCount (position );
81
+ Bundle args = new Bundle ();
82
+ args .putInt (RecyclerViewFragment .ITEMS_COUNT , getFragmentItemsCount (position ));
83
+ recyclerViewFragment .setArguments (args );
96
84
return recyclerViewFragment ;
97
85
}
98
86
@@ -113,29 +101,24 @@ public CharSequence getPageTitle(int position) {
113
101
114
102
115
103
public static class RecyclerViewFragment extends Fragment {
104
+ public static final String ITEMS_COUNT = "ITEMS_COUNT" ;
116
105
public int numberOfItems ;
117
106
118
107
@ Nullable
119
108
@ Override
120
109
public View onCreateView (final LayoutInflater inflater , final ViewGroup container , final Bundle savedInstanceState ) {
110
+ numberOfItems = getArguments ().getInt (ITEMS_COUNT );
121
111
View rootView = inflater .inflate (R .layout .fragment_recycler_view , container , false );
122
- RecyclerView recyclerView = ( RecyclerView ) rootView .findViewById (R .id .recyclerview );
112
+ RecyclerView recyclerView = rootView .findViewById (R .id .recyclerview );
123
113
final LargeAdapter adapter = new LargeAdapter (numberOfItems );
124
114
recyclerView .setAdapter (adapter );
125
- final RecyclerViewFastScroller fastScroller = ( RecyclerViewFastScroller ) rootView .findViewById (R .id .fastscroller );
115
+ final RecyclerViewFastScroller fastScroller = rootView .findViewById (R .id .fastscroller );
126
116
recyclerView .setLayoutManager (new LinearLayoutManager (getActivity (), LinearLayoutManager .VERTICAL , false ) {
127
117
@ Override
128
- public void onLayoutChildren (final RecyclerView .Recycler recycler , final RecyclerView .State state ) {
129
- super .onLayoutChildren (recycler , state );
130
- //TODO if the items are filtered, considered hiding the fast scroller here
118
+ public void onLayoutCompleted (final State state ) {
119
+ super .onLayoutCompleted (state );
131
120
final int firstVisibleItemPosition = findFirstVisibleItemPosition ();
132
- if (firstVisibleItemPosition != 0 ) {
133
- // this avoids trying to handle un-needed calls
134
- if (firstVisibleItemPosition == -1 )
135
- //not initialized, or no items shown, so hide fast-scroller
136
- fastScroller .setVisibility (View .GONE );
137
- return ;
138
- }
121
+ //Log.d("AppLog", "onLayoutCompleted firstVisibleItemPosition:"+firstVisibleItemPosition);
139
122
final int lastVisibleItemPosition = findLastVisibleItemPosition ();
140
123
int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1 ;
141
124
//if all items are shown, hide the fast-scroller
@@ -144,6 +127,13 @@ public void onLayoutChildren(final RecyclerView.Recycler recycler, final Recycle
144
127
});
145
128
fastScroller .setRecyclerView (recyclerView );
146
129
fastScroller .setViewsToUse (R .layout .recycler_view_fast_scroller__fast_scroller , R .id .fastscroller_bubble , R .id .fastscroller_handle );
130
+ //new Handler().postDelayed(new Runnable() {
131
+ // @Override
132
+ // public void run() {
133
+ // adapter.setItemsCount(new Random().nextInt(20));
134
+ // adapter.notifyDataSetChanged();
135
+ // }
136
+ //}, 2000L);
147
137
return rootView ;
148
138
}
149
139
}
0 commit comments