所以我想将弹出窗口锚定到滑动菜单中的特定选项。滑动菜单中有大量项目,但我想获取特定项目的位置,以便我可以使用视图来锚定窗口。
我的问题是,我怎样才能得到那个特定的观点。我有一个SlidingMenuAdapter和一个SlidingMenuFragment。
我希望这个比较清楚。
适配器:
public class SlidingMenuAdapter extends BaseExpandableListAdapter implements OnGroupClickListener, OnChildClickListener, SignInStatusChangedListener, OnTouchListener, OnGroupExpandListener, OnGroupCollapseListener
{
private final LayoutInflater mInflater;
private final String[] mMenuItemsSignedIn;
private final String[] mMenuItemsAnon;
private final String[] mMoreMenuItems;
private final String[] mMyAccountMenuItems;
private final BaseActivity mContext;
private final int srollingDelay = 40;
private ExpandableListView listView;
private int mReservationCount = -1;
private int mShortlistCount = -1;
private static int expandedGroupPosition = -1;
private SlidingMenuItemSelectedListener mListener;
public SlidingMenuAdapter(final Context context, final String[] menuItemsSignedIn, final String[] menuItemsAnon,
final String[] moreMenuItems, final String[] myAccountMenuItems,ExpandableListView listView)
{
mContext = (BaseActivity) context;
mInflater = LayoutInflater.from(context);
mMenuItemsSignedIn = menuItemsSignedIn;
mMenuItemsAnon = menuItemsAnon;
mMoreMenuItems = moreMenuItems;
mMyAccountMenuItems = myAccountMenuItems;
this.listView = listView;
SignInStatusHelper.getInstance().addSignInStatusChangedListener(this);
}
private void bindView(final int position, final ViewHolder viewHolder, final boolean expanded)
{
String title = getGroup(position);
viewHolder.titleText.setText(title);
if(isGroupWelcome(position)) {
viewHolder.titleText.setText(String.format(title, User.init().getFirstName()));
}
int imageRes = getImageRes(position);
if(imageRes!=0) {
viewHolder.homeIcon.setVisibility(View.VISIBLE);
viewHolder.homeIcon.setImageResource(imageRes);
}
else if (isGroupWelcome(position)){
viewHolder.homeIcon.setVisibility(View.GONE);
} else {
viewHolder.homeIcon.setImageResource(R.drawable.sliding_menu_home_icon);
viewHolder.homeIcon.setVisibility(View.INVISIBLE);
}
if (title.equals(mContext.getString(R.string.sliding_menu_current_store)))
{
if (Application.getInstance().getCurrentStore() != null) {
viewHolder.subTitleText.setText(Application.getInstance().getCurrentStore().getName());
viewHolder.subTitleText.setVisibility(View.VISIBLE);
}
else {
viewHolder.subTitleText.setText(mContext.getString(R.string.sliding_menu_no_store_selected));
viewHolder.subTitleText.setVisibility(View.VISIBLE);
}
} else {
viewHolder.subTitleText.setText("");
viewHolder.subTitleText.setVisibility(View.GONE);
}
// if 'More' row, show down arrow
if (isGroupMore(position) || isGroupAccount(position))
{
viewHolder.moreArrow.setVisibility(View.VISIBLE);
if (expanded) {
viewHolder.moreArrow.setImageResource(R.drawable.sliding_menu_less_arrow_black);
} else {
viewHolder.moreArrow.setImageResource(R.drawable.sliding_menu_more_arrow_black);
}
}
else
{
// otherwise make sure its hidden
viewHolder.moreArrow.setVisibility(View.GONE);
}
// if 'Reservations' set count text
if (title.equals(mContext.getString(R.string.sliding_menu_reservations)) && mReservationCount > 0)
{
viewHolder.countText.setText(Integer.toString(mReservationCount));
viewHolder.countText.setVisibility(View.VISIBLE);
}
// if 'Shortlist' set count text
else if (title.equals(mContext.getString(R.string.sliding_menu_shortlist)) && mShortlistCount > 0)
{
viewHolder.countText.setText(Integer.toString(mShortlistCount));
viewHolder.countText.setVisibility(View.VISIBLE);
}
else
{
viewHolder.countText.setVisibility(View.GONE);
}
}
}
片段:
public class SlidingMenuFragment extends BaseFragment implements SlidingMenuItemSelectedListener
{
private SlidingMenuAdapter mSlidingMenuAdapter;
private Integer mShortlistCount;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sliding_menu, null);
}
@Override
public void onViewCreated(final View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final Boolean showMediaLibrary = ConfigManager.getConfigManager().isMediaLibraryEnabled();
final String[] menuItemsSignedIn = showMediaLibrary ?
getResources().getStringArray(R.array.sliding_menu_items_signed_in) :
getResources().getStringArray(R.array.sliding_menu_items_signed_in_no_library);
final String[] menuItemsAnon = showMediaLibrary ?
getResources().getStringArray(R.array.sliding_menu_items_anon) :
getResources().getStringArray(R.array.sliding_menu_items_anon_no_library);
final String[] moreMenuItems = getResources().getStringArray(
R.array.sliding_menu_more_items);
final String[] myAccountMenuItems = getResources().getStringArray(
R.array.sliding_menu_my_account_items);
final ExpandableListView listView = (ExpandableListView) view
.findViewById(R.id.sliding_menu_list);
mSlidingMenuAdapter = new SlidingMenuAdapter(getActivity(), menuItemsSignedIn,
menuItemsAnon, moreMenuItems, myAccountMenuItems , listView);
mSlidingMenuAdapter.setSlidingMenuItemSelectedListener(this);
listView.setAdapter(mSlidingMenuAdapter);
listView.setGroupIndicator(null);
listView.setOnGroupClickListener(mSlidingMenuAdapter);
listView.setOnChildClickListener(mSlidingMenuAdapter);
listView.setOnGroupExpandListener(mSlidingMenuAdapter);
listView.setOnGroupCollapseListener(mSlidingMenuAdapter);
getLoaderManager().initLoader(0, null, mReservationLoaderCallback);
getLoaderManager().initLoader(1, null, mShortlistLoaderCallback);
}
// handle menu item selected
@Override
public boolean slidingMenuItemSelected(final String label)
{
Intent intent = null;
// any menu item which shouldn't cause expansion of a group or open a
// new activity should return true, otherwise false
boolean retVal = false;
boolean closeMenu = true;
if (label.equals(getResources().getString(R.string.sliding_menu_home))) {
intent = new Intent(getActivity(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
} else if (label.equals(getResources().getString(R.string.sliding_menu_heading))) {
// Do nothing when header/ icon tapped
retVal = true;
closeMenu = false;
} else if (label.equals(getResources().getString(R.string.sliding_menu_library))) {
intent = new Intent(getActivity(), LibraryActivity.class);
} else if (label.equals(getResources().getString(R.string.sliding_menu_shortlist))) {
if (mShortlistCount != null && mShortlistCount == 0) {
//ShorlistEmptyDialogFragment.showDialog(getActivity());
SimplePopupWindow popup = new SimplePopupWindow(getActivity());
popup.setLayout(R.layout.shortlist_popup);
popup.showAsDropDown(getView().findViewById(R.id.sliding_menu_list));
closeMenu = false;
retVal = true;
}
}
我试图保持只添加所需的代码。所以在我打电话的片段中
SimplePopupWindow popup = new SimplePopupWindow(getActivity());
popup.setLayout(R.layout.shortlist_popup);
popup.showAsDropDown(getView().findViewById(R.id.sliding_menu_list));
只需在列表底部创建弹出窗口,无论列表位于何处。
答案 0 :(得分:0)
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos, long id) {
// write you logic here
}
});
答案 1 :(得分:0)
首先使用HashMap而不是String [] 请参阅此http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
hashmap.get(position).get(KEY)
}