I have a system of nested ExpandableListViews that continue on for a few generations (a tree-type layout) for comments. Each top-level comment can have comments with children on those comments. To do so, I have a main ExpandableListView to store all the top level comments, and each "comment" XML contains an ExpandableListView for any child comments. The adapter for my ExpandableListView is as follows
public class ExpandableCommentAdapter extends BaseExpandableListAdapter {
private static final class ViewHolder {
TextView textLabel;
}
private final List<CommentNode> itemList;
private final LayoutInflater inflater;
private Context main;
public ExpandableCommentAdapter(Context context, List<CommentNode> itemList) {
this.inflater = LayoutInflater.from(context);
this.itemList = itemList;
main = context;
}
#Override
public CommentNode getChild(int groupPosition, int childPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
for (CommentNode node : n.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == n.getComment().getId()) {
comments.add(node);
}
}
Log.v("RedditSlide", "COMMENT CHILD SIZE:" + comments.size());
return comments.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = n;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
return comments.size();
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
final ViewGroup parent) {
final CommentNode user = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment, parent, false);
}
TextView author = (TextView) convertView.findViewById(R.id.author);
TextView comm = (TextView) convertView.findViewById(R.id.commentLine);
TextView upvote = (TextView) convertView.findViewById(R.id.upvotePost);
author.setText(user.getComment().getAuthor());
comm.setText(user.getComment().getBody());
upvote.setText(user.getComment().getScore() + "");
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = user;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
ExpandableCommentAdapter adapter = new ExpandableCommentAdapter(convertView.getContext(), comments);
ExpandableListView listView = (ExpandableListView) convertView.findViewById(R.id.commentsListUnder);
listView.setAdapter(adapter);
return convertView;
}
#Override
public CommentNode getGroup(int groupPosition) {
return itemList.get(groupPosition);
}
#Override
public int getGroupCount() {
return itemList.size();
}
#Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final CommentNode user = itemList.get(groupPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment, parent, false);
}
TextView author = (TextView) convertView.findViewById(R.id.author);
TextView comm = (TextView) convertView.findViewById(R.id.commentLine);
TextView upvote = (TextView) convertView.findViewById(R.id.upvotePost);
author.setText(user.getComment().getAuthor());
comm.setText(user.getComment().getBody());
upvote.setText(user.getComment().getScore() + "");
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
My Comment XML is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical"
android:padding="20dp"
android:paddingBottom="0dp"
android:paddingEnd="0dp"
android:scrollbars="none">
<TextView
android:id="#+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ccrama"
android:textSize="12dp" />
<TextView
android:id="#+id/commentLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COMMENT"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/upvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="#drawable/roundbutton"
android:onClick="upvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/iconupvote"
/>
<ImageView
android:id="#+id/downvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="-7dp"
android:background="#drawable/roundbutton"
android:onClick="downvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/icondownvote"
/>
<TextView
android:id="#+id/upvotePost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="7dp"
android:text="30"
android:textSize="14dp" />
</LinearLayout>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/commentsListUnder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical" />
</LinearLayout>
The issue I'm having is the "expandableness" of the child comments seems to skip a generation. Level 1, Level 3, etc all have expansion and their children are visible, but Level 2 can't expand, and only one child is visible. Further children (4, 5, 6...) aren't visible, and Level 3 has an expanding icon but when tapped shows no children.
What am I doing wrong with my layout/system?
Thanks!
Your issues are two things. One you are attempting to nest scrollable views that scroll in the same direction. Android can not handle this situation correctly and you will get all sorts of crazy behavior. Attempting to "hack" around the issues will just cause all sorts of headaches.
The second issues is that you are embedding adapters within adapters. While this "could" work, it is really not recommended. There is no guarantee to how many times a getChildView or getParentView will be called for the same position number. Sometimes it's once. Sometimes it's twice. Heck it could be 4 times. Each time you'd be recreating a new adapter that in-turn will invoke it's getChildView or getParentView methods multiple times per position. To put simply, it's a major performance concern.
Your best solution is to rework a UX design that does not entail embedding ExpandableListViews and adapters.
Related
In My expandable list view first child item has two buttons.button1 and button2.in other child list item has check box and text.these check boxes are default disabled
in first time user expand the child list and after press button1 i want to enable all check boxes in particular group
As this point i can only enable first child list item checkbox.how can enable all check boxes by clicking button on the 1st child item.
Expandable list adapter
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandableListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandableListGroup> groups) {
this.context = context;
this.groups = groups;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.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) {
final ExpandableListChild child = (ExpandableListChild) getChild(groupPosition, childPosition);
final ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.btn_start_inspection = (Button) convertView.findViewById(R.id.btn_start_inspection);
holder.btn_complete_inspection = (Button) convertView.findViewById(R.id.btn_complete_inspection);
holder.cbx_complete_inspection = (CheckBox) convertView.findViewById(R.id.check_sub_task);
holder.txt_activity_name = (TextView) convertView.findViewById(R.id.txt_sub_task_title);
holder.txt_activity_name.setText(child.getName().toString());
//button for enable checkboxes
holder.btn_start_inspection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.btn_complete_inspection.setBackgroundColor(Color.RED);
holder.itemEnabled = true;
notifyDataSetChanged();
}
});
//enable check boxes
if(holder.itemEnabled == true){
holder.cbx_complete_inspection.setEnabled(true);
}else{
holder.cbx_complete_inspection.setEnabled(false);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
#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) {
ExpandableListGroup group = (ExpandableListGroup) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.txt_company_name);
tv.setText(group.getName());
TextView tvAddress = (TextView) convertView.findViewById(R.id.txt_company_address);
tvAddress.setText(group.getAddressLevel());
iv = (ImageView) convertView.findViewById(R.id.img_expand);
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_fold));
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_min));
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class ViewHolder {
Button btn_complete_inspection;
Button btn_start_inspection;
CheckBox cbx_complete_inspection;
TextView txt_activity_name;
boolean itemEnabled = false;
}
}
listchildItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lnrChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/lnr_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<Button
android:id="#+id/btn_start_inspection"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_cornerexpandable_list_child"
android:gravity="center"
android:text="Start Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<Button
android:id="#+id/btn_complete_inspection"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_marginLeft="150dp"
android:src="#drawable/rounded_cornerexpandable_list_child_gray"
android:text="Complete Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<LinearLayout
android:id="#+id/lnr_active_sub_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/btn_complete_inspection">
<LinearLayout
android:id="#+id/lnr_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="false"
android:gravity="center"></CheckBox>
</LinearLayout>
<LinearLayout
android:id="#+id/textHolder"
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sub_task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="exx"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:id="#+id/txt_sub_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_keyboard_arrow_right_black_24dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I see is that after click button on 1st list item only 1st check box in the 1st child item is enabled, the other list item check boxes are not. What I want is that checkboxes in the child item rows are enabled. (extra lines added from comments)
I have a problem, I'm doing an android application and I want to do a list a question and when we click on one question it scroll a list of answer (in a fragment).
I did a Custom adapter and I want to add a footer after the list of answer for each question (I want to add a edittext to add an anwser).
And when I scroll the list of answer for one question it's working well with the footer but when I try to scroll a second time the same list, he say me that the textview is null...
Here the code :
-> adapter
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private LinkedList<Question> groups;
public MyExpandableAdapter(Context context, LinkedList<Question> groups)
{
this.context = context;
this.groups = groups;
}
#Override
public int getGroupCount() {
return groups.size();
}
//Add 2 to childcount. The first row and the last row are used as header and footer to childview
#Override
public int getChildrenCount(int i) {
return groups.get(i).getListAnswer().size() + 1 ;
}
#Override
public Question getGroup(int i) {
return groups.get(i);
}
#Override
public Answer getChild(int i, int i2) {
return groups.get(i).getListAnswer().get(i2);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i2) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
Question currentQuestion = groups.get(i);
if(view == null)
{
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listquestion_home,null);
}
((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setText(currentQuestion.getQuestion());
((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setChecked(true);
((TextView) view.findViewById(R.id.listrow_group_author)).setText("Anonymous" + currentQuestion.getUserId().toString());
return view;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here is the ListView of the ChildView
if (childPosition < getChildrenCount(groupPosition) - 1)
{
Answer answer = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listanswer_home, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(answer.getAnswer());
//the last row is used as footer
}
else
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.test_edit_text, null);
}
}
return convertView;
}
#Override
public boolean isChildSelectable(int i, int i2) {
return false;
}
}
-> Fragment code :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
ExpandableListView listView = (ExpandableListView) rootView.findViewById(R.id.listView);
MyExpandableAdapter adapter = new MyExpandableAdapter(getActivity(), QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe());
listView.setAdapter(adapter);
return rootView;
}
XML code :
-> Answer
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="hey"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
-> Question
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
>
<CheckedTextView
android:id="#+id/listrow_group_question"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listrow_group_author"
android:layout_below="#id/listrow_group_question"
android:gravity="right"
/>
-> Footer
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:id="#+id/txtFooter"
android:textColor="#android:color/holo_green_light"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:textStyle="bold"
android:text="Footer" />
The error is in the adapter :
text.setText(answer.getAnswer());
I really don't understand. I add one of the size of child for it but the bug appear when I do this...
If you have any idea :'( !
Thank you
Log :
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: at com.example.thibault.project_mobile.MyExpandableAdapter.getChildView(MyExpandableAdapter.java:92)
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
The error is in the adapter : text.setText(answer.getAnswer());
Because TextView with textView1 id is only available in listanswer_home.xml layout not in test_edit_text.xml and if if (childPosition < getChildrenCount(groupPosition) - 1) condition is false retuning test_edit_text.xml layout from getChildView and convertView.findViewById(R.id.textView1); return null.
Either add null check before calling setText method of TextView as:
if(text !=null){
text.setText(answer.getAnswer());
}
or add TextView with textView1 in listanswer_home.xml.
My problem is, that I have an activity with one big TextView and one ExpandableListView underneath the TextView. Unfortunately the text of the TextView is too long, so I can't see the details when I expand the ExpandableListView.
So my question is, how can I use CWAC-Merge in my Code, to get one big scrollable Activity.
I would like to have, that everything in the activity_contact.xml from scrollView1 to the bottom is scrollable! I already copied the necessary libs (merge-1.0.1.jar and sacklist-1.0.0.jar) to my Project, but I don't know how to apply the MergeAdapter. I would really appreciate, if someone could tell me where and how I have to use the MergeAdapter!
This is my activity_contact.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/titlebar" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/contact"
android:textSize="#dimen/textSize" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ExpandableListView>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is my Contact.java class:
public class Contact extends Activity {
private SparseArray<Group> groups = new SparseArray<Group>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
createData();
ExpandableListAdapter adapter = new ExpandableListAdapter(this, groups,
false, null);
listView.setAdapter(adapter);
}
private void createData() {
Group group1 = new Group(getString(R.string.stringGroup));
group1.children.add(getString(R.string.stringGroup2));
groups.append(0, group1);
}
}
This is my ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
private LayoutInflater inflater;
private Activity activity;
private boolean setOnClickListener;
private Class<?> onClickClass;
public ExpandableListAdapter(Activity act, SparseArray<Group> groups,
boolean setOnClickListener, Class<?> onClickListenerClass) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
this.setOnClickListener = setOnClickListener;
this.onClickClass = onClickListenerClass;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.copyrightTextView);
text.setText(Html.fromHtml(children));
if (setOnClickListener) {
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, onClickClass);
intent.putExtra("name", children);
activity.startActivity(intent);
}
});
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The listrow_details.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/copyrightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="15dp"
android:gravity="center_vertical"
android:linksClickable="true"
android:padding="10dp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/black" />
</LinearLayout>
MergeAdapter is a ListAdapter, not an ExpandableListAdapter. You cannot use MergeAdapter to solve your problem. You are welcome to read through the source code to MergeAdapter and attempt to figure out if there is a way to create a MergeExpandableListAdapter or something like that.
Or, come up with a UI that does not have a long TextView after the ExpandableListView.
I have an expandable listview. I have put some dummy data and evrything seems ok but when I run the application all groups are in collapse mode and they don't take effect when I click on each of them. This is the screenshot:
XML of Group is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_slider"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvRecipeName"
style="#style/normal_text.bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title" />
<ImageButton
android:id="#+id/ibDeleteRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/ic_menu_delete" />
<View
android:layout_width="1dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/tvRecipeName"
android:focusable="false" />
</RelativeLayout>
XML of child is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:gravity="left|center_vertical" >
<View
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_toLeftOf="#+id/tvIngredient"
android:focusable="false" />
<TextView
android:id="#+id/tvIngredient"
style="#style/normal_text_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="5dp"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title"
android:focusable="false" />
<ImageButton
android:id="#+id/ibDeleteIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/btn_dialog" />
</RelativeLayout>
and in main.xml, definition of expandable list is:
<ExpandableListView
android:id="#+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have an adapter that I have written this code for that:
public class ExpAdapter extends BaseExpandableListAdapter {
private final String TAG = "ExpAdapter";
private Context context;
static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{"Ponting", "Adam Gilchrist", "Michael Clarke"},
{"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
{"Graeme Smith", "AB de villiers", "Jacques Kallis"} };
public ExpAdapter(Context context) {
this.context = context;
Log.i(TAG, "Adapter created.");
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_child, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient);
ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient);
ibDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "******");
}
});
tvItem.setText(arrChildelements[groupPosition][childPosition]);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return arrGroupelements.length;
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "%%%%%%");
}
});
tvItem.setText(arrGroupelements[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
and finally, in code of fragment activity i have:
public class ShoppingList extends FragmentActivity {
ExpAdapter adapter = new ExpAdapter(this);
//Linking expnadable list view
expListView = (ExpandableListView) findViewById(R.id.elv);
expListView.setAdapter(adapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " expanded.");
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " collapsed.");
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
return false;
}
});
}
When I run the application and click on parents, nothing happens. I tried to add following lines of code at the end of shopping class:
int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
expListView.expandGroup(position - 1);
Now when I run the application result is like this screenshot:
If I click on delete buttons (parent or child), I can see they take effect (as I check logcat), however when I click on child or parent nothing happens. So, I have no idea why callbacks doesn't work. Based on my research I found that I need to set Focussable of image buttons to false. As you can see in XML files I did it but still when I click on parent and child rows I can't see any response.
Finally I found the solution :)
I have no idea is it bug or disease or ...
As mentioned above based on my research I found that we need to set focus-ability of image view/box to "false". I did it in XML file and it didn't work. I set focus-ability to "true" in XML and in code set it to false. like this:
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
ibDeleteRcipe.setFocusable(false);
tvItem.setText(arrGroupElements[groupPosition]);
return convertView;
}
Therefore based of my achievement! it should be set to "false" in code not in XML! Now the question is What is difference? -I have no idea.
It is necessary to define android:focusable="false" in the layout xml for focusable views within the expandable list view item but not in the "ExpandableListView" attribute.
Eg: if a custom parent list item includes a checkbox (it will automatically get the focus) it should be like below.
<CheckBox
android:id="#+id/cbkSelectDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
transferAvailPowered.axml
<?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"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="left">
<TextView
android:id="#+id/availSerial"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:focusable="false"
android:padding="5dp"
android:gravity="left" />
<TextView
android:id="#+id/availModel"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="false"
android:padding="5dp"
android:gravity="left" />
<AutoCompleteTextView
android:id="#+id/availSite"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:hint="To Site"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:textCursorDrawable="#null"
android:focusable="false"
android:layout_margin="5dp"
android:gravity="left" />
<ImageButton
android:id="#+id/addToTransfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#drawable/addsmall"
android:focusable="false"
android:gravity="left" />
transferAvailAttached.axml
<?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"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp">
<TextView
android:id="#+id/availSerial"
android:layout_width="0dp"
android:layout_weight=".30"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="right" />
<TextView
android:id="#+id/availModel"
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="center" />
<CheckBox
android:id="#+id/include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:focusable="false"
android:layout_marginRight="30dp"
android:background="#drawable/bg_checkbox" />
<ImageButton
android:id="#+id/removeAttachment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="5dp"
android:layout_margin="5dp"
android:background="#drawable/deletesmall"
android:gravity="right" />
Adapter
class EquipAdapter : BaseExpandableListAdapter
{
private List<CPEquipment> Parent { get; set; }
private List<List<CPEquipment>> Child { get; set; }
private Context _context { get; set; }
private IListAdapter _adapter { get; set; }
private ExpandableListView _list { get; set; }
public EquipAdapter(Context context, List<CPEquipment> parent, List<List<CPEquipment>> child, IListAdapter adapter, ExpandableListView list)
{
_context = context;
Parent = parent;
Child = child;
_adapter = adapter;
_list = list;
}
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
List<CPEquipment> level1 = Child.ElementAt(groupPosition);
CPEquipment level2 = level1.ElementAt(childPosition);
E e = new E() {Serial = level2.Serial, Model = level2.Model};
return e;
}
public override long GetChildId(int groupPosition, int childPosition)
{
return Convert.ToInt32(groupPosition.ToString(CultureInfo.InvariantCulture) + childPosition.ToString(CultureInfo.InvariantCulture));
}
public override int GetChildrenCount(int groupPosition)
{
return Child.ElementAt(groupPosition).Count;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);
}
E e = (E)GetChild(groupPosition, childPosition);
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
return convertView;
}
public override Object GetGroup(int groupPosition)
{
CPEquipment c = Parent.ElementAt(groupPosition);
E e = new E(){Serial = c.Serial, Model = c.Model, Type = c.Status};
return e;
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
E e = (E)GetGroup(groupPosition);
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
}
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
acText.Adapter = _adapter;
_list.ExpandGroup(groupPosition);
return convertView;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public override int GroupCount
{
get { return Parent.Count; }
}
public override bool HasStableIds
{
get { return true; }
}
}
Outcome:
In this scenario, the AutoCompleteTextView in the parent group and the green plus button should be "selectable" so that the user can input the info into the field and click that button without collapsing the group. And the CheckBox and the red x button in the child should also be "selectable" so the user can check the CheckBox and click the button. The only part of that that actually works is that the CheckBox is selectable and the group doesn't collapse because for lack of a better term, the group layout is "dead" and does nothing when pressed. And the seemingly "working" CheckBox isn't even so because it does this weird thing where checking or unchecking one will randomly check or uncheck others.
Below code is to resolve checkbox problem "CheckBox isn't even so because it does this weird thing where checking or unchecking one will randomly check or uncheck others." Along with ImageButton and CheckBox handling.
public override View GetChildView(final int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailAttached, null);
E e = (E)GetChild(groupPosition, childPosition);
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
CheckBox include = (CheckBox)convertView.FindViewById(Resource.Id.include);
include.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//DO your checkbox handling here
}
});
ImageButton removeAttachment =(CheckBox)convertView.FindViewById(Resource.Id. removeAttachment);
removeAttachment.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//DO your imageButton handling here
}
});
return convertView;
}
public override View GetGroupView(final int groupPosition, bool isExpanded, View convertview, ViewGroup parent)
{
View convertView = convertview;
if (convertView == null)
{
E e = (E)GetGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.transferAvailPowered, null);
}
TextView serial = (TextView)convertView.FindViewById(Resource.Id.availSerial);
serial.Text = e.Serial;
TextView model = (TextView)convertView.FindViewById(Resource.Id.availModel);
model.Text = e.Model;
AutoCompleteTextView acText = (AutoCompleteTextView)convertView.FindViewById(Resource.Id.availSite);
acText.Adapter = _adapter;
ImageButton addToTransfer =(CheckBox)convertView.FindViewById(Resource.Id. addToTransfer);
addToTransfer.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//DO your addToTransfer imageButton handling here
}
});
_list.ExpandGroup(groupPosition);
return convertView;
}