Skip to content
This repository was archived by the owner on Aug 22, 2020. It is now read-only.

Commit 23c6774

Browse files
committed
VerticalStepperView: Set/Get error text
Signed-off-by: Fung <[email protected]>
1 parent 31ed591 commit 23c6774

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

demo/src/main/java/moe/feng/common/stepperview/demo/fragment/VerticalStepperAdapterDemoFragment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ public View onCreateCustomView(final int index, Context context, VerticalStepper
6060
contentView.setText(
6161
index == 0 ? R.string.content_step_0 : (index == 1 ? R.string.content_step_1 : R.string.content_step_2)
6262
);
63-
inflateView.findViewById(R.id.button_next).setOnClickListener(new View.OnClickListener() {
63+
Button nextButton = inflateView.findViewById(R.id.button_next);
64+
nextButton.setText(index == size() - 1 ? "Set error text" : getString(android.R.string.ok));
65+
nextButton.setOnClickListener(new View.OnClickListener() {
6466
@Override
6567
public void onClick(View view) {
6668
if (!mVerticalStepperView.nextStep()) {
67-
Snackbar.make(mVerticalStepperView, "Finish", Snackbar.LENGTH_LONG).show();
69+
mVerticalStepperView.setErrorText(0, mVerticalStepperView.getErrorText(0) == null ? "Test error" : null);
70+
Snackbar.make(mVerticalStepperView, "Set!", Snackbar.LENGTH_LONG).show();
6871
}
6972
}
7073
});

library/src/main/java/moe/feng/common/stepperview/VerticalStepperView.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.drawable.Drawable;
66
import android.support.annotation.ColorInt;
77
import android.support.annotation.ColorRes;
8+
import android.support.annotation.Nullable;
89
import android.support.v7.widget.LinearLayoutManager;
910
import android.support.v7.widget.RecyclerView;
1011
import android.util.AttributeSet;
@@ -24,8 +25,9 @@ public class VerticalStepperView extends FrameLayout implements IStepperView {
2425
/**
2526
* View State
2627
*/
27-
private IStepperAdapter mViewAdapter;
28+
private IStepperAdapter mStepperAdapter;
2829
private int mCurrentStep = 0;
30+
private String[] mErrorTexts = null;
2931

3032
/**
3133
* View attributes
@@ -92,17 +94,54 @@ private void prepareListView(Context context) {
9294
* @param stepperAdapter Stepper Adapter
9395
*/
9496
public void setStepperAdapter(IStepperAdapter stepperAdapter) {
95-
mViewAdapter = stepperAdapter;
97+
mStepperAdapter = stepperAdapter;
98+
updateSteppers();
99+
}
100+
101+
/**
102+
* Notify the stepper adapter changed
103+
*/
104+
public void updateSteppers() {
105+
if ((mErrorTexts != null && mErrorTexts.length != mStepperAdapter.size()) || mErrorTexts == null) {
106+
mErrorTexts = new String[mStepperAdapter.size()];
107+
}
96108
mAdapter.notifyDataSetChanged();
97109
}
98110

111+
/**
112+
* Set error text for item. If you want to remove error text, the errorText param should be null.
113+
*
114+
* @param index Index
115+
* @param errorText Error text or null
116+
*/
117+
public void setErrorText(int index, @Nullable String errorText) {
118+
if (mErrorTexts == null) {
119+
mErrorTexts = new String[mStepperAdapter.size()];
120+
}
121+
mErrorTexts[index] = errorText;
122+
updateSteppers();
123+
}
124+
125+
/**
126+
* Get error text of item
127+
*
128+
* @param index Index
129+
* @return Error text or null (means no error)
130+
*/
131+
public @Nullable String getErrorText(int index) {
132+
if (mErrorTexts != null) {
133+
return mErrorTexts[index];
134+
}
135+
return null;
136+
}
137+
99138
/**
100139
* Return the count of steps
101140
*
102141
* @return The count of steps
103142
*/
104143
public int getStepCount() {
105-
return mViewAdapter != null ? mViewAdapter.size() : 0;
144+
return mStepperAdapter != null ? mStepperAdapter.size() : 0;
106145
}
107146

108147
/**
@@ -111,7 +150,7 @@ public int getStepCount() {
111150
* @return If stepper can go next
112151
*/
113152
public boolean canNext() {
114-
return mViewAdapter != null && mCurrentStep < mViewAdapter.size() - 1;
153+
return mStepperAdapter != null && mCurrentStep < mStepperAdapter.size() - 1;
115154
}
116155

117156
/**
@@ -120,7 +159,7 @@ public boolean canNext() {
120159
* @return If stepper can go previous
121160
*/
122161
public boolean canPrev() {
123-
return mViewAdapter != null && mCurrentStep > 0;
162+
return mStepperAdapter != null && mCurrentStep > 0;
124163
}
125164

126165
/**
@@ -130,9 +169,9 @@ public boolean canPrev() {
130169
*/
131170
public boolean nextStep() {
132171
if (canNext()) {
133-
mViewAdapter.onHide(mCurrentStep);
172+
mStepperAdapter.onHide(mCurrentStep);
134173
mCurrentStep++;
135-
mViewAdapter.onShow(mCurrentStep);
174+
mStepperAdapter.onShow(mCurrentStep);
136175
if (mAnimationEnabled) {
137176
mAdapter.notifyItemRangeChanged(mCurrentStep - 1, 2);
138177
} else {
@@ -150,9 +189,9 @@ public boolean nextStep() {
150189
*/
151190
public boolean prevStep() {
152191
if (canPrev()) {
153-
mViewAdapter.onHide(mCurrentStep);
192+
mStepperAdapter.onHide(mCurrentStep);
154193
mCurrentStep--;
155-
mViewAdapter.onShow(mCurrentStep);
194+
mStepperAdapter.onShow(mCurrentStep);
156195
if (mAnimationEnabled) {
157196
mAdapter.notifyItemRangeChanged(mCurrentStep, 2);
158197
} else {
@@ -170,7 +209,7 @@ public boolean prevStep() {
170209
*/
171210
@Override
172211
public IStepperAdapter getStepperAdapter() {
173-
return mViewAdapter;
212+
return mStepperAdapter;
174213
}
175214

176215
/**
@@ -317,6 +356,7 @@ public void onBindViewHolder(ItemHolder holder, int position) {
317356
holder.mItemView.setAnimationDuration(mAnimationDuration);
318357
holder.mItemView.setDoneIcon(mDoneIcon);
319358
holder.mItemView.setAnimationEnabled(mAnimationEnabled);
359+
holder.mItemView.setErrorText(mErrorTexts[position]);
320360
if (getCurrentStep() > position) {
321361
holder.mItemView.setState(VerticalStepperItemView.STATE_DONE);
322362
} else if (getCurrentStep() < position) {

0 commit comments

Comments
 (0)