当我旋转手机时,列表视图中未选中所选项目。这是我的适配器。我只想更改所选项目的颜色(仅限单个选项),当我旋转手机时,我想看看我选择的是什么。现在当我旋转手机时,我没有看到一个选择项目。
public class MainAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context context;
ArrayList<RowEntity> rowEntities;
public MainAdapter(Context context, ArrayList<RowEntity> rowEntities) {
this.context = context;
this.rowEntities = rowEntities;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return rowEntities.size();
}
@Override
public Object getItem(int position) {
return rowEntities.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.main_row_holder, null);
holder.address = (TextView) convertView.findViewById(R.id.address);
holder.distance = (TextView) convertView.findViewById(R.id.tv_distance);
holder.description = (TextView) convertView.findViewById(R.id.description);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
RowEntity rowEntity = rowEntities.get(position);
if(rowEntity != null){
holder.address.setText(rowEntity.getAddress());
holder.address.setText(rowEntity.getAddress());
holder.distance.setText(rowEntity.getDistance() +"");
}
return convertView;
}
private class ViewHolder{
TextView address;
TextView distance;
TextView description;
TextView name;
}
}
这是我在布局上的listView:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/background_listview"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/bottomPanel" />
这是background_listview.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white_background"
android:state_pressed="true" />
<item android:drawable="@color/primary_color"
android:state_focused="false" />
</selector>
我在清单上添加了这个:
android:configChanges="keyboardHidden|orientation|screenSize"
当我旋转手机onCreate无法正常工作
答案 0 :(得分:1)
将您的活动launchMode设置为singleTop
:
<activity android:launchMode="singleTop" >
</activity>
并覆盖onNewIntent:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//setIntent(intent);
}
这将重复使用活动堆栈中的活动。
答案 1 :(得分:0)
这是因为Android重新加载了布局(基本上是在配置更改时重新创建它。在你的情况下,它是屏幕的旋转。
您可以通过在发生此问题的活动下的manifest.xml中应用以下属性来阻止android执行此操作
android:configChanges="orientation|screenSize"
这告诉android你想自己处理配置更改。