how to make a list view to show only 3 items

后端 未结 2 1415
我寻月下人不归
我寻月下人不归 2021-01-24 03:47

I have a list view in which I am loading 10 items.

I want only 3 items visible when I scroll it.

I don\'t want to do it by adjusting the list height, and I want

相关标签:
2条回答
  • 2021-01-24 03:59

    I will post a code which I have used to populated 10 records each on scroll event.

      /**
         * 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) is the method I have used to set the values for totalHistoryItemCount & checkInCheckOutHistoryList.

    Hope this would help.

    0 讨论(0)
  • 2021-01-24 04:17

    Except those 3 items make all others items invisible when scrolling.after scrolling again make visible.

    0 讨论(0)
提交回复
热议问题