2017-08-30 7 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です:

<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> 

そして、私はマニフェストにこれを追加します:

<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ある

android:configChanges="keyboardHidden|orientation|screenSize" 

私は電話を回転させる。作成は面倒ではないking

答えて

1

singleTopにあなたの活動launchModeを設定

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

    //setIntent(intent); 
} 

をこの活動のスタックから活動を再利用します。

0

これは、Androidがレイアウトを再読み込みするためです(基本的には設定の変更によって再作成されるため)。あなたの場合、それは画面の回転です。

あなたは、この問題が

android:configChanges="orientation|screenSize" 

これは、あなたが設定を自分で変更処理したいアンドロイドに指示を起こる活動の下であなたのmanifest.xmlで次の属性を適用することによって、これを行うことから、アンドロイドのを防ぐことができます。

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

をしてonNewIntentオーバーライドします:

関連する問題