can we have ExpandableListView inside ListView so that I can have group level first and then child items. Only ExpandableListView is group and child, I want to have an ExpandableListView Inside ListView. Tried with open source TreeView but could not get specifically all those features of ExpandableListView. Looking forward to your reply. thanks.
ExpandableListView本身就是ListView,你不能在ListView里面使用ExpandableListView ...
But you can achieve the need of functionality of the same without the ListView. Here is the code for the same.
ExpandableListView adapter code:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
/**
* Holds the mapping details
*/
Mapdetail mMapDetials = new Mapdetail();
/**
* Holds the contest
*/
private Context mContext;
/**
* Holds tehe xpandable list VIew
*/
private ExpandableListView mExpandableListView;
/**
* Holds the details for the cards
*/
private List<com.sourcebits.sfc.activity.GroupEntity> mGroupCollection;
/**
* Holds the status whether its expanded or not
*/
private int[] groupStatus;
public ExpandableListAdapter(Context pContext,
ExpandableListView pExpandableListView,
List<com.sourcebits.sfc.activity.GroupEntity> mGroupCollection2) {
mContext = pContext;
mGroupCollection = mGroupCollection2;
mExpandableListView = pExpandableListView;
groupStatus = new int[mGroupCollection.size()];
setListEvent();
}
/**
* Sets the event listeners
*/
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int arg0) {
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int arg0) {
groupStatus[arg0] = 0;
}
});
}
@Override
public String getChild(int arg0, int arg1) {
return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
@Override
public long getChildId(int arg0, int arg1) {
return 0;
}
@Override
public View getChildView(final int arg0, final int arg1, boolean arg2,
View arg3, ViewGroup arg4) {
ChildHolder childHolder;
if (arg3 == null) {
arg3 = LayoutInflater.from(mContext).inflate(
R.layout.list_group_item, null);
childHolder = new ChildHolder();
childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
childHolder.background = (ImageView) arg3
.findViewById(R.id.background);
arg3.setTag(childHolder);
} else {
childHolder = (ChildHolder) arg3.getTag();
}
if (arg1 == 0) {
childHolder.background.setBackgroundResource(R.drawable.list_top);
} else {
childHolder.background
.setBackgroundResource(R.drawable.list_bottom);
}
childHolder.title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mMapDetials.setmLanguageIndex(mGroupCollection.get(arg0).GroupItemCollection
.get(arg1).Name);
startNextActivity();
}
});
childHolder.title
.setText(mGroupCollection.get(arg0).GroupItemCollection
.get(arg1).Name);
return arg3;
}
@Override
public int getChildrenCount(int arg0) {
return mGroupCollection.get(arg0).GroupItemCollection.size();
}
@Override
public Object getGroup(int arg0) {
return mGroupCollection.get(arg0);
}
@Override
public int getGroupCount() {
return mGroupCollection.size();
}
@Override
public long getGroupId(int arg0) {
return arg0;
}
@Override
public View getGroupView(final int arg0, boolean arg1, View arg2,
ViewGroup arg3) {
GroupHolder groupHolder;
if (arg2 == null) {
arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
null);
groupHolder = new GroupHolder();
groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
arg2.setTag(groupHolder);
} else {
groupHolder = (GroupHolder) arg2.getTag();
}
if (groupStatus[arg0] == 0) {
groupHolder.img.setImageResource(R.drawable.icon_expand);
} else {
mMapDetials.setTrainingPointIndex(mGroupCollection.get(arg0).Name);
groupHolder.img.setImageResource(R.drawable.icon_collapse);
}
groupHolder.img.setTag("NotClicked");
if (mGroupCollection.get(arg0).GroupItemCollection.size() == 0) {
groupHolder.img.setImageResource(R.drawable.arrow);
mMapDetials.setmLanguageIndex("English");
groupHolder.img.setTag("Clicked");
groupHolder.img.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getTag().toString().equals("Clicked")) {
mMapDetials.setTrainingPointIndex(mGroupCollection
.get(arg0).Name);
startNextActivity();
return false;
}
return false;
}
});
}
groupHolder.title.setText(mGroupCollection.get(arg0).Name);
return arg2;
}
/**
* Holds the view of the parent
*
* @author Gautam Balasubramanian
*
*/
class GroupHolder {
ImageView img;
TextView title;
}
/**
* ChildHolder class view of the child view
*
* @author Gautam Balasubramanian
*
*/
class ChildHolder {
TextView title;
ImageView background;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
void startNextActivity() {
Intent intent = new Intent(mContext, GeneralInfo.class);
mMapDetials.toString();
Bundle extras = new Bundle();
extras.putSerializable("values", mMapDetials);
intent.putExtras(extras);
mContext.startActivity(intent);
}
}
Code to call from the activty:
private void initPage() {
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
mExpandableListView.setCacheColorHint(0);
ExpandableListAdapterforProducts adapter = new ExpandableListAdapterforProducts(
Products.this, mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
ColorDrawable sage = new ColorDrawable(this.getResources().getColor(
R.color.sage));
mExpandableListView.setDivider(sage);
mExpandableListView.setDividerHeight(1);
}
Initailisation of values,
mySQLiteAdapter = new SqliteAdapterForProducts(this);
mySQLiteAdapter.openToRead();
mCursor = mySQLiteAdapter.queueProducts();
Log.v("", mCursor.getCount() + "is the count");
mCursor.moveToFirst();
mGroupCollection = new ArrayList<GroupEntity>();
for (int i = 0; i < mCursor.getCount(); i++) {
GroupEntity groupEntity = new GroupEntity();
groupEntity.Name = mCursor.getString(1) + " "
+ mCursor.getString(2);
String languages[] = mCursor.getString(3).toString().split(",");
if (languages.length > 1) {
for (int j = 0; j < languages.length; j++) {
GroupItemEntity groupItemEntity = groupEntity.new GroupItemEntity();
groupItemEntity.Name = languages[j];
groupEntity.GroupItemCollection.add(groupItemEntity);
}
}
mCursor.moveToNext();
mGroupCollection.add(groupEntity);
}
mCursor.close();
mySQLiteAdapter.close();
The above stuff worked for me. You can try it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.