以编程方式设置ListView的高度 [英] Set ListView Height Programmatically

查看:55
本文介绍了以编程方式设置ListView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有一个TextViewListView.ListView最初设置为Visibility = gone我需要一个功能,例如用户单击TextViewListView绑定并显示为"Wrap_Content" .到目前为止,我已经编写了代码

I have a TextView and ListView in my activity.ListView intially set to Visibility = gone I need a functionality like if user click on TextView, ListView binds and will show to `Wrap_Content'. I have write the code so far

我的XMl文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtDate"
    android:textSize="50dp"
    android:background="@color/colorSplash"
    android:text="20-11-2016"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lstDesc"
    android:visibility="gone" />

Java代码

 private void BindBookedList(){
  final ArrayList<String> arr = new ArrayList<>();
    arr.add("21-11-2016");
    arr.add("22-11-2016");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_collapse, arr){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_collapse, null);
            }
            final TextView txtDate = (TextView) v.findViewById(R.id.txtDate);
            final ListView lstDesc = (ListView) v.findViewById(R.id.lstDesc);
            String strItemDAta = arr.get(position);
            txtDate.setText(strItemDAta);
            String[] arrContries =  getResources().getStringArray(R.array.ListOfAirport);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>
                    (getApplicationContext(), R.layout.spinner_item, arrContries);
            lstDesc.setAdapter(adapter);
            txtDate.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {

                    com.kommlabs.pikmybox.MyUtlilites.ViewAnimationUtils.expand(lstDesc);
                }
            });
            return v;
        }
    };
    ListView listBooked = ((ListView) findViewById(R.id.lstBookedPackages));
    listBooked.setAdapter(adapter);
}

ViewAnimationUtils类

public class ViewAnimationUtils {
static View view;
public static void expand(View v) {
    view = v;
    v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int targtetHeight = v.getMeasuredHeight();
    v.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
    v.setVisibility(View.VISIBLE);

}

在此代码中,我正在调用ViewAnimationUtils类的Expand函数.在扩展功能中,我将布局高度设置为Wrap_Content,但是Listview渲染高度最多仅显示一项. Listview绑定有8个项目.

In this code, i am calling Expand function of ViewAnimationUtils class. In expand function i have set layout height to Wrap_Content but Listview render height upto show only one item. Listview has 8 items on bind.

如何设置ListView的高度以显示列表中的所有项目?

How to set height of ListView to show all items in list ?

推荐答案

这将为您提供帮助.

 ListAdapter listadp = listview.getAdapter();
       if (listadp != null) {
           int totalHeight = 0;
           for (int i = 0; i < listadp.getCount(); i++) {
               View listItem = listadp.getView(i, null, listview);
               listItem.measure(0, 0);
               totalHeight += listItem.getMeasuredHeight();
           }
           ViewGroup.LayoutParams params = listview.getLayoutParams();
           params.height = totalHeight + (listview.getDividerHeight() * (listadp.getCount() - 1));
           listview.setLayoutParams(params);
           listview.requestLayout();

这篇关于以编程方式设置ListView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆