2017-08-30 50 views
0

當我旋轉手機時,所選項目在列表視圖中未被選中。這是我的適配器。我只是想改變選定項目的顏色(這是一個單一的選擇),當我旋轉手機,我想看看我選擇什麼。現在,當我旋轉手機時,我看不到選擇的項目。旋轉電話在列表中取消選擇我的項目查看

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" 

時我旋轉手機上創建不是工作king

回答

1

設置你的活動launchMode到singleTop

<activity android:launchMode="singleTop" > 
</activity> 

,並覆蓋onNewIntent:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    //setIntent(intent); 
} 

這將從活動堆棧重用活動。

0

發生這種情況是因爲Android重新加載了佈局(基本上在配置更改時重新創建了它)。在你的情況下,它是屏幕的旋轉。

您可以防止機器人通過活動下應用以下屬性,在manifest.xml中做這個地方這個問題發生

android:configChanges="orientation|screenSize" 

這告訴Android版要處理的配置改變自己。

相關問題