I have an Adapter, Recyclerview and ItemTouchHelper. I want to make placeholders on which, as a result of moving, it will be possible to place items. Placeholders are now made as empty elements. But I don't know how I can put the element directly on this one.
My adapter:
public class DateTimeAdapter extends RecyclerView.Adapter<MainHolder> implements ItemTouchHelperAdapter {
private List<MainItem> values;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
private Activity context;
private final OnStartDragListener mDragStartListener;
private boolean initEmpty = false;
public DateTimeAdapter(Activity context, List<MainItem> values, OnStartDragListener dragListener) {
this.values = values;
this.context = context;
mDragStartListener = dragListener;
}
@NonNull
@Override
public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType){
case (Constants.ITEM_HEADER_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.list_group_item, parent, false);
return new HeaderHolder(view);
case (Constants.ITEM_EVENT_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.item_wo_items, parent, false);
return new ItemHolder(view);
case (Constants.ITEM_PLACEHOLDER_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.item_placeholder, parent, false);
return new PlaceHolder(view);
default: throw new IllegalArgumentException();
}
}
@Override
public void onBindViewHolder(@NonNull final MainHolder holder, int position) {
holder.setData(values.get(position));
if (holder.getItemViewType() == Constants.ITEM_PLACEHOLDER_VIEWTYPE){
}
}
@Override
public int getItemCount() {
return values.size();
}
@Override
public int getItemViewType(int position) {
return values.get(position).getViewType();
}
protected LocalTime getStartTimebyHeader(int position){
ListIterator<MainItem> listIterator = values.listIterator(position);
while (listIterator.hasPrevious()){
MainItem t = listIterator.previous();
if (t.getHeaderItem() != null){
DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
return localTime;
}
}
return null;
}
protected LocalTime getEndTimebyHeader(int position){
ListIterator<MainItem> listIterator = values.listIterator(position);
while (listIterator.hasNext()){
MainItem t = listIterator.next();
if (t.getHeaderItem() != null){
DateTimeFormatter parse = new DateTimeFormatterBuilder().appendPattern("HH:mm").toFormatter();
LocalTime localTime = LocalTime.parse(t.getHeaderItem().getHeaderText(), parse);
return localTime;
}
}
return null;
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(values, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(values, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
values.remove(position);
notifyItemRemoved(position);
}
}
My ItemTouchHelper
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, 0) {
@Override
public boolean canDropOver(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder current, @NonNull RecyclerView.ViewHolder target) {
if (target.getAdapterPosition() != 0 && target.getAdapterPosition() != dta.getItemCount()-1)
return true;
return false;
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
Log.d("onMove", Integer.toString(viewHolder.getAdapterPosition()) + " - " + Integer.toString(target.getAdapterPosition()));
dta.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.UP | ItemTouchHelper.DOWN : 0;
int swipeFlags = viewHolder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE ? ItemTouchHelper.START | ItemTouchHelper.END : 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeS(dta.getStartTimebyHeader(viewHolder.getAdapterPosition()));
showingDatas.get(viewHolder.getAdapterPosition()).getEventItem().setTimeE(dta.getEndTimebyHeader(viewHolder.getAdapterPosition()));
dta.notifyItemChanged(viewHolder.getAdapterPosition());
super.clearView(recyclerView, viewHolder);
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback);
itemTouchHelper.attachToRecyclerView(rvPlan);
How should I do it?
Problem Resolved: Data class is used to distinguish between Normal View and Place Holder View. Using below list in adapter:
data class MyData(var type:Int, var name: String)
val list = mutableListOf(MyData(0,"PlaceHolder"),MyData(1,"Normal"),MyData(1,"Noraml"),MyData(0,"Place Holder"),MyData(1,"Normal"),MyData(1,"Normal"))
Custom Item Touch Listener class:
ItemTouchHelper(object : RecyclerViewDragDetector() {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
if (adapter.getItemViewType(viewHolder.adapterPosition) == 1){
if (adapter.getItemViewType(target.adapterPosition)!= 1){
list[target.adapterPosition].type = 1
list[target.adapterPosition].name = "Normal"
adapter.notifyItemChanged(target.adapterPosition)
list.removeAt(viewHolder.adapterPosition)
adapter.notifyDataSetChanged()
}
}
return true
}
override fun isLongPressDragEnabled(): Boolean {
return true
}
}).attachToRecyclerView(recyclerView)
Change Adapter as per your need. My sample adapter look like below:
inner class MyAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
private val viewTypeHeader = 0
private val viewTypeNormal = 1
inner class MyHolder(view: View): RecyclerView.ViewHolder(view){
val textView: TextView = view.findViewById(R.id.normal_tv)
fun bind(myData: MyData){
textView.text = myData.name
}
}
inner class MyHeaderHolder(view:View): RecyclerView.ViewHolder(view){
val textView: TextView = view.findViewById(R.id.header_tv)
fun bind(myData: MyData){
textView.text = myData.name
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == viewTypeHeader){
val view = LayoutInflater.from(parent.context).inflate(R.layout.header, parent, false)
MyHeaderHolder(view)
}else {
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
MyHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (getItemViewType(position) == viewTypeHeader){
(holder as MyHeaderHolder).bind(list[position])
}else{
(holder as MyHolder).bind(list[position])
}
}
override fun getItemViewType(position: Int): Int {
return if (list[position].type == viewTypeHeader)
return viewTypeHeader
else
viewTypeNormal
}
override fun getItemCount(): Int {
return list.size
}
}