我正在将类似刷卡功能的小火发到卡片堆上。因此,在我的应用程序中,我向上滑动时会移除卡,向下滑动时会添加卡。 我已成功移除卡,但添加卡功能无法正常进行。我需要将先前移除的卡取回,以便向下滑动时将其重新添加到卡堆栈中。 我的活动课是
public class CardActivity extends AppCompatActivity {
int windowwidth;
int screenCenter;
int x_cord, y_cord, x, y,initial_x,initial_y;
int Likes = 0;
public RelativeLayout parentView;
float alphaValue = 0;
private Context context;
ArrayList<UserDataModel> userDataModelArrayList;
private GestureDetector gestureDetector;
TextView txt_empty;
Animation slidedown;
UserDataModel usd;
int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_card);
context = com.bracesmedia.androidmaterialdashboard.CardActivity.this;
parentView = (RelativeLayout) findViewById(R.id.main_layoutview);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
txt_empty=findViewById(R.id.empty_text);
screenCenter = windowwidth / 2;
userDataModelArrayList = new ArrayList<>();
getArrayData();
for (int i = 0; i <userDataModelArrayList.size(); i++) {
final int inx=i;
index=inx;
LayoutInflater inflate = (LayoutInflater) com.bracesmedia.androidmaterialdashboard.CardActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View containerView = inflate.inflate(R.layout.custom_user_layout, null);
final ImageView userIMG = containerView.findViewById(R.id.userIMG);
final RelativeLayout relativeLayoutContainer = containerView.findViewById(R.id.relative_container);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
containerView.setLayoutParams(layoutParams);
containerView.setTag(index);
userIMG.setBackgroundResource(userDataModelArrayList.get(index).getPhoto());
gestureDetector = new GestureDetector(this, new SingleTapConfirm());
final TextView tvName = containerView.findViewById(R.id.tvName);
final TextView tvTotLikes = containerView.findViewById(R.id.tvTotalLikes);
tvName.setText(userDataModelArrayList.get(index).getFacility());
tvTotLikes.setText(userDataModelArrayList.get(index).getTitle());
relativeLayoutContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
// single tap
Log.d("Click","single tap");
Toast.makeText(context, "Click", Toast.LENGTH_LONG).show();
return false;
} else {
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
Log.v("On touch move down", x + " " + y);
break;
case MotionEvent.ACTION_MOVE:
x_cord = (int) event.getRawX();
// smoother animation.
y_cord = (int) event.getRawY();
if(y_cord<y) {
containerView.setX(x_cord - x);
containerView.setY(y_cord - y);
}
else{
//animate previous data card
}
Log.d("action event","Move");
break;
case MotionEvent.ACTION_UP:
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
Log.e("action up :X Point", "" + x_cord + " , Y " + y_cord);
if(y_cord<y){
Toast.makeText(context,"y_cord<y-UP"+index+"-"+inx,Toast.LENGTH_LONG).show();
index=inx;
Log.d("index=inx",index+"");
parentView.removeView(containerView);
}
else{
Toast.makeText(context,"y_cord>y-DOWN",Toast.LENGTH_LONG).show();
containerView.setX(initial_x);
containerView.setY(initial_y);
Log.d("TAG",""+containerView.getTag());
}
// parentView.removeView(containerView);
break;
default:
break;
}
return true;
}
}
});
parentView.addView(containerView);
}
txt_empty.setVisibility(View.VISIBLE);
}
private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent event) {
return true;
}
}
private void getArrayData() {
UserDataModel model = new UserDataModel();
model.setFacility("Facility1 ");
model.setTitle("3 M");
model.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model);
UserDataModel model2 = new UserDataModel();
model2.setFacility("Facility 2 ");
model2.setTitle("2 M");
model2.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model2);
UserDataModel model3 = new UserDataModel();
model3.setFacility("Facility 3 ");
model3.setTitle("3 M");
model3.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model3);
UserDataModel model4 = new UserDataModel();
model4.setFacility("Facility 4 ");
model4.setTitle("4 M");
model4.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model4);
UserDataModel model5 = new UserDataModel();
model5.setFacility("Facility 5 ");
model5.setTitle("5 M");
model5.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model5);
UserDataModel model6 = new UserDataModel();
model6.setFacility("Facility 6 ");
model6.setTitle("6 M");
model6.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model6);
UserDataModel model7 = new UserDataModel();
model7.setFacility("Facility 7 ");
model7.setTitle("7 M");
model7.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model7);
UserDataModel model8 = new UserDataModel();
model8.setFacility("Facility 8 ");
model8.setTitle("8 M");
model8.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model8);
UserDataModel model9 = new UserDataModel();
model9.setFacility("Facility 9 ");
model9.setTitle("9 M");
model9.setPhoto(R.drawable.logo);
userDataModelArrayList.add(model9);
/*UserDataModel model10 = new UserDataModel();
model10.setFacility("");
model10.setTitle("");
//model9.setPhoto(null);
userDataModelArrayList.add(model10);*/
Collections.reverse(userDataModelArrayList);
}
}
enter code here