我试图在我的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
中工作。
答案 0 :(得分:0)
通过覆盖适配器的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;
}
};