@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); ButterKnife.inject(this); al = new ArrayList<>(); al.add("php"); al.add("c"); al.add("python"); al.add("java"); al.add("html"); al.add("c++"); al.add("css"); al.add("javascript"); arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al); flingContainer.setAdapter(arrayAdapter); flingContainer.setFlingListener( new SwipeFlingAdapterView.onFlingListener() { @Override public void removeFirstObjectInAdapter() { // this is the simplest way to delete an object from the Adapter (/AdapterView) Log.d("LIST", "removed object!"); al.remove(0); arrayAdapter.notifyDataSetChanged(); } @Override public void onScore1CardExit(Object dataObject) { // Do something on the left! // You also have access to the original object. // If you want to use it just cast it (String) dataObject makeToast(MyActivity.this, "Score1!"); } @Override public void onScore2CardExit(Object dataObject) { // Do something on the left! // You also have access to the original object. // If you want to use it just cast it (String) dataObject makeToast(MyActivity.this, "Score2!"); } @Override public void onScore3CardExit(Object dataObject) { // Do something on the left! // You also have access to the original object. // If you want to use it just cast it (String) dataObject makeToast(MyActivity.this, "Score3!"); } @Override public void onScore4CardExit(Object dataObject) { makeToast(MyActivity.this, "Score4!"); } @Override public void onNoScoreCardExit(Object dataObject) { // Do something on the left! // You also have access to the original object. // If you want to use it just cast it (String) dataObject makeToast(MyActivity.this, "No score!"); } @Override public void onAdapterAboutToEmpty(int itemsInAdapter) { // Ask for more data here al.add("XML ".concat(String.valueOf(i))); arrayAdapter.notifyDataSetChanged(); Log.d("LIST", "notified"); i++; } @Override public void onScroll(float scrollProgressPercent) { View view = flingContainer.getSelectedView(); view.findViewById(R.id.item_score_1_indicator).setAlpha(0); view.findViewById(R.id.item_score_2_indicator).setAlpha(0); view.findViewById(R.id.item_score_3_indicator).setAlpha(0); view.findViewById(R.id.item_score_4_indicator).setAlpha(0); if (scrollProgressPercent == 1) { view.findViewById(R.id.item_score_1_indicator).setAlpha(1); } else if (scrollProgressPercent == 2) { view.findViewById(R.id.item_score_2_indicator).setAlpha(1); } else if (scrollProgressPercent == 3) { view.findViewById(R.id.item_score_3_indicator).setAlpha(1); } else if (scrollProgressPercent == 4) { view.findViewById(R.id.item_score_4_indicator).setAlpha(1); } } }); // Optionally add an OnItemClickListener // flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() // { // @Override // public void onItemClicked(int itemPosition, Object dataObject) { // makeToast(MyActivity.this, "Clicked!"); // } // }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame); al = new ArrayList<>(); Parse.initialize( this, "SbBqX0xwl4xJDydLGDkEXcf8M1LhpfRkO6wIkvfg", "KCnKe8imy5QhETsD6v3cVQ44KOjueMzWRa0CAL0m"); ParseQuery<ParseObject> query = ParseQuery.getQuery("food"); query.findInBackground( new FindCallback<ParseObject>() { public void done(List<ParseObject> objects, ParseException e) { if (e == null) { for (ParseObject product : objects) { al.add( new Food( (String) product.get("imagePath"), (String) product.get("description"))); } } else { Log.d("ERROR", "Failed to retrieve Parse objects"); } } }); myAppAdapter = new MyAppAdapter(al, MainActivity.this); flingContainer.setAdapter(myAppAdapter); flingContainer.setFlingListener( new SwipeFlingAdapterView.onFlingListener() { @Override public void removeFirstObjectInAdapter() {} @Override public void onLeftCardExit(Object dataObject) { Log.d("onLeftCardExit", dataObject.toString()); al.remove(0); myAppAdapter.notifyDataSetChanged(); // Do something on the left! // You also have access to the original object. // If you want to use it just cast it (String) dataObject } @Override public void onRightCardExit(Object dataObject) { Food f = al.get(0); Log.d("ARRAY", f.getDescription()); al.remove(0); myAppAdapter.notifyDataSetChanged(); Log.d("onRightCardExit", dataObject.toString()); } @Override public void onAdapterAboutToEmpty(int itemsInAdapter) {} @Override public void onScroll(float scrollProgressPercent) { View view = flingContainer.getSelectedView(); view.findViewById(R.id.background).setAlpha(0); view.findViewById(R.id.item_swipe_right_indicator) .setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0); view.findViewById(R.id.item_swipe_left_indicator) .setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0); } }); // Optionally add an OnItemClickListener flingContainer.setOnItemClickListener( new SwipeFlingAdapterView.OnItemClickListener() { @Override public void onItemClicked(int itemPosition, Object dataObject) { View view = flingContainer.getSelectedView(); view.findViewById(R.id.background).setAlpha(0); myAppAdapter.notifyDataSetChanged(); } }); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); UserDataSource.getUnseenUsers(this); final SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) v.findViewById(R.id.swipe_view); List<Shirt> shirts = new ArrayList<>(); mCardAdapter = new CardAdapter(getActivity(), shirts); flingContainer.setAdapter(mCardAdapter); flingContainer.setFlingListener( new SwipeFlingAdapterView.onFlingListener() { @Override public void removeFirstObjectInAdapter() { // This is the simplest way to delete an object from the adapter mCardAdapter.removeFrontItem(); } @Override public void onLeftCardExit(Object o) { // Do something on the left; you still have access to the original object flingContainer.requestLayout(); // ActionDataSource.saveUserLiked(user.getId()); } @Override public void onRightCardExit(Object o) { flingContainer.requestLayout(); // ActionDataSource.saveUserSkipped(user.getId()); if (o instanceof Shirt) { final User currentUser = UserDataSource.getCurrentUser(); final Shirt likedShirt = (Shirt) o; currentUser.addLikedShirt(likedShirt.id); // check for match // if other user has liked one of my shirts then match List<String> otherUserLikedShirts = likedShirt.user.getLikedShirts(); List<String> myShirts = currentUser.getShirts(); for (String shirtID : myShirts) { final String shirtIDCopy = shirtID; if (otherUserLikedShirts.contains(shirtID)) { // there's a match bitches!!! // get Shirt URL from parse ParseQuery<ParseObject> query = ParseQuery.getQuery("Shirt"); query.getInBackground( shirtID, new GetCallback<ParseObject>() { public void done(ParseObject object, ParseException e) { if (e == null) { ParseFile postImage = object.getParseFile("image"); final ParseObject yourMatch = new ParseObject("Match"); yourMatch.put("yourShirtID", shirtIDCopy); yourMatch.put("yourShirtURL", Uri.parse(postImage.getUrl())); yourMatch.put("theirShirtID", likedShirt.id); yourMatch.put("theirShirtURL", likedShirt.url); yourMatch.put("otherUserID", likedShirt.user.getId()); yourMatch.saveInBackground( new SaveCallback() { @Override public void done(ParseException parseException) { currentUser.addMatch(yourMatch.getObjectId()); } }); final ParseObject theirMatch = new ParseObject("Match"); theirMatch.put("yourShirtID", likedShirt.id); theirMatch.put("yourShirtURL", likedShirt.url); theirMatch.put("theirShirtID", shirtIDCopy); theirMatch.put("theirShirtURL", Uri.parse(postImage.getUrl())); theirMatch.put("otherUserID", currentUser.getId()); theirMatch.saveInBackground( new SaveCallback() { @Override public void done(ParseException parseException) { likedShirt.user.addMatch(theirMatch.getObjectId()); } }); } else { // something went wrong } } }); } } } else { // Something went wrong... } } @Override public void onAdapterAboutToEmpty(int i) { // TODO: Don't fetch all at once; use this } @Override public void onScroll(float scrollProgressPercent) {} }); ImageButton yesButton = (ImageButton) v.findViewById(R.id.yes_button); yesButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { flingContainer.getTopCardListener().selectRight(); } }); ImageButton noButton = (ImageButton) v.findViewById(R.id.no_button); noButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { flingContainer.getTopCardListener().selectLeft(); } }); return v; }