有什么方法可以在API 8 Android中实现拖放功能?
看起来为here,但它出现在API 11之后.
我需要将视图从一个linearLayout拖到另一个.我如何在API 8上实现此功能?
解决方法:
这样尝试
import androID.app.Activity;import androID.content.ClipData;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Point;import androID.graphics.Rect;import androID.os.Bundle;import androID.vIEw.DragEvent;import androID.vIEw.VIEw;import androID.vIEw.VIEw.DragShadowBuilder;import androID.vIEw.VIEw.OnDragListener;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class MainActivity extends Activity {ListVIEw source = null;TextVIEw target1 = null;TextVIEw target2 = null;TextVIEw target3 = null;String[] ListItems = {"Samsung", "Apple", "Google", "Nokia"};@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); source = (ListVIEw)findVIEwByID(R.ID.dragSource); target1 = (TextVIEw) findVIEwByID(R.ID.dragTarget1); target2 = (TextVIEw) findVIEwByID(R.ID.dragTarget2); target3 = (TextVIEw) findVIEwByID(R.ID.dragTarget3); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, androID.R.layout.simple_List_item_1, ListItems); source.setAdapter(adapter); // Start Drag source.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { String item = ListItems[position]; ClipData data = ClipData.newPlainText("DragIt", item); source.startDrag(data, new MyShadowBuilder(vIEw), null, 0); } }); // Handle Drag target1.setonDragListener(new MyDragListener()); target2.setonDragListener(new MyDragListener()); target3.setonDragListener(new MyDragListener());}// Drag Shadowprivate class MyShadowBuilder extends DragShadowBuilder { public MyShadowBuilder(VIEw v) { super(v); } @OverrIDe public voID onDrawShadow(Canvas canvas) { // Set Drag image background or anything you want int wIDth = getVIEw().getWIDth(); int height = getVIEw().getHeight(); Paint paint = new Paint(); paint.setcolor(0x55858585); canvas.drawRect(new Rect(0, 0, wIDth, height), paint); super.onDrawShadow(canvas); } @OverrIDe public voID onProvIDeShadowMetrics(Point shadowSize, Point shadowtouchPoint) { int wIDth = getVIEw().getWIDth(); int height = getVIEw().getHeight(); shadowSize.set(wIDth, height); shadowtouchPoint.set(wIDth/2, height); }}// Drag Listenerprivate class MyDragListener implements OnDragListener { private final int DEFAulT_BG_color = 0xFF858585; private final int HIGHliGHT_BG_color = 0xFF0000FF; @OverrIDe public boolean onDrag(VIEw v, DragEvent event) { if(event.getAction() == DragEvent.ACTION_DRAG_ENTERED) { v.setBackgroundcolor(HIGHliGHT_BG_color); } else if(event.getAction() == DragEvent.ACTION_DRAG_EXITED) { v.setBackgroundcolor(DEFAulT_BG_color); } else if(event.getAction() == DragEvent.ACTION_DROP) { // Perform drop ClipData clip = event.getClipData(); ClipData.Item item = clip.getItemAt(0); String text = item.getText().toString(); ((TextVIEw) v).setText(text); v.setBackgroundcolor(DEFAulT_BG_color); } // Send true to Listen All Drag Events. return true; }}}
XML就像:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:orIEntation="horizontal" ><linearLayout androID:layout_wIDth="161dp" androID:layout_height="fill_parent" androID:background="#FF858585" androID:orIEntation="vertical" > <TextVIEw androID:ID="@+ID/dragTarget1" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="0.04" androID:text="Drop Here" androID:textAppearance="?androID:attr/textAppearanceLarge" /> <TextVIEw androID:ID="@+ID/dragTarget2" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="0.04" androID:text="Drop Here" androID:textAppearance="?androID:attr/textAppearanceLarge" /> <TextVIEw androID:ID="@+ID/dragTarget3" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_weight="0.04" androID:text="Drop Here" androID:textAppearance="?androID:attr/textAppearanceLarge" /></linearLayout><linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <ListVIEw androID:ID="@+ID/dragSource" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" > </ListVIEw></linearLayout>
总结 以上是内存溢出为你收集整理的Android拖放功能全部内容,希望文章能够帮你解决Android拖放功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)