Android/ListView : Select first item after loading datas

笑着哭i 提交于 2019-12-06 11:51:40

I found a solution to my problem and I post here the solution if there is a developer that is in the same situation.

I created a method in my adapter that flag wich position must be activated and in my getView method, I just compare the current position to the flag and activate or not the row :

((ListFeedsAdapter) mListView.getAdapter()).setItemSelected(mOldPosition);

And in the adapter :

private int mItemSelected = -1 ;
public void setItemSelected(int position){
mItemSelected=position;
}

public View getView(int position, View convertView, ViewGroup parent){
       //some code ....
    if(mItemSelected==position){
        convertView.setActivated(true);
    }else{
        convertView.setActivated(false);
    }
}

Thank you !

I think it's because you passed null in the performItemClick() method:

mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());

Try something like:

mListView.performItemClick(mListView.getChildAt(0), 0, mListView.getFirstVisiblePosition());

did you tried setSelection()?

mListView.setSelection(0)
Roshan Jha

I know answer is accepted, but if this helps someone.

If you want to select/play the very first view of list view automatically as list loads, then you should visit this link. I was looking for the same thing and found post() method which waits till your list is filled.

Here is my question link :-Accessing list's first item when list view is loaded in android

Here is its solution link :-smoothScrollToPosition after notifyDataSetChanged not working in android

However writing any code in getView() will cost you as getView is called repeatedly. Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!