使用拖放功能更改两个视图(文本视图)的位置

时间:2017-06-05 10:41:08

标签: java android

我希望使用拖动侦听器和动画在Android中使用拖放功能来更改文本视图的位置。我使用以下代码完成了设计。我想使用Drag Listener更改文本视图的顺序。谢谢i give a image of design and i want change position of text view using drag and drop

public void loadtable()

{


    Sorting_Linear = (LinearLayout) findViewById(R.id.Sorting_Linear);
    submit = (Button) findViewById(R.id.submit);
    LinearLayout[] llRow = new LinearLayout[5];

    final TextView[] outletnametxt = new TextView[5];
    final ImageView[] imageButtonup = new ImageView[5];
    final ImageView[] imageButtondown = new ImageView[5];
    final LinearLayout[] Linearlayout = new LinearLayout[5];


    for (int i = 0; i < 5; i++) {

        llRow[i] = new LinearLayout(mContext);
        LinearLayout.LayoutParams paramsllRow = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        paramsllRow.setMargins(0, 2, 0, 0);
        llRow[i].setLayoutParams(paramsllRow);
        llRow[i].setOrientation(LinearLayout.HORIZONTAL);

        outletnametxt[i] = new TextView(mContext);
        outletnametxt[i].setLayoutParams(nametxt);
        outletnametxt[i].setText(olm_name[i]);
        outletnametxt[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        outletnametxt[i].setTextSize(20);
        outletnametxt[i].setGravity(Gravity.CENTER);
        outletnametxt[i].setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        llRow[i].addView(outletnametxt[i]);


        imageButtonup[i] = new ImageView(mContext);
        imageButtonup[i].setLayoutParams(imagebtnup);
        imageButtonup[i].setImageResource(R.drawable.arrowup);


        imageButtondown[i] = new ImageView(mContext);
        imageButtondown[i].setLayoutParams(imagebtndown);
        imageButtondown[i].setImageResource(R.drawable.arrowdown);


        Linearlayout[i] = new LinearLayout(mContext);
        Linearlayout[i].setLayoutParams(linearlayout);
        Linearlayout[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        Linearlayout[i].setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        Linearlayout[i].setOrientation(LinearLayout.VERTICAL);
        Linearlayout[i].addView(imageButtonup[i]);
        Linearlayout[i].addView(imageButtondown[i]);
        llRow[i].addView(Linearlayout[i]);

        Sorting_Linear.addView(llRow[i]);


    }

1 个答案:

答案 0 :(得分:1)

创建以下界面ItemTouchHelperAdapter

public interface ItemTouchHelperAdapter {

boolean onItemMove(int fromPosition, int toPosition);

void onItemDismiss(int position);}

创建第二个接口ItemTouchHelperViewHolder

public interface ItemTouchHelperViewHolder {

void onItemSelected();

void onItemClear();}

创建OnStartDragListener

 public interface OnStartDragListener {

void onStartDrag(RecyclerView.ViewHolder viewHolder);}

在下面创建

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {

public static final float ALPHA_FULL = 1.0f;

private final ItemTouchHelperAdapter mAdapter;

public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
    mAdapter = adapter;
}

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

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

@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    // Set movement flags based on the layout manager
    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        final int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    } else {
        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        final int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
    if (source.getItemViewType() != target.getItemViewType()) {
        return false;
    }

    // Notify the adapter of the move
    mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
    // Notify the adapter of the dismissal
    mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
    if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
        // Fade out the view as it is swiped out of the parent's bounds
        final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    // We only want the active item to change
    if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
        if (viewHolder instanceof ItemTouchHelperViewHolder) {
            // Let the view holder know that this item is being moved or dragged
            ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
            itemViewHolder.onItemSelected();
        }
    }

    super.onSelectedChanged(viewHolder, actionState);
}

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

    viewHolder.itemView.setAlpha(ALPHA_FULL);

    if (viewHolder instanceof ItemTouchHelperViewHolder) {
        // Tell the view holder it's time to restore the idle state
        ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
        itemViewHolder.onItemClear();
    }
}}

现在只需将此代码添加到您的回收站视图适配器

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));    

adapter = new RecyclerListAdapter(Activity_OutletSorting.this, onStartDragListener, olm_name,idopeningclosing);
            recyclerView.setAdapter(adapter);

            ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
            mItemTouchHelper = new ItemTouchHelper(callback);
            mItemTouchHelper.attachToRecyclerView(recyclerView);