2013-08-14 2 views
2

내가 CheckBox를 사용하지 않고 상황에 맞는 작업 표시 줄과 ListView에 다중 선택을 구현하기 위해 노력하고있어 안드로이드 멀티 선택 모드 수신기. 내가 ArrayAdapterCHOICE_MODE_MULTIPLE_MODAL으로 사용할 때 문제가 없지만 ArrayAdapter을 확장하는 CustomAdapter로 전환 할 때 아무 것도하지 않는 것 같습니다. 사용자 정의 어댑터

public class MainActivity extends Activity implements PullToRefreshAttacher.OnRefreshListener { 

    private PullToRefreshAttacher mPullToRefreshAttacher; 

    private static final int SIMULATED_REFRESH_LENGHT = 5000; 

    private ArrayList<String> mItems = new ArrayList<String>(); 
    private SelectionAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     for (int i = 0; i < 24; i++) { 
      mItems.add("Name" + i); 
     } 

     mAdapter = new SelectionAdapter(this, R.layout.single_item, R.id.item, mItems); 
     final ListView listView = (ListView) findViewById(R.id.list); 
     listView.setLongClickable(true); 
     listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       listView.setItemChecked(i, !mAdapter.isPositionChecked(i)); 
      } 
     }); 
     listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { 

      private int nr = 0; 

      @Override 
      public void onItemCheckedStateChanged(ActionMode actionMode, int i, long l, boolean b) { 
       if (b) 
        nr++; 
       else 
        nr--; 
       actionMode.setTitle(nr + " Selected"); 
      } 

      @Override 
      public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { 
       MenuInflater inflater = actionMode.getMenuInflater(); 
       inflater.inflate(R.menu.main, menu); 
       return true; 
      } 

      @Override 
      public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { 
       return false; 
      } 

      @Override 
      public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { 
       StringBuilder sb = new StringBuilder(); 
       Set<Integer> positions = mAdapter.getCurrentCheckedPosition(); 
       for (Integer pos : positions) { 
        sb.append(" " + pos + ","); 
       } 
       Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show(); 
       actionMode.finish(); 
       return false; 
      } 

      @Override 
      public void onDestroyActionMode(ActionMode actionMode) { 
       mAdapter.clearSelection(); 
      } 
     }); 

     listView.setAdapter(new SlideExpandableListAdapter(mAdapter, R.id.expandable_toggle_button, R.id.expandable)); 

     mPullToRefreshAttacher = PullToRefreshAttacher.get(this); 
     mPullToRefreshAttacher.addRefreshableView(listView, this); 
    } 

    @Override 
    public void onRefreshStarted(View view) { 
     new AsyncTask<Void, Void, Void>() { 
      @Override 
      protected Void doInBackground(Void... params) { 
       try { 
        Thread.sleep(SIMULATED_REFRESH_LENGHT); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       super.onPostExecute(result); 
       mPullToRefreshAttacher.setRefreshComplete(); 
      } 
     }.execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    private class SelectionAdapter extends ArrayAdapter<String> { 

     private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>(); 

     public SelectionAdapter(Context context, int resource, int textViewResourceId, List<String> objects) { 
      super(context, resource, textViewResourceId, objects); 
     } 

     public void setNewSelection(int position, boolean value) { 
      mSelection.put(position, value); 
      notifyDataSetChanged(); 
     } 

     public boolean isPositionChecked(int position) { 
      Boolean result = mSelection.get(position); 
      return result == null ? false : result; 
     } 

     public Set<Integer> getCurrentCheckedPosition() { 
      return mSelection.keySet(); 
     } 

     public void removeSelection(int position) { 
      mSelection.remove(position); 
      notifyDataSetChanged(); 
     } 

     public void clearSelection() { 
      mSelection = new HashMap<Integer, Boolean>(); 
      notifyDataSetChanged(); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = super.getView(position, convertView, parent); 
      v.setBackground(getResources().getDrawable(R.drawable.card_bg_r4)); 
      if (mSelection.get(position) != null) { 
       v.setBackground(getResources().getDrawable(R.drawable.card_bg_r4)); 
      } 
      return v; 
     } 

    } 

} 

당신은 당신의 부모 LinearLayoutandroid:background="?androind:activatedBackgroundIndicator"을 추가 할 것입니다 행

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="@string/hello_world" 
      android:id="@+id/item" /> 
    <Button 
     android:id="@+id/expandable_toggle_button" 
     android:text="More" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignBottom="@+id/item" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@id/item"/> 
</RelativeLayout> 

<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:id="@+id/expandable" 
       android:background="#000000"> 

    <Button 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.5" 
      android:text="Action A" /> 

    <Button 
      android:id="@+id/details" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.5" 
      android:text="Action B"/> 
</LinearLayout> 
</LinearLayout> 

답변

0

의 레이아웃입니다. 선택시 강조 표시된 배경이 생성됩니다.

+1

변경되지 않고 시작하지 않음 CAB – Kage

+0

CAB에서 트리거 할 클래스에'Implements AbsListView.MultiChoiceModeListener'를 추가하십시오. – BC2

관련 문제