2015-05-02 20 views
8

Uygulamamda, görüntüleri bir genişletilebilirListView içinde göstermem gerekiyor ve bu resimlerin bir grüntü görünümü olmalı ve bu resimler seçilebilir, böylece bir genişletilebilirListAdapter ve bunun içindeyim Bir gridview bağdaştırıcısı, uygulanması, görüntü 1'de görebildiğiniz kadar iyi görüntüleniyor, şimdi elde etmeye çalıştığım şey resim 2, grupPosition'ı ve childPosition'ı gridAdapter'e nasıl geçirebilirim? görüntü vurgulamak mı? :GridView, Android'de çoktan seçmeli olarak GenişletilebilirListView ile

enter image description here

Resim 2: enter image description here

public class ExpandListAdapter extends BaseExpandableListAdapter { 


    public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE; 


    public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL; 

    /** 
    * No child could be selected 
    */ 
    public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE; 

    /** 
    * One single choice per group 
    */ 
    public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE; 

    /** 
    * One single choice for all the groups 
    */ 
    public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001; 

    private Context context; 
    private ArrayList<Group> groups; 
    private ArrayList<ArrayList<Child>> child = new ArrayList(); 
    private ArrayList<Child> listchild; 
    private GridAdapter adapter; 
    private CustomGridView gridView; 
    private SparseArray<SparseBooleanArray> checkedPositions; 
    private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName(); 

    private int choiceMode = CHOICE_MODE_MULTIPLE; 

    public ExpandListAdapter(Context context, ArrayList<Group> groups) { 
     this.context = context; 
     this.groups = groups; 
     checkedPositions = new SparseArray<SparseBooleanArray>(); 
     child = new ArrayList(); 
     for (int i = 0; i < groups.size(); i++) { 
      child.add(i, groups.get(i).getItems()); 
     } 
    } 

    public ExpandListAdapter(Context context, ArrayList<Group> children, int choiceMode) { 
     this(context, children); 
     // For now the choice mode CHOICE_MODE_MULTIPLE_MODAL 
     // is not implemented 
     if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) { 
      throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " + 
        "has not implemented yet"); 
     } 
     this.choiceMode = choiceMode; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return child.get(childPosition); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 


     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.gridview, null); 
     } 


     listchild = new ArrayList<Child>(); 

     for (int j = 0; j < groups.get(groupPosition).getItems().size(); j++) { 

      listchild.add(child.get(groupPosition).get(j)); 

     } 

     gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar); 

     gridView.setExpanded(true); 
     adapter = new GridAdapter(context, listchild, checkedPositions, groupPosition, childPosition); 
     gridView.setAdapter(adapter);// Adapter 
     gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE); 
     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       setClicked(groupPosition, childPosition); 
       System.out.println("position" + checkedPositions.get(groupPosition)); 
       if (checkedPositions.get(groupPosition) != null) { 
        boolean isChecked = checkedPositions.get(groupPosition).get(childPosition); 

        if(!isChecked){ 
        } 

       } else { 
        System.out.println("false"); 
       } 

      } 
     }); 


     return convertView; 

    } 

    public void setClicked(int groupPosition, int childPosition) { 
     switch (choiceMode) { 
      case CHOICE_MODE_MULTIPLE: 
       SparseBooleanArray checkedChildPositionsMultiple = checkedPositions.get(groupPosition); 
       // if in the group there was not any child checked 
       if (checkedChildPositionsMultiple == null) { 
        checkedChildPositionsMultiple = new SparseBooleanArray(); 
        // By default, the status of a child is not checked 
        // So a click will enable it 
        checkedChildPositionsMultiple.put(childPosition, true); 
        checkedPositions.put(groupPosition, checkedChildPositionsMultiple); 
       } else { 
        boolean oldState = checkedChildPositionsMultiple.get(childPosition); 
        checkedChildPositionsMultiple.put(childPosition, !oldState); 
       } 
       break; 
      // TODO: Implement it 
      case CHOICE_MODE_MULTIPLE_MODAL: 
       throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " + 
         "has not implemented yet"); 
      case CHOICE_MODE_NONE: 
       checkedPositions.clear(); 
       break; 
      case CHOICE_MODE_SINGLE_PER_GROUP: 
       SparseBooleanArray checkedChildPositionsSingle = checkedPositions.get(groupPosition); 
       // If in the group there was not any child checked 
       if (checkedChildPositionsSingle == null) { 
        checkedChildPositionsSingle = new SparseBooleanArray(); 
        // By default, the status of a child is not checked 
        checkedChildPositionsSingle.put(childPosition, true); 
        checkedPositions.put(groupPosition, checkedChildPositionsSingle); 
       } else { 
        boolean oldState = checkedChildPositionsSingle.get(childPosition); 
        // If the old state was false, set it as the unique one which is true 
        if (!oldState) { 
         checkedChildPositionsSingle.clear(); 
         checkedChildPositionsSingle.put(childPosition, !oldState); 
        } // Else does not allow the user to uncheck it 
       } 
       break; 
      // This mode will remove all the checked positions from other groups 
      // and enable just one from the selected group 
      case CHOICE_MODE_SINGLE_ABSOLUTE: 
       checkedPositions.clear(); 
       SparseBooleanArray checkedChildPositionsSingleAbsolute = new SparseBooleanArray(); 
       checkedChildPositionsSingleAbsolute.put(childPosition, true); 
       checkedPositions.put(groupPosition, checkedChildPositionsSingleAbsolute); 
       break; 
     } 

     // Notify that some data has been changed 
     notifyDataSetChanged(); 
     Log.v(LOG_TAG, "List position updated"); 
     Log.v(LOG_TAG, PrintSparseArrays.sparseArrayToString(checkedPositions)); 
    } 

    public void setChoiceMode(int choiceMode) { 
     this.choiceMode = choiceMode; 
     // For now the choice mode CHOICE_MODEL_MULTIPLE_MODAL 
     // is not implemented 
     if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) { 
      throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " + 
        "has not implemented yet"); 
     } 
     checkedPositions.clear(); 
     Log.v(LOG_TAG, "The choice mode has been changed. Now it is " + this.choiceMode); 
    } 

    @Override 
    public int getChildrenCount(int nGroup) { 
     return 1; 

    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return groups.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     Group group = (Group) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater inf = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = inf.inflate(R.layout.group_item, null); 
     } 
     ExpandableListView mExpandableListView = (ExpandableListView) parent; 
     mExpandableListView.expandGroup(groupPosition); 
     TextView tv = (TextView) convertView.findViewById(R.id.group_name); 
     tv.setText(group.getName()); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

} 

GridAdapter: My Varsayım olarak

public class GridAdapter extends BaseAdapter { 

    private Context mContext; 
    private ArrayList<Child> child; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 
    private SparseArray<SparseBooleanArray> checkedPositions; 
    int groupPosition, childPosition; 

    public GridAdapter(Context context, ArrayList<Child> childValues, SparseArray<SparseBooleanArray> checkedPositions, int groupPosition, int childPosition) { 
     mContext = context; 
     child = childValues; 
     this.checkedPositions = checkedPositions; 
     this.childPosition = childPosition; 
     this.groupPosition = groupPosition; 
    } 


    @Override 
    public int getCount() { 
     return child.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder = null; 
     NetworkImageView i; 
     childPosition = position; 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.child_item, null); 
      holder = new ViewHolder(); 

      if (imageLoader == null) 
       imageLoader = AppController.getInstance().getImageLoader(); 

      i = (NetworkImageView) convertView 
        .findViewById(R.id.flag); 

      i.setImageUrl(String.valueOf(child.get(childPosition).getImage()), imageLoader); 


      convertView.setTag(holder); 

      System.out.println("position" + checkedPositions.get(groupPosition)); 
      if (checkedPositions.get(groupPosition) != null) { 
       boolean isChecked = checkedPositions.get(groupPosition).get(childPosition); 
       i.setBackgroundResource(R.color.bg); 

       if(!isChecked){ 
       } 

      } else { 
       System.out.println("false"); 
      } 


     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.text = (TextView) convertView.findViewById(R.id.label); 
     holder.text.setText(child.get(position).getName()); 


     return convertView; 
    } 


    static class ViewHolder { 
     TextView text; 
    } 


} 
+0

ana yanı düzeninizi ve ekleyebilir miyim? –

+1

u bu ans benzeri benzer: http://stackoverflow.com/q/21431097/1168654 –

cevap

1

ExpandListAdapter Sınıf

getChildView yılında
  • () childPosition burada Tablo görünümü konumunu döndürür Çünkü tıklandığında ediliyor hangi ülkede bayrak bağlıdır gelmez childPosition yöntem değil bayraklar

  • Yani bunun altında, GridView yapıcısı içinde childPosition geçmesine gerek yok, childPosition beri hep gerçek bayrak gibi GridAdapter içine yerleştirin Kullanım OnClickListener tıklandığında almak için 1

  • döndürür:

@Override kamu Görünüm GetView (int pozisyonu, Görünüm convertView, ViewGroup ebeveyn) {

 ViewHolder holder = null; 
     NetworkImageView i; 
     childPosition = position; 

     if (convertView == null) { 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     convertView.setOnclickListener(new OnClickListener{ 

      @Override 
      onClick(View v){ 
     isChecked=child.get(childPosition).isChecked(); 
     if(!isChecked){ 
      child.get(childPosition).setChecked(true); 
     } 
     else { 
      child.get(childPosition).setChecked(false); 
     }   

     }); 

(Çocuğunuz sınıfında isChecked içinde iki yöntem oluşturmak ayrı ayrı kontrol pozisyonları işlemek gerek yok Bayrakların durumunu işaretlemenin işaretini kaldırmak için ve setChecked(). Burada seçilen veya kontrol bayrağın Rengi değiştir

 holder.text = (TextView) convertView.findViewById(R.id.label); 
     holder.text.setText(child.get(position).getName()); 


     return convertView; 
    } 

ve ExpandListAdapter Class adresinin Kontrol mantığı kaldırın:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        setClicked(groupPosition, childPosition); 
        System.out.println("position" + checkedPositions.get(groupPosition)); 
        if (checkedPositions.get(groupPosition) != null) { 
         boolean isChecked = checkedPositions.get(groupPosition).get(childPosition); 

         if(!isChecked){ 
         } 

        } else { 
         System.out.println("false"); 
        } 

       } 
      }); 
+1

kodunuzu düzenleyebilir miyim, ben nullpointerexception alıyorum isChecked = checkedPositions.get (groupPosition) .get (childPosition); – AND4011002849

+0

, ** Child ** sınıfınızın içinde işaretlenmiş işaretli boole bayrağı bulundurmak daha iyidir. isChecked() return boolean İş yükünü azaltacak ve kontrol edilen konumların ayrı ayrı seyrini korumaya gerek kalmayacak **. Bu nedenle, Kod şu şekilde olabilir: // bayrağı yeniden bayraklamak için isChecked = child.get (childPosition) .IsChecked(); // bayrağı ayarlamak için child.get (childPosition) .setChecked (true); Kod şu şekilde düzenliyor –

+0

Çocuk sınıfımda şöyle bir yöntem ekledim: public boolean isChecked() { return false; } ama bu her zaman yanlış geri dönecek, doğru mu? – AND4011002849

0

listenize gridAdapter bir 'OnItemClickListener' ayarlamanız gerekir ve overide onItemClick yöntemi aşağıdaki gibi.

Not: - Bu, tıklattığınız ImageView öğesini vurgulayacaktır ve aynı GridView'da başka bir ImageView öğesine tıklarsanız, ImageView'ın da vurgulanacaktır! 'Çoklu seçim' gibi.

gridAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      ImageView selectedFlagImageView = (ImageView) view; 
      selectedFlagImageView.setBackgroundColor(Color.parseColor("#37a93400")); 
     } 
    }); 

Bu, seçilen ImageView arka planını renklendirecek.Burada kullandığım Renk kodu daha alfa ve daha az RGBS, basitçe. (Kullandım Android Studio renk seçici) sana bir arka plan vererek zaman nasıl görüneceğini hakkında fikir verecektir

android studio color chooser

takiben görüntüleri bunun gibi renk. arka plan rengi olmadan

:

image view with no background colors

arka plan rengi ile:

image view with background color

+0

Bağdaştırıcılara dinleyici eklenemez –