QuickReturn 库 - 如何防止在向上滚动时在目标视图下显示空白区域

QuickReturn library - How do you prevent blank area that displays under target view as you scroll up

我正在使用 QuickReturn,向上滚动时,目标视图后面有一个空白区域。

有没有办法防止目标视图上出现空白区域(当您向上滚动时),或者这是一个现有问题(我没有在问题列表中看到它,但我已经将其提交为错误)?

我试过将目标视图的高度设置为不仅是 0,还有其他 +/- 值。我也试过把它关掉;例如:

qrAttacher.addTargetView(testTextView, QuickReturnTargetView.POSITION_TOP, -1); qrAttacher.addTargetView(testTextView, QuickReturnTargetView.POSITION_TOP, 0); qrAttacher.addTargetView(testTextView, QuickReturnTargetView.POSITION_TOP, 50); qrAttacher.addTargetView(testTextView, QuickReturnTargetView.POSITION_TOP);

...但问题仍然存在。

这里 a video 展示了这个问题。

这是 a video 它应该做什么。

如果视频有问题,这里有一系列图片演示向上滚动时发生的情况:

这是我的代码:

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/testTextView"
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:paddingLeft="10dp"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@android:color/white"
        android:background="@android:color/black"
        android:textStyle="bold"
        android:text="Test Title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_light"/>

</LinearLayout>

act_student_menu_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/studentMenuContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="8dp"
        android:layout_centerVertical="true"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/studentPhotoIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:src="@drawable/icon_photo_grey" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/studentPhotoIcon"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/studentNameText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:textColor="@android:color/white"
                android:textStyle="bold"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/studentPointsText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>


    </RelativeLayout>

</RelativeLayout>

onCreate 在 MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // The list view to show/hide on scroll.
    ListView listView = (ListView) findViewById(android.R.id.list);
    TextView testTextView = (TextView) findViewById(R.id.testTextView);
    TestAdapter testAdapter = new TestAdapter(this, android.R.layout.simple_spinner_item,
            getListData());
    // Wrap the adapter with QuickReturnAdapter.
    listView.setAdapter(new QuickReturnAdapter(testAdapter));
    // Attach QuickReturn Attacher, which takes care of all of the hide/show functionality.
    QuickReturnAttacher qrAttacher = QuickReturnAttacher.forView(listView);
    /**
     * Add a quick return targetView to the attacher.
     * You can pass a position argument (POSITION_TOP or POSITION_BOTTOM). You can also optionally pass the size of
     * the target view, which will be used to offset the list height, preventing it from hiding content behind the
     * target view.
     */
    qrAttacher.addTargetView(testTextView, QuickReturnTargetView.POSITION_TOP, 0);
}

TestAdapter.java

public class TestAdapter extends ArrayAdapter<StudentMenuItem> {
    private final Context currentContext;
    private List<StudentMenuItem> studentMenuItems;
    ImageView studentPhotoIcon;
    TextView studentNameText;

    public TestAdapter(Context context, int layoutResourceId, List<StudentMenuItem> studentMenuItems) {
        super(context, layoutResourceId, studentMenuItems);
        currentContext = context;
        this.studentMenuItems = studentMenuItems;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View studentMenuOption = convertView;
        if (studentMenuOption == null) {
            LayoutInflater inflater =  (LayoutInflater) currentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            studentMenuOption = inflater.inflate(R.layout.act_student_menu_item, parent, false);
        }
        StudentMenuItem current = studentMenuItems.get(position);
        studentNameText = (TextView) studentMenuOption.findViewById(R.id.studentNameText);
        studentNameText.setText(current.getStudentName());
        TextView studentPointsText = (TextView) studentMenuOption.findViewById(R.id.studentPointsText);
        studentPointsText.setText(current.getStudentPoints());
        studentPhotoIcon = (ImageView) studentMenuOption.findViewById(R.id.studentPhotoIcon);
        return studentMenuOption;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getDropDownView(position, convertView, parent);
    }
}

注意: getListData() 只需 returns List<StudentMenuItem> 即可填充列表。

请帮忙。希望有一个简单的解决方法。谢谢。

您应该将目标视图(测试标题TextView)定位在ListView之后

(由于对此 post 的回复只是部分解决方案,我正在 post 提供完整解决方案的答案。)

在activity_main.xml中:

  • 将 TextView 移到 ListView 下方
  • 在 ListView
  • 上设置 android:layout_gravity="bottom"
  • 将 LinearLayout 更改为 FrameLayout