gpt4 book ai didi

android - 在布局中拖放图像

转载 作者:太空狗 更新时间:2023-10-29 13:16:19 24 4
gpt4 key购买 nike

我有一个 10X4 的表格,其中一些单元格有图像,需要将图像拖放到单元格的边表中。如何判断单元格是否有图像?

我的代码:用于创建单元格,拖放。

public class WhiteFragment extends Fragment {
View mView;
private TableLayout Table1;
ImageView img;


int[] imageArray = {R.drawable.a, R.drawable.a, R.drawable.a, R.drawable.a};

LinearLayout layout;
View parentView;

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

Table1 = (TableLayout) mView.findViewById(R.id.table);
for (int i = 0; i < 4; i++) {
TableRow TR = new TableRow(getActivity());

for (int j = 0; j < 10; j++) {
LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.table_image, null, false);
img = (ImageView) row.findViewById(R.id.ivImage);
layout=(LinearLayout) row.findViewById(R.id.layout);

if(j%2==0)
img.setImageResource(imageArray[i]);
layout.setOnTouchListener(new MyTouchListener());
layout.setOnDragListener(new MyDragListener());

TR.addView(row);

}
Table1.addView(TR);
}
return mView;
}

float x, y = 0.0f;
boolean ismoving;

private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {

switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.e("owner status","down");
break;
case MotionEvent.ACTION_MOVE:
Log.e("owner status","move");
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);

break;
case MotionEvent.ACTION_UP:
Log.e("owner status","up");
break;
}
return true;
}
}

class MyDragListener implements View.OnDragListener {

@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;

if(owner.getChildCount()==0)
container.addView(view);
else owner.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
}

xml代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/table_border">
<LinearLayout
android:id="@+id/layout"
android:layout_width="120dp"
android:layout_height="120dp">
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" />
</LinearLayout>
</LinearLayout>

建议你的逻辑..谢谢

最佳答案

是的,我使用元素的 setTag 解决了这个问题,同时在保存列表中拖动检查该标签的状态。

关于android - 在布局中拖放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353686/

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