Android Scrollview onDragListener不能正常工作?

时间:2015-05-18 09:32:01

标签: android scrollview drag

我正在尝试编写代码来将dragListener添加到ScrollView。但是在添加监听器时没有开关案例得到满足。所以,我不是日志中的任何结果。

MyCode:

MainActivity.java

public class MainActivity extends Activity {

ScrollView scrollView;

LinearLayout lnr;

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

    scrollView = (ScrollView) findViewById(R.id.scrl);

    lnr = (LinearLayout) findViewById(R.id.lnr);

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

        TextView txt1 = new TextView(this);

        txt1.setText("Nice One"+i);
        txt1.setTextSize(20);

        lnr.addView(txt1);

    }

    scrollView.setOnDragListener(new OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            // TODO Auto-generated method stub
            int action = event.getAction();
            String LOGCAT = "Demo Scroll";

            switch (action) {
                case DragEvent.ACTION_DRAG_STARTED:
                    Log.d(LOGCAT, "Drag event started");
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.d(LOGCAT, "Drag event entered into ");
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    Log.d(LOGCAT, "Drag event exited from ");
                    break;
                case DragEvent.ACTION_DROP:
                    Log.d(LOGCAT, "Dropped");

                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    Log.d(LOGCAT, "Drag ended");
                    break;
                default:
                    Log.d(LOGCAT, "Nothing");
                    break;
            }
            return true;
        }
    });
}


}

activity_main.xml中

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:id="@+id/scrl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:background="#ffcc00">

    <LinearLayout 
        android:id="@+id/lnr"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Test Layout"
        android:textSize="30sp"
        />

    </ScrollView>

所以这是我的测试应用程序的上述代码,请让我知道一些解决方案,如何为我的Scrollview启用Draglistener。

0 个答案:

没有答案