如何当一个列表项是长pressed加载列表视图菜单里面 [英] How to load menu inside a list view when a list item is long pressed
问题描述
我试图模仿Twitter的Android应用程序的操作。当你长preSS /滑动鸣叫(列表项),它看起来像这样
这很容易让用户采取的列表项的操作,而无需离开屏幕。我该怎么做我的应用程序类似的东西?
现在我添加了一个上下文菜单,看起来像这样(请注意,它淡出了应用程序的其余部分)。
我用 registerForContextMenu做到了这一点(ListView控件)
目前,我的名单是这样的:
ListAdapter适配器=新SimpleAdapter(
MainActivity.this,myList上,
R.layout.list_items,新的String [] {LI_ID,
LI_NAME,LI_COUNT},新的INT [] {
R.id.li_id,R.id.li_name,R.id.li_count});
setListAdapter(适配器);
和我的布局仅仅是这样的:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / li_id
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:知名度=水涨船高/> <的TextView
机器人:ID =@ + ID / li_name
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =16dip
机器人:文字颜色=#000000
机器人:paddingTop =15dip
机器人:paddingBottom会=15dip
机器人:paddingLeft =10dip
机器人:文字样式=大胆/> < TextView的机器人:ID =@ + ID / li_count
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:文字颜色=#FFFFFF
机器人:文字样式=大胆
机器人:背景=#9ed321
机器人:paddingRight =3dip
机器人:paddingLeft =3dip/>
< / RelativeLayout的>
您可以只窝在这最初不可见的每一个人的ListView
项目的另一个布局,然后只需拨动知名度在很长的单击事件的看法。
例如:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:animateLayoutChanges =真正的> <的TextView
机器人:ID =@ + ID / main_view
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=TextView的
机器人:填充=16DP
机器人:layout_gravity =中心/> <的LinearLayout
机器人:ID =@ + ID / hidden_view
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:知名度=水涨船高> <按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=删除/> <按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=编辑/> < / LinearLayout中>< / LinearLayout中>
然后在你的活动,只需设置一个 onItemLongClickListener
在的ListView
,以及切换
的能见度 hidden_view
:
公共布尔onItemLongClick(适配器视图<>母公司,观景,INT位置,长的id){
的LinearLayout MVIEW = view.findViewById(R.id.hidden_view); 开关(mView.getVisibility()){
案例View.VISIBLE:
mView.setVisibility(View.GONE);
打破;
案例View.GONE:
mView.setVisibility(View.VISIBLE);
打破;
} }}
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>
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;
}
}
}
这篇关于如何当一个列表项是长pressed加载列表视图菜单里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!