两个TextView之间的ListView

时间:2018-02-15 12:14:18

标签: android android-layout listview

我有两个TextView和一个ListView。我想将ListView放在两个TextView之间。现在,当ListView大小增加(我动态添加项目)时,当TextView下方触及底部并变为可滚动时,它应该停止增长。

第二个TextView应始终直接位于ListView下。

我几乎尝试了所有我想到的但无法成功的事情。

任何人都可以帮助我吗?

这是我的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.lifo.skipandgo.activities.ShoppingBasketActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:longClickable="false"
                app:logo="@drawable/ic_shopping_cart"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name" />

        </android.support.design.widget.AppBarLayout>

        <TextView
            android:id="@+id/storeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout"
            android:text="StoreName" />


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/storeName">

            <ListView
                android:id="@+id/basketItemListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@color/colorPrimaryDark"
                android:dividerHeight="1px">
            </ListView>

            <TextView
                android:id="@+id/totalPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:text="Gesamtpreis" />

        </LinearLayout>


    </RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

4 个答案:

答案 0 :(得分:0)

保持你的xml原样..只需在你的活动中使用这个功能..

public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
    @Override
    public void run() {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        int listWidth = listView.getMeasuredWidth();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(
                    View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


            totalHeight += listItem.getMeasuredHeight();
            Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
        }

        Log.d("totalHeight " + totalHeight, "********");

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
        listView.setLayoutParams(params);
        listView.requestLayout();

    }
});
}

答案 1 :(得分:0)

I have hack for it, you could use two buttons, one will be below the ListView and one will stick to bottom of Parent, but make one of them visible at a time.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.lifo.skipandgo.activities.ShoppingBasketActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:longClickable="false"
                app:logo="@drawable/ic_shopping_cart"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name" />

        </android.support.design.widget.AppBarLayout>

        <TextView
            android:id="@+id/storeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout"
            android:text="StoreName" />


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/storeName">

            <ListView
                android:id="@+id/basketItemListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@color/colorPrimaryDark"
                android:dividerHeight="1px">
            </ListView>

            <TextView
                android:id="@+id/totalPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:text="Gesamtpreis" />

        </LinearLayout>

               <TextView
                android:id="@+id/totalPrice1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="5dp"
                android:alignParentBottom="true"
                android:text="Gesamtpreis" />
    </RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

Then whenever you add items in list dynamically, after adding items call below function to check whether your RecyclerView is scrollable or not i.e Scrollbars are visible or not. If this method returns true then make visibility of totalPrice as GONE and make visibility of totalPrice1 as VISIBLE and vice versa when you delete items from list.

public boolean isRecyclerScrollable() {
  LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
  RecyclerView.Adapter adapter = recyclerView.getAdapter();
  if (layoutManager == null || adapter == null) return false;

  return layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
}

答案 2 :(得分:0)

you just need to give android:weightSum="2" in your parent layout of listview and android:layout_weight="1" to your child elements like this below

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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" >

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:longClickable="false"
                app:logo="@drawable/ic_noti"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name" />

        </android.support.design.widget.AppBarLayout>

        <TextView
            android:id="@+id/storeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout"
            android:text="StoreName" />


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:layout_below="@+id/storeName">

            <ListView
                android:id="@+id/basketItemListView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:divider="@color/colorPrimaryDark"
                android:dividerHeight="1px">
            </ListView>

            <TextView
                android:id="@+id/totalPrice"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="5dp"
                android:layout_weight="1"
                android:text="Gesamtpreis" />

        </LinearLayout>


    </RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

output..

enter image description here

答案 3 :(得分:0)

我自己就找到了解决方案。谢谢大家试图帮助我!

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.lifo.skipandgo.activities.ShoppingBasketActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:longClickable="false"
                app:logo="@drawable/ic_shopping_cart"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@string/app_name" />

        </android.support.design.widget.AppBarLayout>

        <TextView
            android:id="@+id/storeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appBarLayout"
            android:text="StoreName" />


        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/storeName">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ListView
                    android:id="@+id/basketItemListView"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:divider="@color/colorPrimaryDark"
                    android:dividerHeight="1px">
                </ListView>

                <TextView
                    android:id="@+id/totalPrice"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginStart="5dp"
                    android:text="Gesamtpreis" />
            </LinearLayout>


            <View
                android:layout_width="1dp"
                android:layout_height="0dp"
                android:layout_weight="1" />


        </LinearLayout>


    </RelativeLayout>


</android.support.design.widget.CoordinatorLayout>