gpt4 book ai didi

java - 使用 firebase 在 recyclerView 中拖放

转载 作者:行者123 更新时间:2023-11-30 00:21:54 25 4
gpt4 key购买 nike

我正在尝试在使用 FirebaseRecyclerAdapter 的 recyclerView 中实现拖放功能。我在 firebase child 中有一个属性,用于存储其订单位置并重新排序我发现类似的数据:

  @Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
final int firstPosition = viewHolder.getAdapterPosition();;
final int secondPosition = target.getAdapterPosition();
DatabaseReference firstItemRef = fireBaseRvAdapter.getRef(viewHolder.getAdapterPosition());
DatabaseReference secondItemRef = fireBaseRvAdapter.getRef(target.getAdapterPosition());

HashMap<String, Object> updateFirstItemOrderNumber = new HashMap<>();
updateFirstItemOrderNumber.put(FBdatabase.FIELD_ORDER, secondPosition);
firstItemRef.updateChildren(updateFirstItemOrderNumber);

HashMap<String, Object> updateSecondItemOrderNumber = new HashMap<>();
updateSecondItemOrderNumber.put(FBdatabase.FIELD_ORDER, firstPosition);
secondItemRef.updateChildren(updateSecondItemOrderNumber);

return false;
}

但拖动在第一次交换后自行停止,然后列表奇怪地动画化。它改变了顺序,但看起来不太好。

最佳答案

我会添加更多解释,但稍后……再次)

public class CustomFirebaseAdapter extends RecyclerView.Adapter<CustomFirebaseAdapter.RecordViewHolderIns>
implements ValueEventListener, ChildEventListener {

private static final String TAG = "CustomFerebaseAdapter";
private OnItemClickListener onItemClickListener; //custom interface
private int maxOrderValue;
private ArrayList<Record> records = new ArrayList<>();
private ArrayList<DatabaseReference> references = new ArrayList<>();
public boolean isDragging = false;
private Query mainQuery;
private int textSize=9;


public CustomFirebaseAdapter(Class<Record> modelClass, @LayoutRes int modelLayout,
Class<MyFireBaseRvAdapter.RecordViewHolderIns> viewHolderClass, Query query) {
mainQuery = query;
// getting max order value to insert new record with maxOrderValue + 1

mainQuery.limitToLast(1)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot ds : dataSnapshot.getChildren()) {
Record r = ds.getValue(Record.class);
maxOrderValue = r.getPositionInList();
Log.d(TAG, "onDataChange: max value = " + maxOrderValue);
Log.d(TAG, "onDataChange: max value = " + r.getText());

}

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
//listner for first update
mainQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
updateArrays(dataSnapshot);
notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

mainQuery.addValueEventListener(this);
mainQuery.addChildEventListener(this);
}

@Override
public CustomFirebaseAdapter.RecordViewHolderIns onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new CustomFirebaseAdapter.RecordViewHolderIns(inflatedView);
}

@Override
public void onBindViewHolder(RecordViewHolderIns holder, int position) {
populateViewHolder(holder, records.get(position), position);
}

public ArrayList<Record> getRecords() {
return records;
}

public ArrayList<DatabaseReference> getReferences() {
return references;
}

protected void populateViewHolder(CustomFirebaseAdapter.viewHolder, Record model, int position) {
//.................................
}

public void setTextSize(int textSize){
this.textSize =textSize;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

public int getMaxOrderValue() {
return maxOrderValue;
}

public Record getItem(int position) {
return records.get(position);
}

public DatabaseReference getRef(int position) {
return references.get(position);
}

ArrayList<Record> snapshotList;

public ArrayList<Record> getDataSnapshot() {
return snapshotList;
}


public void onDataChanged() {

}

public void onItemMoved(int itemPos, int targetPos) {

if (itemPos < targetPos) {
for (int i = itemPos; i < targetPos; i++) {
Collections.swap(records, i, i + 1);
Collections.swap(references, i, i + 1);
}
} else {
for (int i = itemPos; i > targetPos; i--) {
Collections.swap(records, i, i - 1);
Collections.swap(references, i, i - 1);
}
}
notifyItemMoved(itemPos, targetPos);

}


@Override
public int getItemViewType(int position) {
return 0;
}

@Override
public int getItemCount() {
return records.size();
}

public void stopListening() {

}

private void updateArrays(DataSnapshot dataSnapshot){
records.clear();
references.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
references.add(snapshot.getRef());
records.add(snapshot.getValue(Record.class));
}

}

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!isDragging) {
updateArrays(dataSnapshot);

}
}

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded: "+dataSnapshot.toString());
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
int i=-1;
for (DatabaseReference r : references) {
if(dataSnapshot.getKey().equals(r.getKey())){
i = references.indexOf(r);
break;
}
}
if(i!=-1){
records.set(i, dataSnapshot.getValue(Record.class ));
references.set(i, dataSnapshot.getRef());
if(!isDragging) notifyItemChanged(i);
}
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
int i=-1;
for (DatabaseReference r : references) {
if(dataSnapshot.getKey().equals(r.getKey())){
i = references.indexOf(r);
break;
}
}
if(i!=-1){
records.remove(i);
references.remove(i);
notifyItemRemoved(i);
}
Log.d(TAG, "onChildRemoved: child: "+i+" " +dataSnapshot.toString());
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}

public void onStartDragging() {
isDragging = true;
}

public void onStopDragging() {
isDragging = false;
}

public class RecordViewHolderIns extends RecyclerView.ViewHolder {
public TextView text;
public TextView date;


public RecordViewHolderIns(final View itemView) {
super(itemView);
text = itemView.findViewById(R.id.activ_item_text);
date = itemView.findViewById(R.id.record_date);


itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
});

}


}
}

触控助手...

  ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

@Override
public boolean isItemViewSwipeEnabled() {
return false;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
fireBaseRvAdapter.onStartDragging();
} else if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {

}
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
final int firstPosition = viewHolder.getAdapterPosition();
final int secondPosition = target.getAdapterPosition();
DatabaseReference firstItemRef = fireBaseRvAdapter.getRef(viewHolder.getAdapterPosition());
DatabaseReference secondItemRef = fireBaseRvAdapter.getRef(target.getAdapterPosition());

firstItemRef.child(FBdatabase.FIELD_ORDER).setValue(secondPosition);

secondItemRef.child(FBdatabase.FIELD_ORDER).setValue(firstPosition);

fireBaseRvAdapter.onItemMoved(firstPosition, secondPosition);
return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

}

@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
fireBaseRvAdapter.onStopDragging();

}
};

在您的 Activity 中初始化 recyclerView:

 recycleListView = (RecyclerView) findViewById(R.id.list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recycleListView.setLayoutManager(layoutManager);
DefaultItemAnimator an = new DefaultItemAnimator();
recycleListView.setItemAnimator(an);

//Firebase
fb = new FBdatabase(); //Some firebase initialization here

fireBaseRvAdapter = new CustomFirebaseAdapter(
Record.class,
R.layout.list_item,
MyFireBaseRvAdapter.RecordViewHolderIns.class,
fb.getRef().orderByChild(FBdatabase.FIELD_ORDER)
);
fireBaseRvAdapter.setOnItemClickListener(this);
recycleListView.setAdapter(fireBaseRvAdapter);

//Firebase

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(recycleListView);

关于java - 使用 firebase 在 recyclerView 中拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46138183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com