K
Krammi
Neues Mitglied
- 0
Hallo zusammen,
ich habe ein Problem mit der ExpandableListView. Ich verwende ein eigenes Layout für die Elemente der ExpandableListView. Dieses enthält eine TextView und eine CheckBox. Wenn der OnClickListener der TextView aufgerufen wird, dann möchte ich, dass das ListItem "gehighlighted" wird, wie beim OnChildClick oder OnParentClick.
Vielen Dank schon mal
LG
ich habe ein Problem mit der ExpandableListView. Ich verwende ein eigenes Layout für die Elemente der ExpandableListView. Dieses enthält eine TextView und eine CheckBox. Wenn der OnClickListener der TextView aufgerufen wird, dann möchte ich, dass das ListItem "gehighlighted" wird, wie beim OnChildClick oder OnParentClick.
Code:
public class MainActivity extends Activity {
private ExpandableListView expListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expListView = (ExpandableListView)findViewById(R.id.expandableListView);
ArrayList<Parent> parents = getParents();
MainAdapter adapter = new MainAdapter(this, parents);
expListView.setAdapter(adapter);
expListView.setOnChildClickListener(adapter);
expListView.setOnGroupClickListener(adapter);
}
@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 ArrayList<Parent> getParents() {
ArrayList<Parent> parents = new ArrayList<Parent>();
Child c1= new Child("test1", true);
Child c2= new Child("test2", true);
Child c3= new Child("test3", true);
Child c4= new Child("test4", true);
ArrayList<Child> childs1 = new ArrayList<Child>();
ArrayList<Child> childs2 = new ArrayList<Child>();
childs1.add(c1);
childs1.add(c2);
childs2.add(c3);
childs2.add(c4);
Parent p1 = new Parent("Group1", true, childs1);
Parent p2 = new Parent("Group2", true, childs2);
parents.add(p1);
parents.add(p2);
return parents;
}
}
Code:
public class MainAdapter extends BaseExpandableListAdapter implements OnChildClickListener, OnGroupClickListener{
private ArrayList<Parent> listParents= new ArrayList<Parent>();
Context context=null;
public MainAdapter(Context context, ArrayList<Parent> listParents){
this.context=context;
this.listParents=listParents;
}
@Override
public Object getChild(int parentPosition, int childPosition) {
return listParents.get(parentPosition).getChilds().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Child child = (Child)getChild(groupPosition, childPosition);
final int transmissionPos=groupPosition;
final int childPos=childPosition;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.main_list_item_child, null);
}
final CheckBox cb=(CheckBox)convertView.findViewById(R.id.list_item_child_checkbox);
cb.setChecked(child.isSelected());
cb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setChildCheckBox(cb.isChecked(),transmissionPos,childPos);
}
});
final TextView tv = (TextView)convertView.findViewById(R.id.list_item_child_text);
tv.setText(child.getName());
tv.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
setChildCheckBox(!cb.isChecked(),transmissionPos,childPos);
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return listParents.get(groupPosition).getChilds().size();
}
@Override
public Object getGroup(int groupPosition) {
return listParents.get(groupPosition);
}
@Override
public int getGroupCount() {
return listParents.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final int groupPos=groupPosition;
final Parent parentObject = (Parent) getGroup(groupPosition);
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.main_list_item_parent, null);
}
final CheckBox cb =(CheckBox) convertView.findViewById(R.id.list_item_parent_checkbox);
cb.setChecked(parentObject.isSelected());
cb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setParentCheckBox(cb.isChecked(), groupPos);
}
});
final TextView tv = (TextView)convertView.findViewById(R.id.list_item_parent_text);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Test");
setParentCheckBox(!cb.isChecked(), groupPos);
}
});
tv.setText(parentObject.getName());
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
CheckBox cb =(CheckBox)v.findViewById(R.id.list_item_child_checkbox);
setChildCheckBox(!cb.isChecked(),groupPosition,childPosition);
return false;
}
private void setChildCheckBox(boolean b, int parentPos, int childPos) {
listParents.get(parentPos).getChilds().get(childPos).setSelected(b);
notifyDataSetChanged();
}
private void setParentCheckBox(boolean b, int parentPos){
listParents.get(parentPos).setSelected(b);
notifyDataSetChanged();
}
}
Vielen Dank schon mal
LG