将Listview项拖放到抽屉的新单独区域中

时间:2013-10-13 22:17:49

标签: android android-listview

在我的应用程序中,我列出了在滑动抽屉中列表视图中设置的所有用户安装的应用程序。如何允许用户将一个链接从列表视图拖动到抽屉的另一个区域(抽屉拉出的区域)?

我调查了拖放,我注意到每个项目都需要一个ID(我无法弄清楚如何为每个项目创建一个ID)。

不管怎样,这是我的编码:

Drag_and_Drop_App:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);

    // load list application
    mListAppInfo = (ListView)findViewById(R.id.lvApps);
    // create new adapter
    AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);
    // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }
    });
}
}

请注意,我有其他类在后台运行以收集用户应用程序并运行onClickListners,但这是实际小部件中显示的内容。

这是Drag_and_Drop_App.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" >

<SlidingDrawer
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:orientation="horizontal"
    android:layout_marginTop="200dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical"
        android:background="#00FF00">

<ListView
    android:id="@+id/lvApps"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"       
/>


    </LinearLayout>
</SlidingDrawer>

</RelativeLayout>

我知道我可能想要使用OnLongClick侦听器,以便当Listview中的项目被长按时,用户可以将其拖放到视图的其他部分。

我也知道链接/应用程序将被删除的视图将是使用DragListener的一个巨大的droptarget。 (我一直在看拖放here

的教程

如何为每个listview项创建一个ID,以便它可以在布局中使用指定的LongTouchListener?

我希望这是有道理的,如果不是,请告诉我!

0 个答案:

没有答案