2014-09-02 68 views
0

有没有方法可以为列表查看设置可查看项目数量?还是我有义务设置listview的高度?在列表视图中设置可查看项目数量

例如,我收到了一个包含9个项目的列表。我只想要其中4人首先出现。然后用户需要滚动查看其他项目。

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

哪里填充物品放入列表视图:HikayeOkuActivity.java是

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); 
+0

有一个默认设置,你可以设置和一个最大尺寸...之后,应该自动启用滚动条 – StackFlowed 2014-09-02 13:16:01

+0

@Aeshang我到底在哪里可以设置这些默认设置? – rentire 2014-09-02 13:28:13

+0

list.setBounds(,,,);在Java SWT ...你在用什么? – StackFlowed 2014-09-02 13:40:45

回答

0

是的,你需要得到listview的高度。在你的onCreate你做这样的事情:

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

在适配器的getView方法,那么你使用height被一分为四组各列表项的高度。

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

现在,这应该最好用ViewHolder来完成,但这不是主题。

+0

似乎没有我想要的简短版本。谢谢回复。 – rentire 2014-09-02 14:51:25

0

基本上,每个视图的高度必须是ListView高度的1/4。如果你想支持所有的设备分辨率,你必须计算它。如果您需要更多的信息,请添加评论,我会详细解释它。

重要的是,您使用什么样的适配器为您的ListView?

+0

你能详细解释一下吗?我为我的列表视图使用ArrayAdapter 。 – rentire 2014-09-02 13:27:32

+0

请在你的问题中显示你已有的代码(适配器) – 2014-09-02 13:33:46

+0

有些人建议通过用类似“return 4;”的方法重写getCount()方法来获取适配器的计数。 。但这实际上没有意义。 – rentire 2014-09-02 13:42:13

0

纠正我,如果我错了,但..

你也可以使用一个安卓WEIGHTSUM安卓重量参数在XML布局。为子视图(ListView项目的布局)设置android:weightsum = 1用于父视图(您的ListView)和android:weigth = 0.25。在这种情况下,您不必计算可见视图的高度。

+0

Weightsum是一个LinearLayout参数,而不是一个ListView参数。 – Qw4z1 2014-09-02 14:44:52

相关问题