Android - Handling ExpandableListView height - android

I'm implementing an ExpandableListView within a LinearLayout, but when I run the app, I get this:
Firstly, is this normal that the header appears twice?
When I click on the upper header, I get the expanded view which is how I want it to be:
If I touch the second header (that, in my opinion, is not supposed to exist), it disppears, letting the upper header alone (like I want it to be):
BUT the expandable height has changed!:
I don't know why this happens. I'd like to have just one header, and I want the size to be fixed. Here are the files with the relevant code:
The layout where the ExpandableListView is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" >
...
<ExpandableListView
android:id="#+id/learnabletm"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</LinearLayout>
expandable_list_view_child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/child"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
expandable_list_view_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="#+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="18dp" />
</LinearLayout>
ExpAdapter.java
public class ExpAdapter extends BaseExpandableListAdapter {
public static final String ARG_POS = "pos";
private Context myContext;
Bundle data;
publicExpAdapter(Context context, Bundle data) {
myContext = context;
this.data = data;
}
#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) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_view_child, null);
}
String text = "";
for(int i = 0; i < bytes.length; i++){
for(int j = 0; j < 8; j++){
text = text + " " + (i*8+j+1) + "\n";
}
}
TextView child = (TextView) convertView.findViewById(R.id.child);
child.setText(text);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return 1;
}
#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) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_list_view_group, null);
}
TextView group = (TextView) convertView.findViewById(R.id.group);
group.setText("Numbers");
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
And finally the fragment where the ExpandableListView is called:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
expList = (ExpandableListView) rootView.findViewById(R.id.learnabletm);
width = args.getInt(ARG_WIDTH);
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(getActivity(), data));
expList.expandGroup(ExpandableListView.PACKED_POSITION_TYPE_CHILD);
expList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener(){
#Override
public void onGroupExpand(int groupPosition){
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
param.height = expList.getHeight();
expList.setLayoutParams(param);
expList.refreshDrawableState();
}
});
expList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener(){
#Override
public void onGroupCollapse(int groupPosition){
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) expList.getLayoutParams();
param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
expList.setLayoutParams(param);
expList.refreshDrawableState();
}
});
expList.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id){
Log.d("OnChildClickListener", "OK");
return false;
}
});
}

Related

I want to know how to properly use adapters that connect Expandable List View and adapters that connect gridView at the same time

What I want to implement is putting a GridView in the Expandable List.
Currently, my code has two adapters called "CustomListAdapter" that connects the GridView to the Expandable List and "SiteAdapter" which connects the data to the GridView.
The code that we have built so far is well connected to the Expandable List to the GridView. However, the data inside the gridView seems to be connected to the gridView (seeing the items in the gridview and clicking on the appropriate link), but the image and text that make up the gridview's internal design do not appear on the screen.
I have used setImage and setText inside the adapter code called "siteAdapter", but I do not know why it is not configured properly on the screen.
1] SiteAdapter.java (the siteAdapter code that links the GridView and the items)
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
public SiteAdapter(Context context, ArrayList<Site> sites) {
super(context, 0, sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
currentSite = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setBackground(new ShapeDrawable(new OvalShape()));
imageView.setClipToOutline(true);
imageView.setImageBitmap(bitmap);
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentSite.getMsite_name());
return listItemView;
}
2] CustomListAdapter.java (links the ExpandableList and the GridView)
public class CustomListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<String> arrayGroup;
private HashMap<String,ArrayList<Site>> arrayChild;
public CustomListAdapter(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<Site>> arrayChild){
super();
this.mContext = context;
this.arrayGroup = arrayGroup;
this.arrayChild = arrayChild;
}
#Override
public int getGroupCount() {
return arrayGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return arrayChild.get(arrayGroup.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return arrayGroup.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition)
{
return arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.parent_listview_title, null);
}
TextView textGroup = (TextView) v.findViewById(R.id.title_site_list);
textGroup.setText(groupName);
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.child_gridview_item, null);
}
SiteAdapter siteAdapter = new SiteAdapter(mContext,arrayChild.get(groupName));
GridView gridView = (GridView) v.findViewById(R.id.gridView);
gridView.setAdapter(siteAdapter);
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
3] site_list.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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/parentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
4] parent_listview_title.xml(Code that designed the parent list of the Expandable List)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_site_list"
android:layout_width="wrap_content"
android:textSize="40sp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:textColor="#000000"
android:layout_marginLeft="16dp"
android:fontFamily="#font/bm_dohyeon"
android:textStyle="bold"/>
</LinearLayout>
5] child_gridview_item.xml(Code that designed the child list of the Expandable List)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ViewSwitcher
android:id="#+id/visibility"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="20dp">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#FFFFFF"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
tools:context=".home"
android:id="#+id/gridView"
android:columnWidth="96dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</ViewSwitcher>
</LinearLayout>
6] list_item.xml(The code that designs the contents of the gridview, the child list of the Expandable List)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/image"
android:scaleType="centerCrop"
tools:src="#mipmap/ic_launcher"
android:layout_gravity="center"
/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:id="#+id/name"
tools:text="Gistsite"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textSize="4sp"
/>
</LinearLayout>
7] Site.java
public class Site {
private String msite_name;
private String msite_url;
private String msite_urlF;
private String msite_urlY;
private String msite_urlB;
private String msite_urlW;
private String msite_urlI;
private int msite_imagesource;
public Site(String name, String url, int imagesource){
msite_name = name;
msite_url = url;
msite_imagesource = imagesource;
}
public Site(String name, String urlf, String urly, int imagesource){
msite_name = name;
msite_urlF = urlf;
msite_urlY = urly;
msite_imagesource = imagesource;
}
public Site(String name, String urlw, String urlb, String urlf, String urli,int imagesource){
msite_name = name;
msite_urlW = urlw;
msite_urlB = urlb;
msite_urlF = urlf;
msite_urlI = urli;
msite_imagesource = imagesource;
}
public String getMsite_name() {
return this.msite_name;
}
public String getMsite_url(){
return this.msite_url;
}
public String getMsite_urlW(){
return this.msite_urlW;
}
public String getMsite_urlB(){ return this.msite_urlB; }
public String getMsite_urlF(){
return this.msite_urlF;
}
public String getMsite_urlY(){
return this.msite_urlY;
}
public String getMsite_urlI(){
return this.msite_urlI;
}
public int getMsite_imagesource(){
return this.msite_imagesource;
}
In summary, the problem is that the contents of the image view and text view designed in list_item.xml are not available.

ExpandableListView is not expanding

I m creating ExpandableList view in Fragment, the Group item display in list, but when I touch the group item, it is not expanding.
Below is my fragment
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
below is adapter
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.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 String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
#Override
public int getGroupCount() {
return this.subjectName.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
below is institute_fragment.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="#+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
below is my groupview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/_10sdp"
android:layout_margin="#dimen/_5sdp"
android:background="#android:color/white"
android:id="#+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
below is childView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_5sdp"
android:background="#color/colorPrimary"
android:id="#+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Well, easy one.
You have imagebutton in groupview that is intercepting touch event. Add these parameters:
android:focusable="false"
android:focusableInTouchMode="false"
And do same for childview. You have exactly the same case there.
P.S. why are you using imagebuttons anyway? you could use imageviews instead
You need to do this one.
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
Hope this will help you.
Try to pass your ExpandableListView in the adapter:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
Then youe adapter should look like:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
In your group view add following:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});

add a static editText view as the last child of a expandableListView Android

I have a problem to add dynamically a static EditText, always in the last children position. My adapter uses JSON objects of type. The first time that I expand the ExpandableListView group's children, can locate the correct position for my EditText, but when I scroll the children in the opposite direction, my EditText continually changes position. This is my Editext layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:background="#color/lgray">
<RelativeLayout android:id="#+id/feedcell_addcomment" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="5dip" android:paddingBottom="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="1dip" android:background="#color/mlgray" >
</RelativeLayout>
</LinearLayout>
This is my childLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/layoutchildren" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="vertical" android:paddingLeft="10dip" android:paddingRight="10dip" android:background="#color/lgray">
<RelativeLayout android:paddingLeft="10dip" android:paddingRight="10dip" android:background="#color/white" android:layout_height="45dip" android:layout_width="fill_parent" android:layout_marginBottom="2dip">
<ImageView android:id="#+id/feedcommentcell_profileimg" android:layout_width="32dip" android:layout_height="32dip" android:layout_centerVertical="true" android:scaleType="fitXY"/>
<TextView android:id="#+id/feedcommentcell_username" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" android:textColor="#color/black" android:layout_toRightOf="#+id/feedcommentcell_profileimg" android:layout_marginLeft="10dip" android:layout_alignTop="#+id/feedcommentcell_profileimg"/>
<TextView android:id="#+id/feedcommentcell_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11sp" android:textColor="#color/black" android:layout_toRightOf="#+id/feedcommentcell_profileimg" android:layout_marginLeft="10dip" android:layout_below="#+id/feedcommentcell_username"/>
<TextView android:id="#+id/feedcommentcell_scanhour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:textColor="#color/dgray" android:layout_alignParentRight="true" android:layout_alignBottom="#+id/feedcommentcell_username"/>
</RelativeLayout>
</LinearLayout>
this is my getChildView in my adapter
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent)
{
int type=getItemViewType(groupPosition,childPosition);
View v = convertView;
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (v == null)
{
switch(type)
{
case TYPE_CHILD:
v = vi.inflate(R.layout.feedcommentcell, null);
break;
case TYPE_EDIT:
v = vi.inflate(R.layout.edittextcommentlist, null);
break;
}
}
try
{
if(type == TYPE_CHILD)
{
JSONObject o = getChild(groupPosition,childPosition);
//JSONObject o = itemsChildren.get(childPosition);
if(o!=null)
{
TextView commentUser=(TextView)v.findViewById(R.id.feedcommentcell_username);
commentUser.setText(o.getString("username"));
String shareMessageId= o.getString("ShareMessageId");
o.put("ShareMessageId", shareMessageId);
TextView commentText=(TextView)v.findViewById(R.id.feedcommentcell_comment);
commentText.setText(o.getString("Message"));
TextView commentScanHour=(TextView)v.findViewById(R.id.feedcommentcell_scanhour);
commentScanHour.setText(o.getString("timestamp"));
ImageView commentImg=(ImageView)v.findViewById(R.id.feedcommentcell_profileimg);
String imgurl=o.getString("userimage");
if(imgurl!=null && imgurl!="")
imageLoader.DisplayImage(imgurl, context, commentImg);
}
}
else if(type==TYPE_EDIT)
{
//JSONObject o = getChild(groupPosition,childPosition);
JSONObject o = itemsChildren.get(childPosition);
o.put("edit", "false");
v = vi.inflate(R.layout.edittextcommentlist, null);
//EditText edit = (EditText) v.findViewById(R.id.feedcell_edittxt);
}
}
catch(Exception e)
{}
return v;
}
And these are the others overrides methods. I don't show you getGroupView and his layout because works properly..
#Override
public JSONObject getChild(int groupPosition, int childPosition) {
return itemsChildren.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean areAllItemsEnabled()
{
return true;
}
#Override
public int getChildrenCount(int groupPosition) {
return itemsChildren.size();
}
#Override
public JSONObject getGroup(int groupPosition) {
return itemsGroup.get(groupPosition);
}
#Override
public int getGroupCount() {
return itemsGroup.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
public boolean isTextView(int groupPosition,int childPosition)
{
JSONObject jo=new JSONObject();
jo = getChild(groupPosition,childPosition);
boolean result =false;
try
{
String isEdit=jo.getString("edit");
if(isEdit.equalsIgnoreCase("true"))
result=true;
}
catch(Exception e){}
return result;
}
public int getItemViewType(int groupPosition,int childPosition)
{
//JSONObject jo=getChild(groupPosition,childPosition);
if(isTextView(groupPosition,childPosition))
return TYPE_EDIT;
else
return TYPE_CHILD;
}
All seems good, but editText changes the position every time...someone has some solutions?Thanks
I have the same problem with my ExpandableList... The main problem here is with lastChild property. You see, on creation, the last child of the group list is the one we think it is, but on scroll opposite, your list is refreshing so next last child is next which you can't see on opposite, or if you go back down, last one you can't see. This is pretty much stupid if you ask me but there is no right solution for this...
Since isLastChild does not work properly, you can do the following:
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
...
if (childPosition == itemsGroup.get(groupPosition).size() - 1) {
// This gives you confirmation that is the last child in the group
}
}

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

I am using Expandable ListView example found on net
Activity:
public class ExpandableListViewActivity extends ExpandableListActivity {
/**
* strings for group elements
*/
static final String arrGroupelements[] = { "India", "Australia", "England",
"South Africa" };
/**
* strings for child elements
*/
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" } };
DisplayMetrics metrics;
int width;
ExpandableListView expList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
expList = getExpandableListView();
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
// this code for adjusting the group indicator into right side of the
// view
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width
- GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(this));
expList.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
Log.e("onGroupExpand", "OK");
}
});
expList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
public void onGroupCollapse(int groupPosition) {
Log.e("onGroupCollapse", "OK");
}
});
expList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.e("OnChildClickListener", "OK");
return false;
}
});
}
public int GetDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
Adapter:
public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpAdapter(Context context) {
myContext = context;
}
public Object getChild(int groupPosition, int childPosition) {
return null;
}
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView tvPlayerName = (TextView) convertView
.findViewById(R.id.tvPlayerName);
tvPlayerName
.setText(ExpandableListViewActivity.arrChildelements[groupPosition][childPosition]);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return ExpandableListViewActivity.arrChildelements[groupPosition].length;
}
public Object getGroup(int groupPosition) {
return null;
}
public int getGroupCount() {
return ExpandableListViewActivity.arrGroupelements.length;
}
public long getGroupId(int groupPosition) {
return 0;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView tvGroupName = (TextView) convertView
.findViewById(R.id.tvGroupName);
tvGroupName
.setText(ExpandableListViewActivity.arrGroupelements[groupPosition]);
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="#drawable/group_indicator" >
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Items" >
</TextView>
</ExpandableListView>
</LinearLayout>
I tried this solution, but didn't work :(
Android: Custom ListAdapter extending BaseAdapter crashes on application launch
there its told to add a third parameter "false", to the inflater.inflate(R.layout.group_row, null, false);
Move the TextView outside the ExpandableListView element:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="#drawable/group_indicator" >
</ExpandableListView>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No Items" >
</TextView>
</LinearLayout>
Subclasses of AdapterView(like ExpandableListView) can't have children in a xml layout(like you did in your layout).
If you're like me and found this question without using ExpandableListView, try using adding the third parameter to the inflate method, as #Luksprog mentions, also mentioned here:
inflater.inflate(R.layout.group_row, parent, false);
Don't use true instead of false or you will get the same error that brought you here, because it's trying to attach/add it to the parent, hence the addView is not supported in AdapterView error.

how to customize views of ExpandableList

I need an ExpandableList in my Android app. By extending ExpandableListActivity whose content view is as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/tv_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/txt_add" />
</LinearLayout>
and extending BaseExpandableListAdapter I am now able to display data of groups and children in a TextView.
However, I want to customize the view of children. How may I be able to do so through another xml file?
EDIT:
Here's my row.xml for the view of children:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
/>
<TextView
android:id="#+id/description"
android:textSize="10sp"
android:textStyle="normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
/>
</LinearLayout>
Create two layout xml files for Group and Child views respectively -
For example, *group_layout.xml* and *child_layout.xml*
These layouts are inflated and used in the custom ExpandableListAdapter as shown below.
You can customize the Adapter class and set that adapter to the ExpandableListView.
public class SampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
ExpandableListAdapter adapter = new ExpandableListAdapter(this, new ArrayList<String>(), new ArrayList<ArrayList<Vehicle>>());
// Set this adapter to the list view
listView.setAdapter(adapter);
}
}
Custom Adapter class can be created as shown below:
class ExpandableListAdapter extends BaseExpandableListAdapter {
public ExpandableListAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<Vehicle>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// Return a child view. You can load your custom layout here.
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + vehicle.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).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;
}
// Return a group view. You can load your custom layout here.
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
Hope this helps you.
Thx to PRC, but according to my row.xml, the correct version of getChildView would be
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
LinearLayout layout;
Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) infalInflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + vehicle.getName());
return layout;
}

Categories

Resources