2014-09-02 64 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

相關問題