android界面中,有一个功能是点击listview的每个item,下方弹出两个菜单,可以点击进入别的界面,这个功能可以使用开源项目expandablelistview,的确是可以实现,但发现导入的代码过多,显得很臃肿,经过师傅指点,我采用的是如下方法,步骤如下:
1.新建一个listview,需要在外层套一层scrollview,不过使用scrollview会导致listview的高度只有一个item,需要使用自定义的listview,代码如下:
package allone.verbank.apad.client.component.fixedListView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.ListView;
public class PriceListViewScroll extends ListView {
public PriceListViewScroll(Context context) {
super(context);
}
public PriceListViewScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PriceListViewScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
listview的界面文件如下:
2.每个item下方的菜单,其实原来是一开始隐藏一段view,有一定的高度,防止菜单,菜单则是使用popuwindow来弹出,item的xml文件如下:
3.下一步就是在代码中,当长按listview时就显示hidepricemenu这个textview,以便于弹出菜单,菜单是使用popuwindow来显示,如下关键代码:
list.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> arg0, View sourView,
final int arg2, long arg3) {
hidepricemenu = (TextView) sourView
.findViewById(R.id.hidepricemenu);
hidepricemenu.setVisibility(View.VISIBLE);
String selectIntrument = maps.get(arg2).get(PRICE_TC_INST)
.toString();
buysellLongClickAction(sourView, selectIntrument, hidepricemenu);
return true;
}
});
private void buysellLongClickAction(final View inner,
final String instname, final TextView hide_PriceMenu_copy) {
StaticContext.TRADE_INST = instname;
if (popupMenu != null) {
popupMenu.destroyPopupMenu();
}
// 浮出菜单
popupMenu = PopupMenuFactory.createPopupWindow(getSelfActivity(),
inner, IPopupMenu.MENU_ID_PRICE, instname);
popupMenu.showPopupMenu();
popupMenu.createPupupWindow().setOnDismissListener(
new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
hide_PriceMenu_copy.setVisibility(View.GONE);
}
});
// 当为最后一行的时候,弹出的菜单会被隐藏,这里使用scroll向上移动
handler.post(new Runnable() {
@Override
public void run() {
if (price_scroll.getScrollY() > 40) {
price_scroll.scrollTo(0, price_scroll.getScrollY() + 200);
}
}
});
}
创建popuwindow的代码如下:
package allone.verbank.apad.client.component.menu.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import allone.verbank.apad.client.R;
import allone.verbank.apad.client.StaticContext;
import allone.verbank.apad.client.component.menu.IPopupMenu;
import allone.verbank.apad.client.doc.DocCaptain;
import allone.verbank.apad.client.doc.IBundleCommData;
import allone.verbank.apad.client.event.IStationEventName;
import allone.verbank.apad.client.event.StationEventCaptain;
import allone.verbank.apad.client.event.StationEventData;
import allone.verbank.apad.client.frame.main.MainActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
public class PriceTradePopupMenu implements IPopupMenu {
private ArrayList
4.使用这种方式的时候,当最后一个item,会出现弹出的菜单在listview的下方的,位置被遮挡住了,所以需要前面scrollview,判断下方的高度,然后进行listview的移动,使用scrollto的方法,如下代码:
// 当为最后一行的时候,弹出的菜单会被隐藏,这里使用scroll向上移动
if (price_scroll.getScrollY() > 10) {
price_scroll.scrollTo(0, price_scroll.getScrollY() + 200);
}
if (price_scroll.getScrollY() == 0) {
price_scroll.scrollTo(0, price_scroll.getScrollY() + 50);
}
至此就可以完成该功能,有些代码省略了,因为是公司的,有问题可以交流!!!