当滚动时,ListPopupWindow会在某些设备上跳转到顶部

时间:2017-12-22 10:24:19

标签: android android-layout

我正在使用ListPopupWindow构建搜索字段。 在某些设备上,ListPopupWindow在滚动时更改其位置。 (它跳到顶部):

滚动前:

enter image description here

滚动后

Jumped to top after scrolling

我在Nexus 6P上进行了测试。 在其他设备上它正常工作,滚动时不会改变它的初始位置。这是在三星Galaxy A5上拍摄的。

Dropdown normal

以下是代码:

The Activiyt:

<?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">

    <EditText

        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter item name"
        android:textSize="26sp"/>
</LinearLayout>

活动的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:padding="6dp"
          android:textSize="50sp"
          android:textStyle="bold"/>

列表项布局:

{{1}}

任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

而不是这样做:

listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);

您必须明确设置listPopupWindow的高度。为此,您需要计算可用高度并找到设置该值的正确时机。

  1. 摆脱上述界限。

  2. 将此添加到您的xml LinearLayout

    android:id="@+id/root"
    
  3. 将此添加到您的MainActivity

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            int popupHeight = findViewById(R.id.root).getHeight() - itemName.getHeight();
            listPopupWindow.setHeight(popupHeight);
        }
    }
    
  4. 在片段中,您不需要onWindowFocusChanged。您只需将Runnable发布到视图:

    即可
    Activity activity;
    EditText itemName;
    ListPopupWindow listPopupWindow;
    LinearLayout linearLayout;
    String[] items = {"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"};
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Activity) {
            activity = (Activity) context;
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        final View view = inflater.inflate(R.layout.fragment_main, container, false);
        linearLayout = view.findViewById(R.id.root);
        itemName = view.findViewById(R.id.item_name);
        listPopupWindow = new ListPopupWindow(activity);
        listPopupWindow.setAdapter(new ArrayAdapter<>(activity, R.layout.list_item, items));
        listPopupWindow.setAnchorView(itemName);
        listPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);
        listPopupWindow.setModal(true);
        listPopupWindow.setOnItemClickListener(this);
        itemName.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                listPopupWindow.show();
            }
        });
        view.post(new Runnable() {
            @Override
            public void run() {
                int popupHeight = linearLayout.getHeight() - itemName.getHeight();
                listPopupWindow.setHeight(popupHeight);
            }
        });
        return view;
    }