我要重写一个布局,然后留有二个对外的方法。一个是addViews()【动态的增加View】,别外一个是changeType()【改变控件的布局】。这两个现在基本实现了。我用 的是TableLayout。但是如果想随意交换view的位置,并且有拖动的效果。怎么实现??
布局中(除绝对布局之外)可以实现这种效果吗?如果只是一个drawable这个功能好实现,但是现在是一个view。有没有大仙作过的请指教一下。
布局中(除绝对布局之外)可以实现这种效果吗?如果只是一个drawable这个功能好实现,但是现在是一个view。有没有大仙作过的请指教一下。
解决方案 »
- 求大侠帮忙看看哪里错了啊,谢谢
- 新手答疑:请教如何使用Context?
- 我用自己写的phone应用替换了android源码中package/apps下的phone应用。那么,请问我的这个phone应用有系统权限吗。
- 求助:在android里如何调用动态链接库.so?
- android QQ
- 调试程序时怎样终止?
- android 关于通知栏开发的兼容性问题!!!!!!!
- 安卓项目分工
- FTPClient SockerTimeoutException
- myListActivity 和 myListAdapter 的控件传值问题。
- Android4.0都有哪些变化,知道的都进来说下
- LogCat为什么没有任何输出信息?
谢谢了,这个效果有点像。有时间看一下吧,也是从别人那里COPY来的。import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;public class DragAllViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); lLayout = (LinearLayout) findViewById(R.id.line1); for (int i = 0; i < lLayout.getChildCount(); i++) {
TextView tx = (TextView) lLayout.getChildAt(i);
if (tx != null) {
tx.setOnTouchListener(touchListener);
tx.setLongClickable(true);
} else {
Log.i("", "Err");
}
} } private int point1;
private int point2;
private int startX;
private int startY;
private LinearLayout lLayout;
OnTouchListener touchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction(); int x = (int) event.getRawX();
int y = (int) event.getRawY(); Log.i("", "x=" + x + " y=" + y); switch (action) {
// 鼠标按下 拖拉动作开始
case MotionEvent.ACTION_DOWN:
point1 = v.getTop(); startX = (int) event.getX();
startY = y - v.getTop(); Log.i("", "startX=" + startX + " startY=" + startY);
break; // 鼠标移动 拖拉动作进行中
case MotionEvent.ACTION_MOVE:
int temp_x = x - startX;
int temp_y = y - startY;
Log.i("", temp_x+" , "+temp_y);
v.layout(x - startX, y - startY, x + v.getWidth() - startX, y
- startY + v.getHeight());
v.bringToFront();
v.postInvalidate(); break;
// 鼠标释放 拖拉动作结束
case MotionEvent.ACTION_UP:
point2 = v.getTop(); // 计算插入位置 位于哪两个相邻View之间
int dest = getLocation(v); // remove ori view, and then add view here
lLayout.removeView(v);
lLayout.addView(v, dest);
break;
}
return false;
} }; public int getLocation(View v) {
for (int i = 0; i < lLayout.getChildCount() - 1; i++) {
TextView tv = (TextView) lLayout.getChildAt(i);
TextView tv2 = (TextView) lLayout.getChildAt(i + 1); if (tv.getTop() < v.getTop() && tv2.getTop() > v.getTop()) {
// refer delta of point1 & point2
if (point1 < point2) { // drag to bottom
return i + 1;
} else { // drag to up
return i + 1;
}
} }
// otherwise return last location
return lLayout.getChildCount() - 1;
}}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/line1" >
<TextView
android:id="@+id/tv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="一"/>
<TextView
android:id="@+id/tv02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:text="二"/>
<TextView
android:id="@+id/tv03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:text="三"/>
<TextView
android:id="@+id/tv04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
android:text="四"/>
</LinearLayout>