View.getWidth возвращает 2 разных значения случайным образом - PullRequest
1 голос
/ 22 марта 2012

Я показываю PopupWindow рядом с элементом ListView. Для этого я вызываю метод showPopup в моем listView.setOnItemClickListener().

Идея состоит в том, чтобы показать некоторые подкатегории во всплывающем окне, используя тот же макет строки списка (menu_row.xml).

Моя проблема в том, что иногда view.getHeight() в showPopup (...) возвращает значение (46), а иногда и другое (76) (с тем же устройством), поэтому аспект строк в моем всплывающем окне изменяется, в то время как:

1 - строки в ListView не меняются

2- view.getHeight(), похоже, не меняется

Кто-нибудь может помочь?

listView.setOnItemClickListener (...)

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {

        mAdapter.mSelected = position;
        mAdapter.notifyDataSetChanged();

        if(GlobalVars.menuItemArray[position].children==null){
            // TODO launch activity
        }
        else{

            showPopup(HomeActivity.this, layout_base, v, position);
        }
    }
});

showPopup (...)

public void showPopup(Context context, View parent, View view, int position){

    /*
     * Compute popup position and size
     */

    int[] itemPosition = new int[2];
    view.getLocationOnScreen(itemPosition);

    int item_width = view.getWidth();
    int item_height = view.getHeight();

    int nChildren = GlobalVars.menuItemArray[position].children.size();

    int popup_height = item_height * nChildren; 

    int popup_x = itemPosition[0] + item_width;
    int popup_y = itemPosition[1] + item_height/2 - popup_height/2;

    /*
     * Build the view
     */

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout popupContainer = new LinearLayout(context);
    popupContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    popupContainer.setOrientation(LinearLayout.VERTICAL);

    LayoutParams params;

    params = new LayoutParams(item_width, item_height);
    popupContainer.setOrientation(LinearLayout.VERTICAL); 

    for(int i=0; i<nChildren; i++){

        RelativeLayout menu_row = (RelativeLayout) inflater.inflate(R.layout.menu_row, null);        

        popupContainer.addView(menu_row, params);
    }

    mPopup.setContentView(popupContainer);
    mPopup.setHeight(popup_height);
    mPopup.setWidth(item_width);

    mPopup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, popup_x, popup_y);         

}

menu_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_row_container"
    android:layout_width="250dp"
    android:layout_height="46dp"
    android:background="@drawable/bg_menu_cell_inactive"
    android:paddingRight="4dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="20dp"
        android:singleLine="true"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/num_items"
        android:layout_width="40dp"
        android:layout_height="23dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/bg_badge_cell"
        android:gravity="center"
        android:textColor="#ffffff" />

</RelativeLayout>
...