问题
EDIT I must tell you that my ListView is populate by an AsyncTask.
The code below works fine when I do in onPostExecute method :
synchronized (mListView) {
            if(mFeeds==null || mFeeds.size()==0){
                Utils.Log("mFeeds empty");
                _this.setListShown(false);
            }else{
                Utils.Log("mFeeds Full");
                _this.setListShown(true);
                mListView.setAdapter(new ListFeedsAdapter(mActivity,mFeeds));
                mListView.notifyAll();
                NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment) getFragmentManager()
                        .findFragmentById(R.id.feeddetailViewPagerFragment);
                if(fragment!=null){
                    mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());
                }
            }
        }
The item is clicked and my detail view is populate...
I try to change my Fragment to a ListFragment but nothing changed...
EDIT END
For my application, I created an UI (for tablet) with ListView at left screen and a detail view at the right. I would like to automatically select the first item after loading datas, and view the detail.
I am able to do this by calling mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition()); and 
mListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
    // TODO Auto-generated method stub
    NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment)
            getFragmentManager().findFragmentById(R.id.feeddetailViewPagerFragment);
    if(fragment!=null){
        fragment.setFeedDetail(mFeeds, arg2);
    }
});
Now, what I want to do is to highlight the first item like Gmail application for tablet. I use a selector background on each row like :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selected_item_background" android:state_activated="true"/>
    <item android:drawable="@drawable/pressed_item_background" android:state_activated="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/pressed_item_background" android:state_focused="true"/>
    <item android:drawable="@drawable/unselected_item_background"/>
</selector>
When I use mListView.getChildAt(arg2).setActivated(true); in the onItemClick listener, I have a NullPointerException because mListView.getChildAt(arg2) is null, only if I keep the perforItemClick. If I just comment this line, and click on a row, this row is highlight as in Gmail application.
Can you help me and tell me what I'm doing wrong ?
Thank in advance
回答1:
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 !
回答2:
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());
    回答3:
did you tried setSelection()?
mListView.setSelection(0)
    回答4:
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.
来源:https://stackoverflow.com/questions/10296846/android-listview-select-first-item-after-loading-datas