2016-02-29 2 views
2

Я разрабатываю приложение, которое используется для ExpandableListview. Во-первых, я добавляю заголовки, а затем добавляю дочерние элементы в определенные заголовки. Но когда я добавляю childs, каждый заголовок обновления ExpandableListView. (Объяснение скриншотов)Добавление дочернего элемента в определенный заголовок ExpandableListView

Я использую адаптер BaseExpandableListView, но не могу решить эту проблему. Любая помощь очень достойна для меня. Спасибо

Здесь мой класс адаптера;

public class SYCriteraAdapter extends BaseExpandableListAdapter { 

private LayoutInflater inflater; 
private ArrayList<SYCriteraModel> mParent; 
private ArrayList<ArrayList<SYCriteraModel>> mChild; 
private View view; 


public ArrayList<SYCriteraModel> getMParent() { 
    return mParent; 
} 


public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList) { 
    this.mParent = parentList; 
    this.inflater = LayoutInflater.from(context); 

} 

public SYCriteraAdapter(Context context,ArrayList<SYCriteraModel> parentList,ArrayList<ArrayList<SYCriteraModel>> childList) { 
    this.mParent = parentList; 
    this.inflater = LayoutInflater.from(context); 
    this.mChild = childList; 

} 

// counts the number of group/parent items so the list knows how many 
// times calls getGroupView() method 
public int getGroupCount() { 
    return mParent.size(); 
} 

// counts the number of children items so the list knows how many times 
// calls getChildView() method 
public int getChildrenCount(int parentPosition) { 
    int childCount = 0; 
    try { 
     childCount = mChild.get(parentPosition).size(); 
    } 
    catch (Exception e){ 
     System.out.println("Exception " + e); 
    } 
    return childCount; 
} 

// gets the title of each parent/group 
public Object getGroup(int i) { 
    return null; 
} 

// gets the name of each item 
public Object getChild(int parentPosition, int childPosition) { 

    return null; 
} 

public long getGroupId(int parentPosition) { 
    return 0; 
} 

public long getChildId(int i, int childPosition) { 
    return 0; 
} 

public boolean hasStableIds() { 
    return false; 
} 

// in this method you must set the text to see the parent/group on the list 

public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) { 
    if (view == null) { 
     view = inflater.inflate(R.layout.critera_upper_adapter, viewGroup, false); 
    } 
    TextView uppercritera = (TextView) view.findViewById(R.id.etCritera); 
    this.notifyDataSetChanged(); 
    uppercritera.setText(mParent.get(parentPosition).getParent()); 
    return view; 

} 

// in this method you must set the text to see the children on the list 

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) { 
    if (view == null) { 
     view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false); 
    } 
    TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera); 
    lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren()); 

    return view; 
} 

public boolean isChildSelectable(int i, int i1) { 
    return true; 
} 


} 

enter image description here enter image description here

ответ

1

Существует один способ getChildView внутри BaseExpandable адаптер проверка материнской позиции в этом методе.

public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) { 
if (view == null) { 
    view = inflater.inflate(R.layout.critera_lower_adapter, viewGroup, false); 
} 

// Здесь Вы должны проверить положение parentPosition ребенка должно быть добавлено или нет, е

если (parentPosition == 1 || parentPosition == 3 || parentPosition == 5)

TextView lowerCritera = (TextView) view.findViewById(R.id.etCritera); 
lowerCritera.setText(mChild.get(parentPosition).get(childPosition).getChildren()); 

return view; 
+0

Но иногда я не знаю родительскую позицию. Предположим, что я определил родительскую позицию 5, и у меня только 3 родителя? Как я могу получить гибкость в этом фрагменте кода? – salih

+0

, если вы не знаете положения родителя, чем конкретно установите ребенка? ok не путайте проверку родителя, я имею в виду текст заголовка, если его совпадение, а не только добавление дочернего элемента. –

+0

Я знаю родительскую позицию, но меняю ее. Я ссылаюсь на его изменчивость. – salih

2

попробовать, установив clicklistener

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      Toast.makeText(MainActivity.this,headers.get(groupPosition)+"--"+headeritems.get(headers.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 

для получения более подробной информации см здесь http://coderzpassion.com/android-expandable-listview-tutorial/

+0

Я использую setOnGroupClickListener для добавьте childs на ExpandableListView. – salih

Смежные вопросы