I have a ListActivity that implements onListItemClick() and calls a doSomething() function of the class. The latter contains l.setSelection(position) where l is the ListView object.
我有一個ListActivity實現onListItemClick()並調用該類的doSomething()函數。后者包含l.setSelection(position),其中l是ListView對象。
Now there is a onClickListener() listening for a button click that perfoms some actions and that too calls doSomething().
現在有一個onClickListener()監聽按鈕單擊,它執行一些操作,並且也調用doSomething()。
In the first case, the selected item get positioned appropriately, but in the latter, nothing happens.
在第一種情況下,所選項目適當定位,但在后者中,沒有任何反應。
Any clues about this strange behaviour and how I might make it work?
有關這種奇怪行為的任何線索以及我如何使其發揮作用?
16 个解决方案
#1
101
maybe you need to use function:
也許你需要使用功能:
ListView.setItemChecked(int position, boolean checked);
#2
64
use requestFocusFromTouch() before calling setSelection() method
在調用setSelection()方法之前使用requestFocusFromTouch()
#3
30
I know this is an old question but I just had a similar problem that I solved in this way:
我知道這是一個老問題,但我遇到了類似的問題,我以這種方式解決了這個問題:
mListView.clearFocus();
mListView.post(new Runnable() {
@Override
public void run() {
mListView.setSelection(index);
}
});
#4
14
You might need to wrap setSelection() in a posted Runnable (reference).
您可能需要在已發布的Runnable(引用)中包裝setSelection()。
#5
13
setSelection() does not necessarily have visual impact. The selection bar only appears if you use the D-pad/trackball to navigate the list. If you tap on the screen to click something, the selection bar appears briefly and vanishes.
setSelection()不一定具有視覺效果。僅當您使用D-pad /軌跡球導航列表時,才會顯示選擇欄。如果您點擊屏幕上點擊某些內容,選擇欄會短暫顯示並消失。
Hence, setSelection() will only have a visual impact if the activity is not in touch mode (i.e., the last thing the user did was use the D-pad/trackball).
因此,如果活動未處於觸摸模式,則setSelection()將僅具有視覺效果(即,用戶做的最后一件事是使用D-pad /軌跡球)。
I am not 100% certain this explains your phenomenon given the description you provided, but I figured it is worth a shot...
鑒於您提供的描述,我不是100%肯定這會解釋您的現象,但我認為值得一試......
#6
5
If you use an Adapter for your ListView add this code to your adapter:
如果您為ListView使用適配器,請將此代碼添加到適配器:
public class MyAdapter extends
ArrayAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflator.inflate(R.layout.my_adapter, null);
} else {
rowView = (View) convertView;
}
//...
// set selected item
LinearLayout ActiveItem = (LinearLayout) rowView;
if (position == selectedItem)
{
ActiveItem
.setBackgroundResource(R.drawable.background_dark_blue);
// for focus on it
int top = (ActiveItem == null) ? 0 : ActiveItem.getTop();
((ListView) parent).setSelectionFromTop(position, top);
}
else
{
ActiveItem
.setBackgroundResource(R.drawable.border02);
}
}
private int selectedItem;
public void setSelectedItem(int position) {
selectedItem = position;
}
}
In your Activity:
在您的活動中:
myAdapter.setSelectedItem(1);
#7
4
I have an very large Request with Webcontent. When I used the code in onCreateView the Listview wasnt even finished loading. I put it in onPostExecute of my AsyncTask.
我有一個非常大的WebContent請求。當我在onCreateView中使用代碼時,Listview甚至沒有完成加載。我把它放在我的AsyncTask的onPostExecute中。
//Get last position in listview
if (listView != null && scrollPosition != 0) {
listView.clearFocus();
listView.requestFocusFromTouch();
listView.post(new Runnable() {
@Override
public void run() {
listView.setItemChecked(scrollPosition, true);
listView.setSelection(scrollPosition);
}
});
}
Dont forget to set the item checked in on Click ;)
別忘了在Click上設置檢查項目;)
#8
2
Maybe you should use the smoothScrollToPosition(int position) method of ListView
也許你應該使用ListView的smoothScrollToPosition(int position)方法
#9
2
For me calling
對我來說
listView.notifyDataSetChanged();
listView.requestFocusFromTouch();
and then
接着
listView.setSelection(position);
solved the issue.
解決了這個問題。
if you do that in a runnable it works without calling requestFocusFromTouch(), but the old position of the ListView is showen for a sekound.
如果你在runnable中這樣做,它可以在不調用requestFocusFromTouch()的情況下工作,但是ListView的舊位置是為sekound顯示的。
#10
0
In my case smoothScrollToPosition(int position) worked, can you also tell me how to set that scrolled position into center of the list. It appeared at the bottom of visible items.
在我的情況下,smoothScrollToPosition(int position)工作,你還可以告訴我如何將滾動位置設置到列表的中心。它出現在可見項目的底部。
#11
0
For me it helped to set
ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); or ListView.CHOICE_MODE_MULTIPLE
then
ListView.setSelection(position) or ListView.setItemChecked(position, true);
works fine
對我來說,它有助於設置ListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);或ListView.CHOICE_MODE_MULTIPLE然后ListView.setSelection(position)或ListView.setItemChecked(position,true);工作正常
#12
0
Found a solution in my case. I am not using a Runnable since my class is extending ListFragment. What I had to do is make my index a final; final index = 5; mListView.setSelection(index);
在我的案例中找到了解決方案。我沒有使用Runnable,因為我的類正在擴展ListFragment。我必須做的是讓我的索引成為最終的;最終指數= 5; mListView.setSelection(索引);
#13
0
I found that sometimes setSelection will not work because I set attribute "android:height" of listView to "wrap_content".
我發現有時setSelection不起作用,因為我將listView的屬性“android:height”設置為“wrap_content”。
And the times my App won't work is that when listView become scrollable from non-scrollable.
而我的應用程序無法工作的時間是當listView變得可滾動而不可滾動時。
For example, if my app is "File Browser App". When my list is less than, let's say 6, then it's non-scrollable. Now I return to the parent directory, and it has 11 objects, and I want to set selection to some position, and it won't work here.
例如,如果我的應用程序是“文件瀏覽器應用程序”。當我的列表小於時,讓我們說6,那么它是不可滾動的。現在我返回到父目錄,它有11個對象,我想將選擇設置到某個位置,它在這里不起作用。
to\from | Scrollable | non-Scrollable
Scrollable | O | O( of course )
可滾動| O | O(當然)
non-Scrollable | X | O( of course )
不可滾動| X | O(當然)
I don't want to use post(Runnable), because there will has delay.
我不想使用post(Runnable),因為會有延遲。
==================================
==================================
Answer:
回答:
You can try to set "android:height" to "match_parent"
您可以嘗試將“android:height”設置為“match_parent”
God, it spends three days.
上帝,它花了三天時間。
#14
0
When use post to setSelection(), the ListView will see first , then scroll to the position , thank to "魏經軒", then layout actually will effect the setSelection(), because setSelection() call the setSelectionFromTop(int position, int y), there is another way to solve it.
當使用post到setSelection()時,ListView會首先看到,然后滾動到該位置,感謝“魏經軒”,然后布局實際上會影響setSelection(),因為setSelection()調用setSelectionFromTop(int position,int y) ,還有另一種解決方法。
listView.setAdapter(listView.getAdapter());
listView.setSelection(123);
#15
0
Simply try this code
只需嘗試此代碼即可
listView.setAdapter(adapter);
listView.setSelection(position);
adapter.notifyDataSetChanged();
#16
-1
For me the solution to this problem was:
對我來說,這個問題的解決方案是:
listView.clearChoices();