gpt4 book ai didi

android - 如何在 android 中使用拖放框架从一个 LinearLayout 交换到另一个 LinearLayout

转载 作者:太空狗 更新时间:2023-10-29 14:35:05 27 4
gpt4 key购买 nike

我在一个 LinearLayout 中有 TextViewImageView。我想以这种方式将第一个 LinearLayout 拖到第二个 LinearLayout ,当我将第一个线性拖放到第二个 LinearLayout 时,它的 TextViewImageView 应该放在第一个 LinearLayout 中,反之亦然。我已经完成研发,发现可以将布局从第一个拖放到第二个,但第二个布局不在第一个布局中。

问题是布局没有得到交换。我可以从第一拖放到第二,但无法从第一拖放到第二。

下面是我的 fragment :

public class MoreDestinationFragment extends Fragment implements View.OnClickListener, View.OnTouchListener, View.OnDragListener {

private OnFragmentInteractionListener mListener;
private LinearLayout llNews;

public MoreDestinationFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_more_destination, container, false);

llNews = view.findViewById(R.id.ll_new);

llNews.setOnClickListener(this);
llNews.setOnTouchListener(this);
llNews.setOnDragListener(this);

return view;
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ll_new:
Navigation.findNavController(view).navigate(R.id.action_moreDestinationFragment_to_newsDestinationFragment);
break;

}

@Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d("xxxxxxx", "Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("xxxxxxx", "Drag event entered into " + layoutview.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d("xxxxxxx", "Drag event exited from " + layoutview.toString());
break;
case DragEvent.ACTION_DROP:
Log.d("xxxxxxx", "Dropped");
View view = (View) dragevent.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d("xxxxxxx", "Drag ended");
break;
default:
break;
}
return true;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}

public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}

请帮帮我,请提出解决方案。谢谢。

最佳答案

You can do this with RecyclerView with GridLayoutManager instead of several LinearLayout's as below:

步骤:

在gradle app模块中添加以下依赖

// RecyclerView
implementation 'androidx.recyclerview:recyclerview:1.0.0'

// CardView
implementation 'androidx.cardview:cardview:1.0.0'

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

</androidx.recyclerview.widget.RecyclerView>

列表项布局(RecyclerView 的)

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="4dp"
android:clickable="true"
android:elevation="4dp"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="10dp"
card_view:cardUseCompatPadding="true"
tools:ignore="UnusedAttribute">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="4dp">

<ImageView
android:id="@+id/imThumbnail"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
tools:background="@drawable/ic_android_black_24dp" />

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:text="Android" />

</LinearLayout>

</androidx.cardview.widget.CardView>

要在 RecyclerView 中填充的项目的模型类

public class Item {

private String mTitle;
private int mThumbnail;

public String getTitle() {
return mTitle;
}

public Item(String title, int thumbnail) {
this.mTitle = title;
mThumbnail = thumbnail;
}

public void setTitle(String title) {
this.mTitle = title;
}

public int getThumbnail() {
return mThumbnail;
}

public void setThumbnail(int thumbnail) {
mThumbnail = thumbnail;
}

}

RecyclerView 适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

List<Item> mItems;

public Adapter(ArrayList items) {
mItems = items;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.bindViews(mItems.get(position));
}

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

class ViewHolder extends RecyclerView.ViewHolder {
TextView mTitle;
ImageView mThumbnail;

ViewHolder(@NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.tvTitle);
mThumbnail = itemView.findViewById(R.id.imThumbnail);
}

void bindViews(Item item) {
mTitle.setText(item.getTitle());
mThumbnail.setImageResource(item.getThumbnail());
}
}
}

最后是主要行为

利用 ItemTouchHelper 类拖动功能并使用 attachToRecyclerView() 方法将其附加到 RecyclerView

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private ArrayList<Item> mItems;
private Adapter mAdapter;

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

populateItems();

mAdapter = new Adapter(mItems);

RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
recyclerView.setAdapter(mAdapter);


ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP |
ItemTouchHelper.DOWN |
ItemTouchHelper.LEFT |
ItemTouchHelper.RIGHT,
0) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return false;
}

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

}
});

itemTouchHelper.attachToRecyclerView(recyclerView);

}

private void populateItems() {
mItems = new ArrayList<>();
mItems.add(new Item("Android", R.drawable.ic_android_black_24dp));
mItems.add(new Item("Archive", R.drawable.ic_archive_black_24dp));
mItems.add(new Item("Alarm", R.drawable.ic_access_alarm_black_24dp));
mItems.add(new Item("Block", R.drawable.ic_block_black_24dp));
mItems.add(new Item("CAll", R.drawable.ic_call_black_24dp));
mItems.add(new Item("Android", R.drawable.ic_android_black_24dp));
mItems.add(new Item("Archive", R.drawable.ic_archive_black_24dp));
mItems.add(new Item("Alarm", R.drawable.ic_access_alarm_black_24dp));
mItems.add(new Item("Block", R.drawable.ic_block_black_24dp));
mItems.add(new Item("CAll", R.drawable.ic_call_black_24dp));
}

private void moveItem(int oldPos, int newPos) {
Item temp = mItems.get(oldPos);
mItems.set(oldPos, mItems.get(newPos));
mItems.set(newPos, temp);
mAdapter.notifyItemMoved(oldPos, newPos);
}

}

这里是结果

希望能满足你的需求

关于android - 如何在 android 中使用拖放框架从一个 LinearLayout 交换到另一个 LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787880/

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