How to show a fixed number of rows in a listView

I want to display only 5 rows in a ListView , and the rest of them should be scrollable . So I tried this

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/txtFilename" android:layout_width="375dp" android:layout_height="wrap_content" android:layout_margin="10dp" /> <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarSize="2dp" android: /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/gray" android:padding="5dp" > <Button android:id="@+id/btnOk" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="OK"/> <Button android:id="@+id/btnCancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="CANCEL"/> </LinearLayout> </LinearLayout> 

and in java code

 public class MainActivity extends Activity { private ListView list1; private String array[] = { "Iphone", "Tutorials", "Gallery", "Android", "item 1", "item 2", "item3", "item 4" }; @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //parentLayout.add(childLayout); setContentView(R.layout.activity_main); list1 = (ListView) findViewById(R.id.ListView01); // By using setAdpater method in listview we an add string array in // list. list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array)); } 

have an idea how to achieve it

+3
source share
2 answers

Here I just change your Layout , just check it out!

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:id="@+id/txtFilename" android:layout_width="375dp" android:layout_height="wrap_content" android:layout_margin="10dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="300dip"> <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarSize="2dp" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/gray" android:padding="5dp" > <Button android:id="@+id/btnOk" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="OK"/> <Button android:id="@+id/btnCancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="CANCEL"/> </LinearLayout> </LinearLayout> 
0
source

I wrote a demo for this Like:

if you want to fix ListView height to 5 elements. then you should set the right side of the ListView height by calculating the height of listItems .

First you need to create a layout for the list item and assign it some fixed height, here the height is added to Tag reason runtime I got layouts as high as 0.

This is a ListItem Layout

  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout" android:layout_width="fill_parent" android:background="@android:color/black" android:tag="40" android:layout_height="40dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XXX" android:textColor="@android:color/darker_gray" android:textSize="30sp" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/text" android:text="ZZZ" android:textColor="@android:color/darker_gray" android:textSize="30sp" /> </RelativeLayout> 

and set the Height ListView:

 list = (ListView) findViewById(R.id.listView1); adapter = new Adapter(MainActivity.this, setter); list.setAdapter(adapter); RelativeLayout.LayoutParams lstViewParams = (LayoutParams) list.getLayoutParams(); View lisItemInflatedView = View.inflate(this, R.layout.item, null); RelativeLayout relLayoutInLstItem = (RelativeLayout) lisItemInflatedView.findViewById(R.id.relativeLayout); final float scale = this.getResources().getDisplayMetrics().density; int pixels = (int) ( Integer.parseInt( relLayoutInLstItem.getTag().toString() ) * scale + 0.5f); int listHeigt = pixels * 5 + 5; lstViewParams.height = listHeigt; 

here is a screenshot ..

enter image description here

Hope this helps.

-1
source

Source: https://habr.com/ru/post/984172/


All Articles