再次刷卡和上一次刷卡后,如何使用上一个按钮刷回卡后管理订单?
How can I manage order after swiped card back using previous button after swipe again and previous again?
问题:如果用户单击上一个按钮,如 Google primer app,如何将所有以前刷过的卡片重新堆叠起来?
解释:
假设我有 10 张卡片。 001、002...010。现在我开始刷了。我刷了 001、002 和 003,所以我现在看到的是 004。现在我点击了上一个按钮。所以它显示的是 003,这是正确的。
之后可能会发生两个用例:
1:假设我点击了上一个按钮,那么它应该显示002,但没有显示。
2: 假设我点了下一个按钮,所以它应该再次显示004,它也显示并且是正确的,但是现在我再次点击上一个按钮,现在没有显示003 !
Google 初级应用示例图片:enter image description here
我在我的项目中尝试了这个库 类 因为我不想在我的项目中添加依赖项:https://github.com/flschweiger/SwipeStack
滑动堆栈适配器:
public class SwipeStackAdapter extends BaseAdapter {
public static final String TAG = SwipeStackAdapter.class.getSimpleName();
private List<String> mData;
public SwipeStackAdapter(List<String> data) {
this.mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
TextView textViewCard = convertView.findViewById(R.id.textViewCard);
textViewCard.setText(mData.get(position));
return convertView;
}
private void showLog(String msg) {
Log.d(TAG, msg);
}
}
我在SwipeStack.java中找到了这个方法:
private void addNextView() {
if (mCurrentViewIndex < mAdapter.getCount()) {
View bottomView = mAdapter.getView(mCurrentViewIndex, null, this);
bottomView.setTag(R.id.new_view, true);
if (!mDisableHwAcceleration) {
bottomView.setLayerType(LAYER_TYPE_HARDWARE, null);
}
if (mViewRotation > 0) {
bottomView.setRotation(mRandom.nextInt(mViewRotation) - (mViewRotation / 2));
}
int width = getWidth() - (getPaddingLeft() + getPaddingRight());
int height = getHeight() - (getPaddingTop() + getPaddingBottom());
LayoutParams params = bottomView.getLayoutParams();
if (params == null) {
params = new LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
}
int measureSpecWidth = MeasureSpec.AT_MOST;
int measureSpecHeight = MeasureSpec.AT_MOST;
if (params.width == LayoutParams.MATCH_PARENT) {
measureSpecWidth = MeasureSpec.EXACTLY;
}
if (params.height == LayoutParams.MATCH_PARENT) {
measureSpecHeight = MeasureSpec.EXACTLY;
}
bottomView.measure(measureSpecWidth | width, measureSpecHeight | height);
addViewInLayout(bottomView, 0, params, true);
mCurrentViewIndex++;
}
}
编辑:感谢 Navneet Shamra 的特别帮助。非常感谢。我非常感谢你的经验和才华。
//添加一个新的变量来保存最后一次滑动的位置
int lastSwipedPosition=0;
// 主列表和临时列表
private ArrayList<String> mData = new Arraylist();
private ArrayList<String> mTempData = new Arraylist();
//现在填写临时列表中的所有数据并在您的适配器中使用临时文件`
`mTempData.addAll(mData);
mAdapter = new SwipeStackAdapter(mTempData);
mSwipeStack.setAdapter(mAdapter);
// 回调方法
@Override
public void onViewSwipedToRight(int position) {
lastSwipedPosition=position+1;
}
@Override
public void onViewSwipedToLeft(int position) {
lastSwipedPosition=position+1;
}
/单击上一个按钮并返回上一个,我们将从 lastSwipedPosition 中减去 1 以显示最后刷卡。
lastSwipedPosition = lastSwipedPosition - 1;
if (lastSwipedPosition >= 0) {
mTempData.clear();//clear all data
for (int i = lastSwipedPosition; i < mData.size(); i++) {
mTempData.add(mData.get(i));
}
mSwipeStack.resetStack();
if (lastSwipedPosition == 0){
Toast.makeText(this, "hide back button", Toast.LENGTH_SHORT).show();
}
} else {
lastSwipedPosition = 0;
}
问题:如果用户单击上一个按钮,如 Google primer app,如何将所有以前刷过的卡片重新堆叠起来?
解释:
假设我有 10 张卡片。 001、002...010。现在我开始刷了。我刷了 001、002 和 003,所以我现在看到的是 004。现在我点击了上一个按钮。所以它显示的是 003,这是正确的。
之后可能会发生两个用例:
1:假设我点击了上一个按钮,那么它应该显示002,但没有显示。
2: 假设我点了下一个按钮,所以它应该再次显示004,它也显示并且是正确的,但是现在我再次点击上一个按钮,现在没有显示003 !
Google 初级应用示例图片:enter image description here
我在我的项目中尝试了这个库 类 因为我不想在我的项目中添加依赖项:https://github.com/flschweiger/SwipeStack
滑动堆栈适配器:
public class SwipeStackAdapter extends BaseAdapter {
public static final String TAG = SwipeStackAdapter.class.getSimpleName();
private List<String> mData;
public SwipeStackAdapter(List<String> data) {
this.mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
TextView textViewCard = convertView.findViewById(R.id.textViewCard);
textViewCard.setText(mData.get(position));
return convertView;
}
private void showLog(String msg) {
Log.d(TAG, msg);
}
}
我在SwipeStack.java中找到了这个方法:
private void addNextView() {
if (mCurrentViewIndex < mAdapter.getCount()) {
View bottomView = mAdapter.getView(mCurrentViewIndex, null, this);
bottomView.setTag(R.id.new_view, true);
if (!mDisableHwAcceleration) {
bottomView.setLayerType(LAYER_TYPE_HARDWARE, null);
}
if (mViewRotation > 0) {
bottomView.setRotation(mRandom.nextInt(mViewRotation) - (mViewRotation / 2));
}
int width = getWidth() - (getPaddingLeft() + getPaddingRight());
int height = getHeight() - (getPaddingTop() + getPaddingBottom());
LayoutParams params = bottomView.getLayoutParams();
if (params == null) {
params = new LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
}
int measureSpecWidth = MeasureSpec.AT_MOST;
int measureSpecHeight = MeasureSpec.AT_MOST;
if (params.width == LayoutParams.MATCH_PARENT) {
measureSpecWidth = MeasureSpec.EXACTLY;
}
if (params.height == LayoutParams.MATCH_PARENT) {
measureSpecHeight = MeasureSpec.EXACTLY;
}
bottomView.measure(measureSpecWidth | width, measureSpecHeight | height);
addViewInLayout(bottomView, 0, params, true);
mCurrentViewIndex++;
}
}
编辑:感谢 Navneet Shamra 的特别帮助。非常感谢。我非常感谢你的经验和才华。
//添加一个新的变量来保存最后一次滑动的位置
int lastSwipedPosition=0;
// 主列表和临时列表
private ArrayList<String> mData = new Arraylist();
private ArrayList<String> mTempData = new Arraylist();
//现在填写临时列表中的所有数据并在您的适配器中使用临时文件`
`mTempData.addAll(mData);
mAdapter = new SwipeStackAdapter(mTempData);
mSwipeStack.setAdapter(mAdapter);
// 回调方法
@Override
public void onViewSwipedToRight(int position) {
lastSwipedPosition=position+1;
}
@Override
public void onViewSwipedToLeft(int position) {
lastSwipedPosition=position+1;
}
/单击上一个按钮并返回上一个,我们将从 lastSwipedPosition 中减去 1 以显示最后刷卡。
lastSwipedPosition = lastSwipedPosition - 1;
if (lastSwipedPosition >= 0) {
mTempData.clear();//clear all data
for (int i = lastSwipedPosition; i < mData.size(); i++) {
mTempData.add(mData.get(i));
}
mSwipeStack.resetStack();
if (lastSwipedPosition == 0){
Toast.makeText(this, "hide back button", Toast.LENGTH_SHORT).show();
}
} else {
lastSwipedPosition = 0;
}