Skip to content

Commit 70fda11

Browse files
ATHBKATHBK
authored andcommitted
Fix bug not swap data
Change to insert data position
1 parent 8109c1a commit 70fda11

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/athbk/changeavtarliketinder/AvatarAdapter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class AvatarAdapter extends TinderRVAdapter<DataModel, AvatarAdapter.Avat
2929
List<DataModel> listData;
3030
Context context;
3131

32+
IFinishDrag iFinishDrag;
33+
3234
public AvatarAdapter(Context context, List<DataModel> list) {
3335
this.listData = list;
3436
this.context = context;
@@ -96,7 +98,14 @@ public void onItemClear() {
9698
super.onItemClear();
9799
layout.setAlpha(1f);
98100
// Log.e("FINISH DRAG", "FINISH DRAG");
101+
if (iFinishDrag != null){
102+
iFinishDrag.updateListData((ArrayList<DataModel>) getListItem());
103+
}
99104

100105
}
101106
}
107+
108+
public void setiFinishDrag(IFinishDrag iFinishDrag) {
109+
this.iFinishDrag = iFinishDrag;
110+
}
102111
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.athbk.changeavtarliketinder;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Created by athbk on 6/14/17.
7+
*/
8+
9+
public interface IFinishDrag {
10+
11+
void updateListData(ArrayList<DataModel> listData);
12+
}

app/src/main/java/com/athbk/changeavtarliketinder/MainActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.util.Log;
56

67
import com.athbk.avatarview.TinderRecyclerView;
78

@@ -77,5 +78,14 @@ protected void onCreate(Bundle savedInstanceState) {
7778

7879
AvatarAdapter adapter = new AvatarAdapter(this, listData);
7980
recyclerView.initRecyclerView(this, adapter);
81+
82+
adapter.setiFinishDrag(new IFinishDrag() {
83+
@Override
84+
public void updateListData(ArrayList<DataModel> listData) {
85+
for (int i=0; i< listData.size(); i++){
86+
Log.e("TAG", "" + i + "/"+ listData.get(i).getLogo());
87+
}
88+
}
89+
});
8090
}
8191
}

avatarview/src/main/java/com/athbk/avatarview/adapter/TinderRVAdapter.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.support.v4.view.MotionEventCompat;
44
import android.support.v7.widget.OrientationHelper;
55
import android.support.v7.widget.RecyclerView;
6+
import android.util.Log;
67
import android.view.MotionEvent;
78
import android.view.View;
89
import android.view.ViewGroup;
@@ -63,13 +64,15 @@ public int getItemCount() {
6364
@Override
6465
public boolean onItemMove(int fromPosition, int toPosition) {
6566
if (getListItem() == null) return false;
66-
Collections.swap(getListItem(), fromPosition, toPosition);
67-
if (toPosition == 0 || toPosition == 1){
68-
notifyDataSetChanged();
69-
}
70-
else {
67+
Log.e("fromPosition: " + fromPosition, "toPosition: " + toPosition);
68+
// Collections.swap(getListItem(), fromPosition, toPosition);
69+
moveList(getListItem(), fromPosition, toPosition);
70+
// if (toPosition == 0 || toPosition == 1){
71+
// notifyDataSetChanged();
72+
// }
73+
// else {
7174
notifyItemMoved(fromPosition, toPosition);
72-
}
75+
// }
7376
return true;
7477
}
7578

@@ -83,4 +86,12 @@ public void onItemDismiss(int position) {
8386
public void setDragStartListener(OnStartDragListener mDragStartListener) {
8487
this.mDragStartListener = mDragStartListener;
8588
}
89+
90+
public void moveList(List<T> list, int fromToPosition, int toPosition){
91+
if (list == null || list.size() < fromToPosition + 1 || list.size() < toPosition + 1) return;
92+
93+
T model = list.get(fromToPosition);
94+
list.remove(fromToPosition);
95+
list.add(toPosition, model);
96+
}
8697
}

0 commit comments

Comments
 (0)