2016-04-01 21 views
0

我有具有與多個元素來填充的列表視圖。最初,我想顯示前兩個元素,並隨後在用戶開始滾動時顯示其他元素。顯示在列表視圖2個初始項目,然後顯示所有項目

,我有代碼如下:

ListView in XML

<LinearLayout 
     android:id="@+id/layout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="vertical"> 

     <ListView 
      android:id="@+id/options" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

    </LinearLayout> 

ListView Item Definition

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="52dp" 
    android:gravity="center_vertical" 
    android:background="@color/white"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="8dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="16dp" 
     android:inputType="textMultiLine" 
     android:lines="2" /> 

</RelativeLayout> 

適配器:

String[] mobileArray = {"Android", "iPhone","Windows", "Mac", "Moto", "Oneplus", "Random A", "Random B", "Random C", "Random D"}; 

ListView listView = (ListView) getActivity().findViewById(R.id.options); 

LinearLayout responseLayout = (LinearLayout) getActivity().findViewById(R.id.layout); 

ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.item, R.id.text,mobileArray); 

listView.setAdapter(adapter); 

listView.setVisibility(View.VISIBLE); 

responseLayout.setVisibility(View.VISIBLE); 

在這個例子中,我想AndroidiPhone初始可見的,然後用戶應該能夠滾動到其他選項。

目前所有的選項都是可見的,我要去哪裏錯了?

+0

1.你只有四個項目,我相信你永遠不會滾動條上滾動的用戶界面。2.您可以通過設置滾動收聽做到這一點,當你到達終點,然後兩個項目添加到陣列,並呼籲'notifyDatasetChanged();' – Bharatesh

+0

@bharat我可以添加更多的項目到適配器,假設有10個項目,那麼我該怎麼做? –

+0

然後按照這個...你可以通過設置滾動監聽器到列表視圖,當你到達結束然後再添加兩個項目數組,並調用notifyDatasetChanged(); ...這就像分頁時,你到達列表視圖添加結束更多的項目。 – Bharatesh

回答

0

我沒有嘗試,但如果你做什麼自定義適配器和getView()將返回null,當你不希望的項目是可見的?

0

可能是你需要動態分配表單元格高度。爲了僅顯示前兩項,每個單元高度應該是屏幕尺寸的一半。

Point size = new Point(); 
    Display display=getWindowManager().getDefaultDisplay(); 
    int screenWidth; 
    int screenHeight; 
    if(Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB_MR2) 
    { 
     screenWidth=display.getWidth(); 
     screenHeight=display.getHeight(); 
    } 
    else 
    { 
     display.getSize(size); 
     screenWidth = size.x; 
     screenHeight=size.y; 
    } 
int halfScreenHeight = (int)(screenHeight*0.5); 

您需要爲列表項目relativelayout設置halfscreenHeight。

0

只需創建您自己的自定義適配器並覆蓋getCount方法,以便在必要時返回2。然後,當你需要證明你應該設置標誌,通知您有更多的項目和呼叫adapter.notifydatasetchanged

像這樣的事情後事項:

class CustomAdapter extends BaseAdapter { 
    private List elements; 
    private boolean showAll; 
    private int resId; 

    public CustomAdapter(List elements, boolean showAll, int resId) { 
     this.elements = elements; 
     this.showAll = showAll; 
     this.resId = resId; 
    } 

    @Override 
    public int getCount() { 
     if (elements == null) { 
      return 0; 
     } else if (elements.size() < 2 || showAll) { 
      return elements.size(); 
     } else { 
      return 2; 
     } 
    } 

    public void setShowAll(boolean showAll) { 
     this.showAll = showAll; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return elements.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //Todo inflate your element 
     return null; 
    } 
} 
+0

你可以添加一些代碼告訴我該怎麼做? –