android-如何使列表视图仅显示3个项目

我有一个列表视图,其中正在加载10个项目.

滚动时只希望显示3个项目.

我不想通过调整列表高度来做到这一点,并且即使滚动几下,我也只想显示3个项目(意味着任何项目都不应部分出现).

如何实现这一目标?

提前致谢..!

解决方法:

我将发布一个代码,该代码用于在滚动事件中分别填充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)是我用来设置totalHistoryItemCount&的值的方法. checkInCheckOutHistoryList.

希望这会有所帮助.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...