ListFragment 中的选定项目未突出显示

Selected item in ListFragment doesn't get highlighted

我试图在我的 ListFragment 中突出显示之前选择的项目,但它对我不起作用。突出显示是唯一的问题,因为我能够在控制台中看到我选择的内容,但着色无论出于何种原因都无法正常工作。

这是我用来突出显示的代码

@Override
public void onStart() {
    super.onStart();
    ListView view = getListView();
    TextView previousSelected = null;
    int selectedPosition = 0;
    if(view != null) {
        int adapterSize = view.getAdapter().getCount();
        if (selectedView != null) {
            for (int i = 0; i < adapterSize; i++) {
                Log.d(TAG, "view getchildat " + view.getAdapter().getView(i, null, view) + " selected " + selectedView);
                if (((TextView) view.getAdapter().getView(i, null, view)).getText().toString().equalsIgnoreCase(((TextView) selectedView).getText().toString())){
                    previousSelected = (TextView) view.getAdapter().getView(i, null, view);
                    Log.d(TAG, "selected is " + previousSelected.getText().toString());
                    view.setSelection(i);
                    view.setSelected(true);
                    view.setFocusable(true);
                    selectedPosition = i;
                }
            }
        } else if (tagScan.getLastFeedback()[0] != null) {
            Log.d(TAG, "count fragment is " + count);
            for(int i = 0; i < adapterSize; i++){
                Log.d(TAG, "view getchildat " + ((TextView)view.getAdapter().getView(i, null, view)).getText().toString() + " selected " + tagScan.getLastFeedback()[count]);
                if (((TextView) view.getAdapter().getView(i, null, view)).getText().toString().equalsIgnoreCase(tagScan.getLastFeedback()[count])){
                    previousSelected = (TextView) view.getAdapter().getView(i, null, view);
                    Log.d(TAG, "selected is " + previousSelected.getText().toString());
                    view.setSelection(i);
                    view.setSelected(true);
                    view.setFocusable(true);
                }
            }
        }
        if(previousSelected != null){
            Log.d(TAG,"set color of the previous selection " + previousSelected.getText().toString());
            previousSelected.setBackgroundColor(Color.BLUE);
            previousSelected.setTextColor(Color.WHITE);
            selectedView = previousSelected;

        }
    }
}

我试过使用选择器,但也不成功:

 <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/list_selector_selected"/>
    <item
        android:state_activated="true"
        android:drawable="@drawable/list_selector_selected"/>
    <item
        android:state_focused="true"
        android:drawable="@drawable/list_selector_selected"/>
    <item
        android:state_pressed="true"
        android:drawable="@drawable/list_selector_selected"/>
    <!--<item-->
        <!--android:drawable="@drawable/list_selector_selected" />-->
</selector>

对我来说,突出显示在 onClickListener 中效果很好,所以我不确定为什么它在 onStart 中不起作用。

通过像这样覆盖适配器的 getView 来修复它

new ArrayAdapter<String>(this, R.layout.feedback_list, feedback.getOptions()){
                            @Override
                            public View getView(int position, View convertView, ViewGroup parent) {
                                View view = super.getView(position, convertView, parent);
                                Log.d(TAG,"view is " + view + " selected is " + listFragment.selectedView);
                                if(listFragment.selectedPosition != null) {
                                    if (listFragment.selectedPosition[0] == position) {
                                        view.setBackgroundColor(R.color.colorPrimary);
                                    }
                                }
                                return view;
                            }
                        };