使用子节点按位置拖放可扩展的RecyclerView列表项

时间:2017-11-09 04:03:32

标签: android android-recyclerview expandablelistview

移动父列表项时,我无法移动子列表项。当用户在Android Studio中更改父项的位置时,还需要移动子项。

MyAdapter.java

public class myAdapter extends ExpandableRecyclerAdapter<TitleParentViewHolder, TitleChildViewHolder> implements ItemTouchHelperAdapter {

    private final OnStartDragListener mDragStartListener;
    LayoutInflater inflater;
    TitleParent titleParent;
    ArrayList<TitleParent> titleParent1 = new ArrayList<>();
    ArrayList<TitleChild> TitleChild1 = new ArrayList<>();

    public myAdapter(Context context, List<ParentObject> parentItemList, OnStartDragListener dragListner) {
        super(context, parentItemList);
        inflater = LayoutInflater.from(context);
        mDragStartListener = dragListner;
    }

    @Override
    public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
        View view = inflater.inflate(R.layout.list_parent, viewGroup, false);
        return new TitleParentViewHolder(view);
    }

    @Override
    public TitleChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
        View view = inflater.inflate(R.layout.layout_child, viewGroup, false);
        return new TitleChildViewHolder(view);
    }

    @Override
    public void onBindParentViewHolder(final TitleParentViewHolder titleParentViewHolder, final int i, Object o) {
        titleParent = (TitleParent) o;
        titleParent1.add(titleParent);
        for (int j = 0; j < titleParent1.size(); j++) {
            titleParentViewHolder._textView.setText(titleParent1.get(j).getTitle());
        }
        titleParentViewHolder._textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "PostionParent: " + i, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onBindChildViewHolder(final TitleChildViewHolder titleChildViewHolder, final int i, Object o) {
        TitleChild titleChild = (TitleChild) o;
        TitleChild1.add(titleChild);

        for (int k = 0; k < TitleChild1.size(); k++) {
            titleChildViewHolder.option1.setText(TitleChild1.get(k).getOption1());
            titleChildViewHolder.option2.setText(TitleChild1.get(k).getOption2());
        }

        titleChildViewHolder.option1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(titleChildViewHolder);
                }
                return false;
            }
        });
        titleChildViewHolder.option2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(titleChildViewHolder);
                }
                return false;
            }
        });
        titleChildViewHolder.option1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "PostionChiled: " + i, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {

        if (fromPosition < titleParent1.size() && toPosition < titleParent1.size()) {
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(titleParent1, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(titleParent1, i, i - 1);

                }
            }
            notifyItemMoved(fromPosition, toPosition);
        }
        return true;

    }

    @Override
    public void onItemDismiss(int position) {
        titleParent1.remove(position);
        if (TitleChild1.size() > 0) {
            TitleChild1.remove(position);
        }
        notifyItemRemoved(position);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements OnStartDragListener {

    int i = 1;
    private RecyclerView recyclerView;
    private ItemTouchHelper mItemTouchHelper;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ((myAdapter) recyclerView.getAdapter()).onSaveInstanceState(outState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        myAdapter adapter = new myAdapter(this, initDate(), this);
        adapter.setCustomParentAnimationViewId(R.id.tvButton);
        adapter.setParentClickableViewAnimationDefaultDuration();
        adapter.setParentAndIconExpandOnClick(true);

        ItemTouchHelper.Callback callback = new EditItemTouchHelperCallback(adapter);
        mItemTouchHelper = new ItemTouchHelper(callback);
        mItemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.setAdapter(adapter);
    }

    private List<ParentObject> initDate() {
        TitleCreator titleCreator = TitleCreator.get(this);
        List<TitleParent> title = titleCreator.getAll();
        List<ParentObject> parentObjects = new ArrayList<>();
        for (TitleParent titleParent : title) {
            i = i+1;
            List<Object> childlist = new ArrayList<>();
            childlist.add(new TitleChild("Add: " + i, "Send"));
            titleParent.setChildObjectList(childlist);
            parentObjects.add(titleParent);
        }
        return parentObjects;
    }


    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }
}

EditItemTouchHelperCallback.java

public class EditItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private final myAdapter mAdapter;

    public EditItemTouchHelperCallback(myAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

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

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
    }
}

当我移动我的父列表项时移动子列表项并在父项的位置将由用户更改时移动子项

0 个答案:

没有答案