SwipeRefreshLayout隐藏最后一个ListView项的一部分

时间:2018-04-13 21:05:29

标签: android swiperefreshlayout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.partyspottr.appdir.ui.mainfragments.eventchildfragments.alle_eventer_fragment">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_layout_events"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="@dimen/_8sdp"
    android:layout_marginStart="@dimen/_8sdp"
    android:layout_marginTop="@dimen/_5sdp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/search_alle_eventer"
    app:layout_constraintVertical_bias="0.0">

    <ListView
        android:id="@+id/lvalle_eventer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/transparent"
        android:dividerHeight="@dimen/_20sdp"
        android:scrollbarSize="@dimen/_3sdp"
        android:scrollbarThumbVertical="@color/lightred"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.v4.widget.SwipeRefreshLayout>

<EditText
    android:id="@+id/search_alle_eventer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="@dimen/_8sdp"
    android:layout_marginStart="@dimen/_8sdp"
    android:layout_marginTop="@dimen/_90sdp"
    android:background="@drawable/border_rounded"
    android:hint="Search.."
    android:inputType="text"
    android:paddingEnd="@dimen/_10sdp"
    android:paddingStart="@dimen/_10sdp"
    android:textColor="@android:color/black"
    android:textSize="@dimen/_15ssp"
    app:layout_constraintEnd_toEndOf="@+id/swipe_layout_events"
    app:layout_constraintStart_toStartOf="@+id/swipe_layout_events"
    app:layout_constraintTop_toTopOf="parent" />

这是我的布局XML,包含父级(ConstraintLayout),带有ListView作为子级的SwipeRefreshLayout,以及用于搜索ListView内部项目的EditText。

这个布局用作片段,所以我在这个布局之外有一个按钮,当我按下这个按钮时,EditText显示或不显示,我用这个代码执行此操作:

search_alle_eventer.setVisibility(search_alle_eventer.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
                ViewGroup.LayoutParams params = search_alle_eventer.getLayoutParams();

                if(search_alle_eventer.getVisibility() == View.INVISIBLE)
                    params.height = 0;
                else {
                    params.height = WRAP_CONTENT;

                    search_alle_eventer.setLayoutParams(params);

现在,我遇到的问题是当EditText显示(WRAP_CONTENT)时,当我滚动到ListView的底部时,最后一项被切断。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以设置从底部到ListView的边距。 android:marginBottom = "50dp"

或者试试像这样的相对布局

< RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.partyspottr.appdir.ui.mainfragments.eventchildfragments.alle_eventer_fragment">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout_events"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="@dimen/_5sdp"
>

<ListView
    android:id="@+id/lvalle_eventer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@color/transparent"
    android:dividerHeight="@dimen/_20sdp"
    android:scrollbarSize="@dimen/_3sdp"
    android:scrollbarThumbVertical="@color/lightred"
     />
    </android.support.v4.widget.SwipeRefreshLayout>

   <EditText
android:id="@+id/search_alle_eventer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginStart="@dimen/_8sdp"
android:layout_marginTop="@dimen/_90sdp"
android:background="@drawable/border_rounded"
android:hint="Search.."
android:inputType="text"
android:paddingEnd="@dimen/_10sdp"
android:paddingStart="@dimen/_10sdp"
android:textColor="@android:color/black"
android:textSize="@dimen/_15ssp"
 />
</RelativeLayout>

答案 1 :(得分:0)

好的,谢谢你的帮助,但我的情况是一个特殊的需求案例,无论我在做什么,我肯定不是正确的方法,但我会让它成为。

通过在我提到的按钮的OnClickListener中添加更多代码来修复它:

new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if(search_alle_eventer.getVisibility() == View.INVISIBLE)
                            listView.setPadding(0, 0, 0, 0);
                        else
                            listView.setPadding(0, 0, 0, search_alle_eventer.getHeight());
                    }
                }, 100);

只要EditText可见,就在ListView上添加一些填充对我来说是个窍门。

编辑:谢谢Mark Keen指出碎片的容器,我没有直接思考。我通过调整容器大小来修复它。 对,这个问题很抱歉。

相关问题