2017-06-27 94 views
0

我實施了一個小應用程序兩個檢查一些ExpandableListView我在一個更大的應用程序中的錯誤。我閱讀並通過thisthat後。但後來我決定在第一次運行時不使用ViewHolder模式來實現ExpandableListView適配器。Android ExpandableListView兒童interdepending也沒有ViewHolder

但是,當打開和關閉父視圖時,我仍然有一個錯誤。這裏是我的MainActivity:

public class MainActivity extends AppCompatActivity { 

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

     ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.exp_list_view); 

     List<Parent> parentList = new ArrayList<>(); 
     Parent parent; 

     List<Child> childList; 

     childList = new ArrayList<>(); 
     childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three")); 

     parent = new Parent("First", childList); 
     parentList.add(parent); 

     childList = new ArrayList<>(); 
     childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three")); 

     parent = new Parent("Second", childList); 
     parentList.add(parent); 

     childList = new ArrayList<>(); 
     childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three")); 

     parent = new Parent("Third", childList); 
     parentList.add(parent); 

     childList = new ArrayList<>(); 
     childList.add(new Child("One")); childList.add(new Child("Two")); childList.add(new Child("Three")); 

     parent = new Parent("Forth", childList); 
     parentList.add(parent); 

     final MyAdapter adapter = new MyAdapter(this, parentList); 
     expandableListView.setAdapter(adapter); 
    } 
} 

我的適配器類:

class MyAdapter extends BaseExpandableListAdapter { 


    private final List<Parent> mParentList; 
    private final Context mContext; 

    public MyAdapter(final Context context, final List<Parent> parentList) { 
     mContext = context; 
     mParentList = parentList; 
    } 

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

    @Override 
    public int getChildrenCount(final int groupPosition) { 
     return mParentList.get(groupPosition).getChildList().size(); 
    } 

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

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

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

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

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

    @Override 
    public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      v = inflater.inflate(R.layout.row_parent, parent, false); 
     } 
     TextView tvn = (TextView) v.findViewById(R.id.tv_parent_name); 
     tvn.setText(mParentList.get(groupPosition).getName()); 
     return v; 
    } 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) { 
     View row = convertView; 
     if (row == null) { 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      row = inflater.inflate(R.layout.row_child, parent, false); 
     } 
     TextView tvn = (TextView) row.findViewById(R.id.tv_child_name); 
     Switch sw = (Switch) row.findViewById(R.id.sw_child); 
     tvn.setText(mParentList.get(groupPosition).getChildList().get(childPosition).getName()); 
     sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { 
       Log.v(getClass().getName(), "onCheckedChanged() (" + groupPosition + ", " + childPosition + ")"); 
      } 
     }); 
     return row; 
    } 

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

和我的兩個模型類。家長:

public class Parent { 
    private String name; 
    private List<Child> childList = new ArrayList<>(); 

    public Parent(final String name, List<Child> childList) { 
     this.name = name; 
     this.childList = childList; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(final String name) { 
     this.name = name; 
    } 

    public List<Child> getChildList() { 
     return childList; 
    } 

    public void setChildList(final List<Child> childList) { 
     this.childList = childList; 
    } 
} 

兒童:

public class Child { 
    private String name; 

    public Child(final String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(final String name) { 
     this.name = name; 
    } 
} 

現在我觀察上運行的活動如下問題。請遵循:

  1. 展開所有的父母(從第一個到等)
  2. 點擊切換從父第二個和兩個來回(從上到下)
  3. unexpand所有的父母(從第一個到類推)
  4. unexpand所有的父母(從第一個到等)

觀察:

  • 秒家長:只有一個和兩個選擇
  • 的家長:一,二和三是選擇
  • 提出父:只有兩個選擇

這是怎麼回事錯在這裏?此外,實現ViewHolder模式也不起作用。

回答

0

我有兩點:

  1. 延長孩子的狀態,並跟蹤它
  2. 獲取內getChildView()擺脫if語句和getGroupView

我的新的子類現在是:

public class Child { 
    private String name; 
    private boolean state; 

    public Child(final String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(final String name) { 
     this.name = name; 
    } 

    public boolean isState() { 
     return state; 
    } 

    public void setState(final boolean state) { 
     this.state = state; 
    } 
} 

我的新getChildView()方法:

@Override 
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, final View convertView, final ViewGroup parent) { 
    View view = layoutInflater.inflate(R.layout.child_row, parent, false); 

    final Switch sw = (Switch) view.findViewById(R.id.sw); 
    TextView textViewChildName = (TextView) view.findViewById(R.id.childname); 


    String name = ((Child) getChild(groupPosition, childPosition)).getName(); 

    sw.setChecked(list.get(groupPosition).getChildList().get(childPosition).isState()); 

    textViewChildName.setText(name); 

    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { 
      list.get(groupPosition).getChildList().get(childPosition).setState(isChecked); 
      sw.setChecked(isChecked); 
     } 
    }); 

    return view; 
} 

最後getGroupView()

@Override 
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView, final ViewGroup parent) { 
    View view = layoutInflater.inflate(R.layout.group_row, parent, false); 
    TextView textView = (TextView) view.findViewById(R.id.groupname); 

    String name = ((Parent) getGroup(groupPosition)).getName(); 
    textView.setText(name); 

    return view; 
} 

我曾與ViewHolder模式退出。它不適用於我,因爲我正在使用開關並希望保持狀態。