问题
I have searched similar threads but they didn't help me and they were light different or they still are unanswered, so I'll describe my trouble.
I have an ExpandableListView
with a custom
ExpandableListAdapter
and I can show my Groups
but not my Children
. GetChildView
don't get called anyway.
I would thank you for your Help.
EDIT:
If I log this section
...
client = (ExpandableListView)row.findViewById(R.id.field_expandable_list);
client.setFocusable(false);
client_adapter = new ClientAdapter(context, titles, allFields);
client.setAdapter(client_adapter);
Log.i("TAG", "WmsMApActivity::doClient:: Adapter #children: "+client_adapter.getChildrenCount(0)+
" ExpListView #children: "+client.getChildCount());
...
, then I get that
I/TAG(692): WmsMApActivity::doClient::0 Adapter #children: 13 ExpListView #children: 0
So, my Adapter gets Data but the variable client
(ExpandableListView) don't realize, it has data although I properly set it.
My code is the following
Main Code
// view = Item of my ListView. We come from a OnClickItem Method
private void doClient(View view) {
ArrayList<String> titles = new ArrayList<String>();
titles.add("fields");
ArrayList allFields = transformFarmsResult(parentItems);
final Context context = getApplicationContext();
RelativeLayout mainLayout = (RelativeLayout) view.findViewById(R.id.relLayoutDrawer);
// We check whether the first item (0th position) is expanded
View lin_layout_field_list = view.findViewById(R.id.lin_layout_field_list);
if ( lin_layout_field_list != null ) { // Hide Field_list Layout
((ViewGroup) view).removeView(lin_layout_field_list);
} else {
mainLayout = (RelativeLayout) view.findViewById(R.id.relLayoutDrawer);
// ----------- Inflating ExpandableListView "Fields"
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.field_list, null);
client = (ExpandableListView)row.findViewById(R.id.field_expandable_list);
// Adapter Population
client_adapter = new ClientAdapter(context, titles, allFields);
client.setAdapter(client_adapter);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title_tv = (TextView)view.findViewById(R.id.title);
params.addRule(RelativeLayout.ALIGN_LEFT, title_tv.getId() );
params.addRule(RelativeLayout.BELOW, title_tv.getId());
mainLayout.addView(row, params);
// Listview Group click listener
client.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Toast.makeText(context, "Group Clicked " + groupPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
client.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(context, "Group " + groupPosition + " Expanded", Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
client.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(context, "Group " + groupPosition + " Collapsed", Toast.LENGTH_SHORT).show();;
}
});
// Listview on child click listener
client.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(context, "Group " + groupPosition + " : Child " + childPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Adapter
public class ClientAdapter extends BaseExpandableListAdapter{
private Context context;
//private String header_title;
private ArrayList<String> headers;
private ArrayList fields;
private int groupPosition = 0;
private LayoutInflater inflater;
public ClientAdapter(Context context, ArrayList<String> headers, ArrayList fields) {
this.context = context;
this.headers = headers;
this.fields = fields;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Just to check, whether I get correct ArrayList "fields"
Log.i("TAG","ClientAdapter::Constructor:: fields.size: "+fields.size());
for (int i=0;i<fields.size();i++)
Log.i("TAG","ClientAdapter::Constructor:: field["+i+"]: "+fields.get(i).toString());
}
@Override
public int getGroupCount() {
// Just 1 Group
return headers.size();
}
@Override
public String getGroup(int groupPosition) {
return headers.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = convertView;
GroupHolder holder=null;
String title_item = getGroup(groupPosition);
if ( view == null ) {
holder = new GroupHolder();
view = inflater.inflate(R.layout.group_client, null);
holder.title = (TextView) view.findViewById(R.id.client_group_title);
view.setTag(holder);
} else {
holder = (GroupHolder) view.getTag();
}
holder.title.setText(title_item);
return view;
}
// Children
@Override
public int getChildrenCount(int groupPosition) {
return fields.size();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return fields.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) {
// TODO Auto-generated method stub
View view = convertView;
ChildHolder holder;
Log.i("TAG", "ClientAdapter::getChildView:: 0 - parent.id: "+parent.getId());
final HashMap<String,Object> field = (HashMap<String,Object>) getChild(groupPosition, childPosition);
if (view == null) {
holder = new ChildHolder();
view = inflater.inflate(R.layout.child_client, null);
holder.title = (TextView) view.findViewById(R.id.client_child_title);
holder.map_icon = (ImageView) view.findViewById(R.id.client_child_icon);
holder.contour = (CheckBox) view.findViewById(R.id.client_child_checkbox);
convertView.setTag(holder);
} else {
holder = (ChildHolder) view.getTag();
}
// Handling of Title
String temp =(String)field.get(Constants._FIELDNAME);
Log.i("TAG", "ClientAdapter::getChildView:: title: "+temp);
holder.title.setText((String)field.get(Constants._FIELDNAME));
// Handling of Contour - If it exists!
if ( field.containsKey(Constants._BOUNDARY)) {
holder.contour.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (((CheckBox)v).isChecked()) {
Toast.makeText(context, "Contour enabled", Toast.LENGTH_LONG).show();
// TODO : Fade-in Contour in Graphics
} else {
Toast.makeText(context, "Contour disabled", Toast.LENGTH_LONG).show();
// TODO : Fade-in Contour in Graphics
}
}
});
} else { // If there is no Contour, remains the checkbox but it's not clickable, so, it's grey!
holder.contour.setFocusable(false);
holder.contour.setClickable(false);
}
// Handling of Map Icon
Bitmap bitmap_icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.gmaps_icon_24);
holder.map_icon.setImageBitmap(bitmap_icon);
// Applying gray filter if not referentiable
if ( !field.containsKey(Constants._LATITUDE) || !field.containsKey(Constants._LONGITUDE))
holder.map_icon.setColorFilter(Color.GRAY);
holder.map_icon.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if ( field.containsKey(Constants._LATITUDE) && field.containsKey(Constants._LONGITUDE)) {
Toast.makeText(context, "Center in Field Position", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, Constants.NOT_CENTRED, Toast.LENGTH_LONG).show();
}
}
});
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
/** Events **/
/** Holder Classes **/
private static class ChildHolder {
TextView title;
ImageView map_icon;
CheckBox contour;
}
private static class GroupHolder {
TextView title;
}
}
My Layouts.
Main xml. field_list.xml -> Layout for ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lin_layout_field_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/field_expandable_list"
android:layout_width="match_parent"
android:layout_height="0dp" <- That's a SO advice, in order that children won't be croped. Originally wrap_content
android:layout_weight="1"
android:headerDividersEnabled="true"
android:divider="@color/metallic_silver"
android:dividerHeight="1dp">
</ExpandableListView>
</LinearLayout>
Group_client.xml -> Layout for each Group Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lin_layout_client_group"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/client_group_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:text="Something"
android:textColor="@android:color/white"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
</LinearLayout>
child_client.xml -> Layout for each Child Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lin_layout_client_child"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/client_child_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:checked="true"
android:gravity="center_vertical|center" />
<TextView
android:id="@+id/client_child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:text="FIELD_NAME"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white"
android:layout_weight="0.90" />
<ImageView
android:id="@+id/client_child_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:layout_margin="5dp"
android:gravity="center_vertical|center"
android:src="@drawable/gmaps_icon_24" />
</LinearLayout>
</LinearLayout>
EDIT(2):
As said, it's no problem cause of Layouts or ClientAdapter
, so that it works fine in this example
public class ClientActivity2 extends Activity{
private ExpandableListView client;
private ClientAdapter client_adapter;
private ArrayList children;
private ArrayList<String> groups;
private LayoutInflater inflater;
private LinearLayout lin_layout_probe;
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.field_list);
context = getApplicationContext();
prepareData();
ExpandableListView client = (ExpandableListView)findViewById(R.id.field_expandable_list);
client_adapter = new ClientAdapter(context, groups, children);
client.setAdapter(client_adapter);
client.expandGroup(0);
}
private void prepareData() {
children = new ArrayList();
HashMap<String,Object> hm1 = new HashMap<String,Object>();
hm1.put(Constants._FIELDNAME,"Child1");
children.add(hm1);
HashMap<String,Object> hm2 = new HashMap<String,Object>();
hm2.put(Constants._FIELDNAME,"Child2");
children.add(hm2);
HashMap<String,Object> hm3 = new HashMap<String,Object>();
hm3.put(Constants._FIELDNAME,"Child3");
children.add(hm3);
groups = new ArrayList<String>();
groups.add("Group1");
}
}
Now it comes what confuses me...
If I test all(Adapter and the same calls) with a simple Activity, which has a clickable TextView
at its main layout then works fine!
If I click this TextView then the ExpandableListView
prompts with properly populated Group
and Children
and they are expandable
/collapsable
.
If I test like my last post hier,(ExpandableListView
embedded in a ListView
Item) so, the ExpandableListView
prompts, if I click that the ExpandableListView
of that Item
then the groups are populated but not the children. GetChildView
gets never called!!!(I used LogCat, so I'm completely sure)
I have seen, many people had troubles with clicks in ExpandableListViews in ListViewItems, my case is different, I can perform that, my trouble is understanding, why with a TextView works fine that ExpandableListView Population via my custom BaseExpandableListAdapter
but not in a ListView
Item.
回答1:
Solved
In my first level Adapter (An adapter for the first level ExpandableListView
) I modified the getChildView
Method
...
// Member Field
private ExpandableListView expandable_field_list;
...
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if ( groupPosition == 1 ) {
if ( expandable_field_list == null ) {
expandable_field_list = new CustomExpandableListView(context);
// Here I initialize the second and third Level
client_adapter = new ClientAdapter(context, groups, children);
expandable_field_list.setAdapter(client_adapter);
} else {
client_adapter.notifyDataSetChanged();
}
convertView = expandable_field_list;
}
return convertView;
}
...
And CustomExpandableListView
looks like this
public class CustomExpandableListView extends ExpandableListView {
private boolean expanded = true;
public CustomExpandableListView(Context context) {
super(context);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onDetachedFromWindow() {
try {
super.onDetachedFromWindow();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
public void setIsExpanded(boolean expanded) {
this.expanded = expanded;
}
public boolean isExpanded() {
return expanded;
}
}
That is its aspect now as desired
来源:https://stackoverflow.com/questions/26379164/getchildview-gets-not-called