Вопрос

The problem I have is that listView.getLastVisiblePosition always returns -1 so I can't hide the searchView. I check this right after setting the adapter and anywhere I have tried to put this it still returns -1. I didn't see in the Docs why this would be but I imagine it would return -1 if the ListView is not showing any items. However, listView.getFirstVisiblePosition() returns 0 always, even when there is more than one item showing.

I have tried both methods Here but it doesn't make a difference when getting the wrong value.

@SuppressLint("NewApi") private void setFilters(String curType, Object curFilter)
{
    // initialize several lists
    itemsAdapter = new ArrayAdapter<Rowdata>(this, R.layout.list_item_text, foodItems);
    listView.setAdapter(itemsAdapter);

    int numItems = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
    if (numItems > foodItems.length)    
    {   searchField.setVisibility(View.GONE);   }
    else
    {   searchField.setVisibility(View.VISIBLE);    }
}

This method is called any time a Button is pressed or text is changed that can filter through the list. So the question is why would listView.getLastVisiblePosition() always return -1 and why would listView.getFirstVisiblePosition() always return 0? No errors/exceptions, everything runs fine except for not getting the expected results. Note: itemsAdapter.getCount() returns the correct value.

Also, I have to support API >=10

Edit

If anyone needs clarification, let me know. But basically, I have an EditText I use to search through the list. I want to hide this when there aren't more items in the list than what fit on the screen. listView.getLastVisiblePosition() always returns -1

I would really like to know the cause of the original problem but if anyone has any better way of hiding the search box when items all fit on the screen, I am open to suggestions.

Update

I put a breakpoint in onItemClick() and there I get the correct values for getFirstVisiblePosition(), getLastVisiblePosition(), and listView.getChildCount(). Before this, I get 0, -1, and null respectively.

Это было полезно?

Решение

What you need to do is roughly

listview.post(new Runnable() {
    public void run() {
        listview.getLastVisiblePosition();
    }
});

Why this way and not directly?

Android apps run in a big event loop known as the UI / main thread. Everything that is executed in there is the result of some event. For example when your Activity needs to be created that's some sort of Activity creation event. The loop will execute code that handles this event and will for example once your are considered "created" call the onCreate method. It might call more than one method within the same iteration but that's not really important.

When you setup things like the UI in any of those onSomething methods nothing is actually drawn directly. All you do is set some state variables like a new Adapter. Once you return from those on methods the system gains back control and will check what it needs to do next.

The system will for example check if the window needs to be redrawn and if so will enqueue a redraw event in the event queue which is at a later point executed by the loop. If nothing needs to be drawn it's just idle and will wait for example for touch events that are enqueued for that loop as well.

Back to your problem: By calling .setAdapter() you essentially reset all states of the ListView. And since actual updates of the ListView will only happen after you hand control back to the system you will get nothing useful out of .getLastVisiblePosition().

What needs to happen before is that ListView is instructed to be redrawn or to measure it's new size, count the amount of items it has and so on. Once it has done that it will be able to give you the required information.

.post(Runnable r) simply enqueues a Runnable into the eventqueue which is then executed by the loop once it's first in the queue.

a Runnable does not require a Thread, it's just a regular Object with a method named run() and the contract of a Runnable is simply that something (which often happens to be a Thread) can call the run() method to execute whatever you want to run. Magical loop does that.

Result of you posting a runnable is looks inn pseudo code somewhat like this:

void loop() {
    yourActivity.onSomething() { loop.enqueue(runnable) }
    ListView.redraw()            //       |
    runnable.run()               //    <--+
}

Другие советы

My suggestion to resolve this problem will not be professional or light weight.

I am suggesting that you should get count of all views in listView and check every one of them are they visible.

example:

   private int getIndexOfLastVisibleView(ListView view){

      int count = view.getChildCount()-1;

      for(int i = count ; i>=0 ; i--){
         View checkedView = view.getChildAt(i);
         if(checkedView.isShown()){
            return i;
         }
      }


      return -1;
  }

May not be perfect but I hope that it will work.

You can refer to my answer here Strange problem with broadcast receiver in Android not exactly the same but you can get the idea why your code not working.

To make it more clear, when you set the adapter to the ListView, nothing has been drawn yet and the method getLastVisiblePosition() can only return the correct value after the listview finish drawing all of it's visible children and know which one is the last visible one.

So, the most appropriate approach I can suggest here is trigger a callback after the listView finished drawing and we get the correct value then.

The ListView with listener after drawing:

static class MyListView extends ListView {
        private OnDrawCompletedListener mOnDrawCompletedListener;

        public MyListView(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (mOnDrawCompletedListener != null) {
                mOnDrawCompletedListener.onDrawCompleted();
            }
        }

        public void setOnDrawCompletedListener(OnDrawCompletedListener listener) {
            mOnDrawCompletedListener = listener;
        }

        public static interface OnDrawCompletedListener {
            public void onDrawCompleted();
        }
    }

The sample code for getting last visible position

mListView.setAdapter(new EfficientAdapter(this));
//Will get -1 here
Log.e("Question-17953268",
        "getLastVisiblePosition  = "
                + mListView.getLastVisiblePosition());

mListView.setOnDrawCompletedListener(new OnDrawCompletedListener() {

    @Override
    public void onDrawCompleted() {
        //Will get correct value here
        Log.e("Question-17953268",
                "getLastVisiblePosition  = "
                        + mListView.getLastVisiblePosition());
    }

});

Thanks to zapl's answer I was able to get what I needed. I thought I would post the full code in case it helps anyone

listView.post(new Runnable()
{       
    public void run()
    {
        int numItemsVisible = listView.getLastVisiblePosition() - 
                listView.getFirstVisiblePosition();
        if (itemsAdapter.getCount() - 1 > numItemsVisible)
        {   searchField.setVisibility(View.VISIBLE);    }                                   
        else
        {   
            searchField.setVisibility(View.GONE);   
            setFilters("searchtext", "");
        }               
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top