listview动态项目高度

时间:2017-02-10 06:29:51

标签: android listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="100"
    android:background="@drawable/round_shape"
    android:descendantFocusability="blocksDescendants"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="25">
    <TextView
        android:text="Title"
        android:paddingStart="20dp"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/borrowBookTitle"
        android:maxLines="1"
        android:textSize="25sp"
        android:textColor="#000000"
        />
    <TextView
        android:text="Locate"
        android:paddingStart="20dp"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/locate"
        android:maxLines="2"
        android:textSize="15sp"
        android:textColor="#838383"
        />
    <TextView
        android:text="borrowStartDate"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="8dp"
        android:paddingStart="10dp"
        android:paddingEnd="20dp"
        android:paddingBottom="5dp"
        android:layout_width="wrap_content"`enter code here`
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:id="@+id/borrowStartDate"
        android:maxLines="1"
        android:textSize="17sp"
        android:textColor="#000000"
        android:background="@color/childview_background"
    />
    <TextView
        android:text="borrowEndDate"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:paddingStart="10dp"
        android:paddingEnd="20dp"
        android:paddingBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/borrowEndDate"
        android:visibility="gone"
        android:maxLines="1"
        android:textSize="17sp"
        android:textColor="#000000"
        android:background="@color/childview_background"
        />
</LinearLayout>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="75">

    <TextView
        android:text="D-11"
        android:paddingStart="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/borrowDday"
        android:textSize="35sp"
        android:textColor="#000000"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/button1"
        android:id="@+id/renewBtn"
        android:textSize="20sp"
        android:textColor="#000000"
        android:visibility="gone"
        android:text="RENEW"
        />
</LinearLayout>

上面的xml文件是Listview's项xml文件。 borrowStartDateborrowEndDaterenewBtn可见性消失了。 但是当点击项目时,更改为可见

borrowListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                        TextView startDate = (TextView)view.findViewById(R.id.borrowStartDate);
                        TextView endDate = (TextView)view.findViewById(R.id.borrowEndDate);
                        Button renewBtn = (Button)view.findViewById(R.id.renewBtn);
                        if ( startDate.getVisibility() == View.GONE) {
                            startDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
                            endDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
                            renewBtn.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in));

                            startDate.setVisibility(View.VISIBLE);
                            endDate.setVisibility(View.VISIBLE);
                            renewBtn.setVisibility(View.VISIBLE);
                        }
                        else {
                            startDate.setVisibility(View.GONE);
                            endDate.setVisibility(View.GONE);
                            renewBtn.setVisibility(View.GONE);
                        }
                    }
                });

它改变了可见性代码

问题是Listview高度匹配当项目已消失的高度。 所以当我点击每个项目时不直接显示部分。

我想点击项目时,Listview高度自动展开

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
        <WebView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone"
            android:id="@+id/rightWebView"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Refresh"
            android:backgroundTint="@color/button1"
            android:drawableEnd="@drawable/refresh_picto"
            android:id="@+id/refreshBorrowBook"
            android:onClick="refreshBorrow"
            android:layout_marginBottom="10dp"
            />

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/borrowBookList"
            android:dividerHeight="10dp"
            android:divider="@color/main_background"
            />
        <TextView
            android:text="예약 목록"
            android:paddingStart="50dp"
            android:visibility="gone"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="20dp"
            android:textColor="#000000"
            android:textSize="25sp"
            android:background="@drawable/round_shape"
            android:backgroundTint="@color/childview_background"
            android:id="@+id/reservation_list_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/reservationBookList"
            android:dividerHeight="10dp"
            android:divider="@color/main_background"
            />
    </LinearLayout>
</ScrollView>

这是片段代码

添加快照

点击刷新按钮后

after refresh button

单击项目

when click item

如果向下滚动,则显示listview的其他部分

我不想滚动。

只需点击项目,然后listview自动获得高度。

我认为此问题与notifyDataSetChanged();

无关

2 个答案:

答案 0 :(得分:0)

使用

view.getLayoutParams().Height=value

顺便说一句,您不必切换可见性 希望有所帮助

答案 1 :(得分:0)

下面的方法计算每个子视图高度,按Listview中任何子视图的按钮后可以调用以下方法可见和不可见。

RecyclerView

更改ListView项目后立即调用此函数,如:

public class Utility {
        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }

            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }

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