以编程方式设置ListView高度

时间:2016-11-29 08:35:46

标签: java android listview

我的活动中有TextViewListViewListView最初设置为Visibility = gone我需要像用户点击TextView一样的功能,{ {1}}绑定并将显示给“Wrap_Content”。我到目前为止已编写代码

我的XMl文件

ListView

Java代码

<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" />

ViewAnimationUtils类

 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);
}

在此代码中,我调用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); } 类的Expand函数。在扩展功能中,我将布局高度设置为ViewAnimationUtils,但Listview渲染高度仅显示一个项目。 Wrap_Content有8项绑定。

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

3 个答案:

答案 0 :(得分:16)

这会对你有所帮助。

 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();

答案 1 :(得分:2)

你可以尝试这个函数来更新listView的高度

public static void updateListViewHeight(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        return;
    }
    // get listview height
    int totalHeight = 0;
    int adapterCount = myListAdapter.getCount();
    for (int size = 0; size < adapterCount; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // Change Height of ListView
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = (totalHeight
            + (myListView.getDividerHeight() * (adapterCount));
    myListView.setLayoutParams(params);
}

答案 2 :(得分:1)

如果要在ScrollView上添加ListView并调整ListView的高度,请尝试以下代码

if (adapter != null) {
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
         View listItem = adapter.getView(i, null, listView);
         listItem.measure(0, 0);
         totalHeight += listItem.getMeasuredHeight() + (listItem.getMeasuredHeightAndState()/2);
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();