隐藏的自定义搜索栏如果适配器的所有项目都出现了 [英] Hide custom search bar if all items in adapter are showing

查看:112
本文介绍了隐藏的自定义搜索栏如果适配器的所有项目都出现了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是, listView.getLastVisiblePosition 总是返回-1,所以我不能隐藏搜索查看 。我检查这种权利设置适配器之后的任何地方我曾试图把这个它仍然返回-1。我没有看到<一href="http://developer.android.com/reference/android/widget/AdapterView.html#getLastVisiblePosition%28%29"相对=nofollow>文档为什么会但我想它会返回-1,如果的ListView 未显示任何项目。然而, listView.getFirstVisiblePosition()返回0始终,即使有多个项目展示。

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);    }
}

该方法被调用任何时候一个按钮是pressed或文本发生变化,可以通过过滤列表。所以,问题是为什么会 listView.getLastVisiblePosition()总是返回-1,为什么会 listView.getFirstVisiblePosition()总是返回0?没有错误/例外,一切都正常运行,除了没有得到预期的结果。注: itemsAdapter.getCount()返回正确的值

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.

另外,我要支持 API&GT; = 10

修改

如果有人需要澄清,让我知道。但基本上,我有一个的EditText 我使用该列表中进行搜索。我想隐藏这个时候出现在列表中比适合在屏幕上没有更多的项目。 listView.getLastVisiblePosition()总是返回-1

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.

更新

我把一个断点 onItemClick()在那里我获得正确的值getFirstVisiblePosition() getLastVisiblePosition() listView.getChildCount()。在此之前,我0,-1,分别获得。

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.

推荐答案

您需要做的是大致

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

为什么这种方式,而不是直接?

Android应用程序运行在被称为UI /主线程中的一件大事循环。就是在那里执行的一切是某些事件的结果。例如,当需要创建的是某种形式的活动创建活动的活动。该循环将执行code处理此事件,并会为例,一旦你被认为是创造致电的onCreate 方法。它可能会在同一个迭代中调用多个方法,但是这并不重要。

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.

当你设置的东西,如UI中的任何一种的 onSomething 方法没有什么实际直接绘制。你要做的就是设置一些状态变量像一个新的适配器。一旦你从这些方法返回系统得到返回的控制,将检查什么需要做下一个。

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.

回到你的问题:通过调用 .setAdapter()你基本上是重新设置的ListView 的所有状态。而且,由于的ListView 的实际更新只会发生的的你的手控制权交还给系统你会得到什么有用的出 .getLastVisiblePosition()

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().

需要什么之前发生的是,的ListView 指示重新绘制或测量它的新的大小,计算项目有等的数量。一旦它完成,这将是能够给你所需要的信息。

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.

。员额(Runnable的R)只是入队一个Runnable进入,然后通过循环执行一次,它是第一个在队列中的EventQueue。

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

的Runnable 不需要,它只是一个名为<$ C $的方法的常规对象C>的run()和的的Runnable 是一个简单的事合同(这往往恰好是一个)可以调用的run()方法来执行任何你想要运行。神奇的循环做到这一点。

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.

你的结果发布可运行时看起来客栈伪code有点像这样的:

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()               //    <--+
}

这篇关于隐藏的自定义搜索栏如果适配器的所有项目都出现了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆