0

Is there a way to set the viewable item count for a listview? or am I obliged to set the height for the listview?

For instance, I got a list that includes 9 items. I only want 4 of them to appear at first. Then user needs to scroll to see other items.

list_anlam.xml

<LinearLayout 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:layout_margin="1dp"
     android:background="@android:color/darker_gray" >

      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/title_popup_hikayeoku" />
      <EditText android:id="@+id/kelime_text"
          android:inputType="text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          />

      <ListView android:id="@+id/list_anlam"
         android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
      <Button
          android:id="@+id/button_sec"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/button_popup_hikayeoku" />

 </LinearLayout>

list_item.xml :

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:textSize="20sp"
    android:padding="10dp" />

Where I fill items into listview : HikayeOkuActivity.java is

String[] anlamlar = null;
                    anlamlar = MainActivity.dbM.ilkAnlamGetir(selectedText);

                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item,anlamlar);
                    anlamList.setAdapter(adapter);
                    popupWindow.showAsDropDown(mainLayout, touchedX,touchedY);
rentire
  • 96
  • 10

3 Answers3

0

Basically, the height of each view must be 1/4 of the height of your ListView. If you want to support all device resolutions, you have to calculate it. If you need more info about that, add a comment and I will explain it in detail.

Important for details, what kind of Adapter are you using for your ListView?

Francesco verheye
  • 1,574
  • 2
  • 14
  • 32
0

Correct me if I'm wrong, but..

you could also use an android:WEIGHTSUM and android:WEIGHT parameters in your xml layouts. Set android:weightsum=1 for parent view (your ListView) and android:weigth=0.25 for child views (layout of ListView items). In this case you don't have to calculate heights of your viewables.

Iriminage
  • 177
  • 2
  • 17
0

Yes, you need to get the height of the listview. In your onCreate you do something like:

listview.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    height = listView.getHeight();
}

In the getView method of your adapter you then use the height divided by four to set the height of each list item.

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = activity.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.item_layout, parent, false);
    rowView.setMinHeight(height/4);
    return rowView;
}

Now this should preferably be done with a ViewHolder, but that's not the topic here.

Qw4z1
  • 3,041
  • 1
  • 24
  • 36