How to load menu inside a list view when a list it

2019-08-02 20:49发布

问题:

I'm trying to mimic an action in the Twitter android app. When you long press/swipe a tweet (list item) it looks like this

This makes it easy for the user to take actions on the list item without leaving the screen. How can I do something similar in my app?

For now I've added a context menu which looks like this (notice that it fades out rest of the app).

I achieved this by registerForContextMenu(listView)

Currently, my list is like this:

ListAdapter adapter = new SimpleAdapter(
        MainActivity.this, myList,
        R.layout.list_items, new String[] {LI_ID,
        LI_NAME, LI_COUNT}, new int[] {
        R.id.li_id, R.id.li_name, R.id.li_count});
setListAdapter(adapter);

and my layout is simply like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/li_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/li_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>

    <TextView android:id="@+id/li_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:background="#9ed321"
        android:paddingRight="3dip"
        android:paddingLeft="3dip"/>


</RelativeLayout>

回答1:

Have you looked tried an expandable list view ?

http://developer.android.com/reference/android/widget/ExpandableListView.html

http://www.youtube.com/watch?v=mwE61B56pVQ



回答2:

You could just nest another layout in each individual ListView item that's initially invisible, then just toggle the visibility of the view on a long click event.

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:animateLayoutChanges="true" >

    <TextView
        android:id="@+id/main_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:padding="16dp"
        android:layout_gravity="center" />

    <LinearLayout
        android:id="@+id/hidden_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Delete" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Edit" />

    </LinearLayout>

</LinearLayout>

Then in your activity, just set an onItemLongClickListener on your ListView, and toggle the visibility of the hidden_view:

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
    LinearLayout mView = view.findViewById(R.id.hidden_view);

       switch(mView.getVisibility()){
            case View.VISIBLE:
                mView.setVisibility(View.GONE);
                break;
            case View.GONE:
                mView.setVisibility(View.VISIBLE);
                break;
            }

        }

}