-
Notifications
You must be signed in to change notification settings - Fork 41
/
ViewsSliderActivity.java
177 lines (148 loc) · 5.69 KB
/
ViewsSliderActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package info.androidhive.viewpager2.views;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import info.androidhive.viewpager2.R;
import info.androidhive.viewpager2.Utils;
import info.androidhive.viewpager2.databinding.ActivityViewsSliderBinding;
public class ViewsSliderActivity extends AppCompatActivity {
private ViewsSliderAdapter mAdapter;
private TextView[] dots;
private int[] layouts;
private ActivityViewsSliderBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityViewsSliderBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
init();
}
private void init() {
// layouts of all welcome sliders
// add few more layouts if you want
layouts = new int[]{
R.layout.slide_one,
R.layout.slide_two,
R.layout.slide_three,
R.layout.slide_four};
mAdapter = new ViewsSliderAdapter();
binding.viewPager.setAdapter(mAdapter);
binding.viewPager.registerOnPageChangeCallback(pageChangeCallback);
binding.btnSkip.setOnClickListener(v -> launchHomeScreen());
binding.btnNext.setOnClickListener(v -> {
// checking for last page
// if last page home screen will be launched
int current = getItem(+1);
if (current < layouts.length) {
// move to next screen
binding.viewPager.setCurrentItem(current);
} else {
launchHomeScreen();
}
});
binding.iconMore.setOnClickListener(view -> {
showMenu(view);
});
// adding bottom dots
addBottomDots(0);
}
/*
* Adds bottom dots indicator
* */
private void addBottomDots(int currentPage) {
dots = new TextView[layouts.length];
int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);
binding.layoutDots.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorsInactive[currentPage]);
binding.layoutDots.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive[currentPage]);
}
private int getItem(int i) {
return binding.viewPager.getCurrentItem() + i;
}
private void launchHomeScreen() {
Toast.makeText(this, R.string.slides_ended, Toast.LENGTH_LONG).show();
finish();
}
private void showMenu(View view) {
PopupMenu popup = new PopupMenu(this, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.pager_transformers, popup.getMenu());
popup.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.action_orientation) {
binding.viewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
} else {
binding.viewPager.setPageTransformer(Utils.getTransformer(item.getItemId()));
binding.viewPager.setCurrentItem(0);
binding.viewPager.getAdapter().notifyDataSetChanged();
}
return false;
});
popup.show();
}
/*
* ViewPager page change listener
*/
ViewPager2.OnPageChangeCallback pageChangeCallback = new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
addBottomDots(position);
// changing the next button text 'NEXT' / 'GOT IT'
if (position == layouts.length - 1) {
// last page. make button text to GOT IT
binding.btnNext.setText(getString(R.string.start));
binding.btnSkip.setVisibility(View.GONE);
} else {
// still pages are left
binding.btnNext.setText(getString(R.string.next));
binding.btnSkip.setVisibility(View.VISIBLE);
}
}
};
public class ViewsSliderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public ViewsSliderAdapter() {
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(viewType, parent, false);
return new SliderViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
}
@Override
public int getItemViewType(int position) {
return layouts[position];
}
@Override
public int getItemCount() {
return layouts.length;
}
public class SliderViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public SliderViewHolder(View view) {
super(view);
}
}
}
}