2011-11-11 64 views
1

我有一個列表視圖,其中我正在加載10個項目。如何使列表視圖只顯示3個項目

我只需要滾動顯示3個項目。

我不想通過調整列表高度來做到這一點,即使我滾動很少(意味着沒有項目應該部分顯示),我也只想顯示3個項目。

如何實現這個...?

在此先感謝..!

+1

檢查此:http://stackoverflow.com/questions/6573489/android-endless-scrolling-listview-and-cursor/6759834#6759834希望這會解決您的問題。 – Ian

+0

@ Amy88將此評論作爲答覆...! – Noby

+0

當我嘗試添加這個答案時,它會自動轉換爲註釋,顯示**「將簡單答案轉換爲註釋」**。 Anway感謝Noby。很高興知道我可以幫助你。 – Ian

回答

2

我會發佈一個代碼,我曾用它在滾動事件中填充10條記錄。

/** 
    * Called when the activity is first created. 
    * 
    * @param savedInstanceState 
    *   the saved instance state 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.view_checkin_checkout_history); 

      Thread thread = new Thread() { 

      public void run() { 

         synchronized (this) { 
        fetchHistory(0); 

        handler.post(new Runnable() { 
         public void run() { 
          pd.dismiss(); 

          displayUI(); 
         }; 
        }); 
       } 
      } 
     }; 

     thread.start(); 
     } 

     /** 
    * Display the check in check out history list. 
    */ 
    private void displayUI() { 
     if ((checkInCheckOutHistoryList != null) 
       && (checkInCheckOutHistoryList.size() > 0)) { 
      historyArrayList = new ArrayList<HashMap<String, String>>(); 

      histroyListAdapter = new SimpleAdapter(
        ViewCheckInCheckOutHistory.this, historyArrayList, 
        R.layout.multi_colummn_list_text_style_small, new String[] { 
          "assetTag", "gif" , "action", "actionTime"}, 
        new int[] { R.id.list_content_column1, 
          R.id.list_content_imagecolumn, 
          R.id.list_content_column3, 
          R.id.list_content_column4}); 

      // To add more items to list view on scroll event. 
      historyListView.setOnScrollListener(new OnScrollListener() { 

       @Override 
       public void onScrollStateChanged(AbsListView view, 
         int scrollState) { 
       } 

       @Override 
       public void onScroll(AbsListView view, int firstVisibleItem, 
         int visibleItemCount, int totalItemCount) { 

        int lastInScreen = firstVisibleItem + visibleItemCount; 

        if ((lastInScreen == totalItemCount) && !(loadingMore) && (lastInScreen < totalHistoryItemCount)) { 

         if (!firstInstance) { 
          openSlider();       
         }       

         fetchHistory(lastInScreen);  

         Thread thread = new Thread(null, loadMoreListItems); 
         thread.start(); 
        } 
       } 
      }); 
        } 
        } 

// Runnable to load the items 

    private Runnable loadMoreListItems = new Runnable() { 

     @Override 
     synchronized public void run() { 
      // Set flag so we cant load new items 2 at the same time 
      loadingMore = true; 

      HashMap<String, String> historyObjectMap; 

      for (CheckInCheckOutHistory checkOutHistoryObj : checkInCheckOutHistoryList) { 
       historyObjectMap = new HashMap<String, String>(); 
       historyObjectMap.put("assetTag", 
         checkOutHistoryObj.getAssetTag()); 
       historyObjectMap.put("action", checkOutHistoryObj.getAction()); 
       historyObjectMap.put("actionTime", 
         checkOutHistoryObj.getActionDate()); 

       if (checkOutHistoryObj.getAction().equals("Checked out")) { 
        historyObjectMap.put("gif", R.drawable.radio_button_yellow 
          + ""); 
       } else { 
        historyObjectMap.put("gif", R.drawable.radio_button_green 
          + ""); 
       } 

       historyArrayList.add(historyObjectMap); 
      } 

      runOnUiThread(returnRes); 
     } 
    }; 

    // Since we cant update our UI from a thread this Runnable takes care of 
    // that! 
    private Runnable returnRes = new Runnable() { 
     @Override 
     public void run() { 

      // Add the new items to the adapter 
      if (historyArrayList != null && historyArrayList.size() > 0) { 
       histroyListAdapter.notifyDataSetChanged(); 
      } 

      if (firstInstance) { 
       historyListView.setAdapter(histroyListAdapter); 
       firstInstance = false; 
      } 

      historyListLayout.setVisibility(View.VISIBLE); 

      // Done loading more. 
      loadingMore = false; 

      if ((slidingDrawer.isOpened()) && (!loadingMore)) { 

       handler.postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         slidingDrawer.close(); 
         slidingDrawer.setVisibility(View.GONE); 
        } 
       }, 1000); 

      } 
     } 
    }; 


fetchHistory(int count)是我用於爲totalHistoryItemCountcheckInCheckOutHistoryList &設置的值的方法。

希望這會有所幫助。

0

除了這3個項目使所有其他項目在滾動時不可見。在再次滾動後可見。

+0

http://developer.android.com/reference/android/view/View.html#GONE獲取更多信息。 – Pedantic

相關問題