下面列出了怎么用android.widget.ExpandableListAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 可扩展listview展开时调用
*
* @param listView
* @param groupPosition
*/
public static void setExpandedListViewHeightBasedOnChildren(
ExpandableListView listView, int groupPosition) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
if (listAdapter == null) {
return;
}
View listItem = listAdapter.getChildView(groupPosition, 0, true, null,
listView);
listItem.measure(0, 0);
int appendHeight = 0;
for (int i = 0; i < listAdapter.getChildrenCount(groupPosition); i++) {
appendHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
// Log.d(TAG, "Expand params.height" + params.height);
params.height += appendHeight;
listView.setLayoutParams(params);
}
/**
* 可扩展listview收起时调用
*
* @param listView
* @param groupPosition
*/
public static void setCollapseListViewHeightBasedOnChildren(
ExpandableListView listView, int groupPosition) {
ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
if (listAdapter == null) {
return;
}
View listItem = listAdapter.getChildView(groupPosition, 0, true, null,
listView);
listItem.measure(0, 0);
int appendHeight = 0;
for (int i = 0; i < listAdapter.getChildrenCount(groupPosition); i++) {
appendHeight += listItem.getMeasuredHeight();
}
/*Log.d(TAG,
"Collapse childCount="
+ listAdapter.getChildrenCount(groupPosition));*/
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height -= appendHeight;
listView.setLayoutParams(params);
}
private ArrayList<Long> getExpandedIds() {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
int length = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for(int i=0; i < length; i++) {
if(list.isGroupExpanded(i)) {
expandedIds.add(adapter.getGroupId(i));
}
}
return expandedIds;
} else {
return null;
}
}
private ArrayList<Long> getExpandedIds() {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
int length = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for(int i=0; i < length; i++) {
if(list.isGroupExpanded(i)) {
expandedIds.add(adapter.getGroupId(i));
}
}
return expandedIds;
} else {
return null;
}
}
private ArrayList<Long> getExpandedIds() {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
int length = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for(int i=0; i < length; i++) {
if(list.isGroupExpanded(i)) {
expandedIds.add(adapter.getGroupId(i));
}
}
return expandedIds;
} else {
return null;
}
}
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results_expandablelistview);
Gson gson = new Gson();
LinkedHashMap<String, List<String>> groupedStatistics = gson
.fromJson(results, new ResultsActivity.LinkedHashMapTypeToken().getType());
// create expandable list view
ExpandableListView expandableListView = findViewById(R.id.expandableListView);
ExpandableListAdapter expandableListAdapter = new ExpandableListViewAdapter(this, groupedStatistics);
expandableListView.setAdapter(expandableListAdapter);
// expand the first group by default
expandableListView.expandGroup(0);
}
public void expandGroups(long[] groupsToExpand)
{
// this.expandedIds = expandedIds;
if (groupsToExpand != null && groupsToExpand.length > 0)
{
ExpandableListAdapter adapter = getExpandableListAdapter();
if (adapter != null)
{
for (int i = 0; i < adapter.getGroupCount(); i++)
{
long id = adapter.getGroupId(i);
if (inArray(groupsToExpand, id))
{
expandGroup(i);
}
}
}
}
}
private void selectChildView(ExpandableListView expandLV, int groupPosition, int childPosition, boolean force)
{
if (groupPosition < mAdapter.getGroupCount() && childPosition < mAdapter.getChildrenCount(groupPosition))
{
// a task instance element has been clicked, get it's instance id and notify the activity
ExpandableListAdapter listAdapter = expandLV.getExpandableListAdapter();
Cursor cursor = (Cursor) listAdapter.getChild(groupPosition, childPosition);
if (cursor == null)
{
return;
}
Uri taskUri = ContentUris.withAppendedId(Instances.getContentUri(mAuthority), (long) TaskFieldAdapters.TASK_ID.get(cursor));
Color taskListColor = new ValueColor(TaskFieldAdapters.LIST_COLOR.get(cursor));
mCallbacks.onItemSelected(taskUri, taskListColor, force, mInstancePosition);
}
}
/**
* Provide the adapter for the expandable list.
*/
public void setListAdapter(ExpandableListAdapter adapter) {
synchronized (this) {
ensureList();
mAdapter = adapter;
mList.setAdapter(adapter);
}
}
/**
* @see ExpandableListView#setAdapter(ExpandableListAdapter)
*/
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
// Make sure that the adapter extends AnimatedExpandableListAdapter
if(adapter instanceof AnimatedExpandableListAdapter) {
this.adapter = (AnimatedExpandableListAdapter) adapter;
this.adapter.setParent(this);
} else {
throw new ClassCastException(adapter.toString() + " must implement AnimatedExpandableListAdapter");
}
}
private void restoreExpandedState(ArrayList<Long> expandedIds) {
this.expandedIds = expandedIds;
if (expandedIds != null) {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
for (int i=0; i<adapter.getGroupCount(); i++) {
long id = adapter.getGroupId(i);
if (expandedIds.contains(id)) list.expandGroup(i);
}
}
}
}
private void restoreExpandedState(ArrayList<Long> expandedIds) {
this.expandedIds = expandedIds;
if (expandedIds != null) {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
for (int i=0; i<adapter.getGroupCount(); i++) {
long id = adapter.getGroupId(i);
if (expandedIds.contains(id)) list.expandGroup(i);
}
}
}
}
private void restoreExpandedState(ArrayList<Long> expandedIds) {
this.expandedIds = expandedIds;
if (expandedIds != null) {
ExpandableListView list = mList;
ExpandableListAdapter adapter = mAdapter;
if (adapter != null) {
for (int i=0; i<adapter.getGroupCount(); i++) {
long id = adapter.getGroupId(i);
if (expandedIds.contains(id)) list.expandGroup(i);
}
}
}
}
/**
* @see ExpandableListView#setAdapter(ExpandableListAdapter)
*/
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
// Make sure that the adapter extends AnimatedExpandableListAdapter
if (adapter instanceof AnimatedExpandableListAdapter) {
this.adapter = (AnimatedExpandableListAdapter) adapter;
this.adapter.setParent(this);
} else {
throw new ClassCastException(adapter.toString() + " must implement AnimatedExpandableListAdapter");
}
}
/**
* Provide the adapter for the expandable list.
*/
public void setListAdapter(final ExpandableListAdapter adapter) {
synchronized (this) {
ensureList();
this.adapter = adapter;
list.setAdapter(adapter);
}
}
private void inflateExpandableListView() {
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_quick_start));
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_simple_upload));
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_advanced_upload));
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_audio_video_play));
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_image_view));
this.exampleGroupTitleList.add(this
.getString(R.string.qiniu_system_capture));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_quick_start_demo)));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_simple_upload_values)));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_advanced_upload_values)));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_video_play)));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_image_view)));
this.exampleItemTitleList.add(Arrays.asList(this.getResources()
.getStringArray(R.array.qiniu_system_capture)));
ExpandableListAdapter exampleListViewAdapter = new ExampleExpandableListAdapter(
this, this.exampleGroupTitleList, this.exampleItemTitleList);
this.exampleListView.setAdapter(exampleListViewAdapter);
OnExampleItemClickListener onExampleItemClickListener = new OnExampleItemClickListener(
this);
this.exampleListView
.setOnChildClickListener(onExampleItemClickListener);
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
if(adapter instanceof ITreeViewHeaderUpdater) {
mUpdater = (ITreeViewHeaderUpdater) adapter;
} else {
throw new IllegalArgumentException("The adapter must instanceof ITreeViewHeaderUpdater.");
}
}
/**
* @see ExpandableListView#setAdapter(ExpandableListAdapter)
*/
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
// Make sure that the adapter extends AnimatedExpandableListAdapter
if(adapter instanceof AnimatedExpandableListAdapter) {
this.adapter = (AnimatedExpandableListAdapter) adapter;
this.adapter.setParent(this);
} else {
throw new ClassCastException(adapter.toString() + " must implement AnimatedExpandableListAdapter");
}
}
/**
* @see ExpandableListView#setAdapter(ExpandableListAdapter)
*/
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
// Make sure that the adapter extends AnimatedExpandableListAdapter
if (adapter instanceof AnimatedExpandableListAdapter) {
this.adapter = (AnimatedExpandableListAdapter) adapter;
this.adapter.setParent(this);
} else {
throw new ClassCastException(adapter.toString() + " must implement AnimatedExpandableListAdapter");
}
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
if (adapter instanceof AnimatedExpandableListAdapter) {
this.adapter = (AnimatedExpandableListAdapter) adapter;
} else {
throw new ClassCastException(adapter.toString() + " must implement AnimatedExpandableListAdapter");
}
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
if(!(adapter instanceof WrapperExpandableListAdapter)) {
throw new IllegalArgumentException("The adapter must be an instance of WrapperExpandableListAdapter");
}
setAdapter((WrapperExpandableListAdapter) adapter);
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
if (!(adapter instanceof BaseSectionAdapter)) {
System.err.print("SectionListView need BaseSectionAdapter");
return;
}
super.setAdapter(adapter);
mAdapter = (BaseSectionAdapter) adapter;
if (mAlwaysExpanded) {
openGroup();
fixOpenedGroup();
}
}
public long[] getExpandedGroups()
{
ExpandableListAdapter adapter = this.getExpandableListAdapter();
int count = adapter.getGroupCount();
ArrayList<Long> expandedIds = new ArrayList<Long>();
for (int i = 0; i < count; i++)
{
if (this.isGroupExpanded(i))
{
expandedIds.add(adapter.getGroupId(i));
}
}
return toLongArray(expandedIds);
}
public ExpandableListViewAssert hasExpandableListAdapter(ExpandableListAdapter adapter) {
isNotNull();
ExpandableListAdapter actualAdapter = actual.getExpandableListAdapter();
assertThat(actualAdapter) //
.overridingErrorMessage("Expected expandable list adapter <%s> but was <%s>.", adapter,
actualAdapter) //
.isSameAs(adapter);
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
final String lang = Locale.getDefault().getLanguage();
if (translations.contains(lang)) {
wikiLangSubDomain = lang + ".";
}
recogs = getIntent().getStringArrayListExtra("recogs");
expListView = findViewById(R.id.lvExp);
prepareListData();
final ExpandableListAdapter listAdapter = new ListAdapter(this, listDataHeader, listDataChild);
expListView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
final String title = listDataHeader.get(groupPosition);
final String searchText = title.replace(" ", "+");
final DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
final String url = String.format("https://%swikipedia.org/w/index.php?search=%s&title=Special:Search", wikiLangSubDomain, searchText);
final Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
};
(new AlertDialog.Builder(this))
.setMessage(R.string.searchFor).setTitle(title)
.setNegativeButton(R.string.no, dialogClickListener)
.setPositiveButton(R.string.yes, dialogClickListener).show();
return false;
});
expListView.setFastScrollEnabled(true);
expListView.setAdapter(listAdapter);
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
}
public void setAdapter(ExpandableListAdapter adapter) {
((ExpandableListView) this.mRootView).setAdapter(adapter);
}
@Override
public void setAdapter(ExpandableListAdapter adapter) {
super.setAdapter(adapter);
mAdapter = (IphoneTreeHeaderAdapter) adapter;
}
public void configureHeaderView(int groupPosition, int childPosition) {
if (mHeaderView == null || mAdapter == null
|| ((ExpandableListAdapter) mAdapter).getGroupCount() == 0) {
return;
}
int state = mAdapter.getTreeHeaderState(groupPosition, childPosition);
switch (state) {
case IphoneTreeHeaderAdapter.PINNED_HEADER_GONE: {
mHeaderViewVisible = false;
break;
}
case IphoneTreeHeaderAdapter.PINNED_HEADER_VISIBLE: {
mAdapter.configureTreeHeader(mHeaderView, groupPosition,
childPosition, MAX_ALPHA);
if (mHeaderView.getTop() != 0) {
mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
}
mHeaderViewVisible = true;
break;
}
case IphoneTreeHeaderAdapter.PINNED_HEADER_PUSHED_UP: {
View firstView = getChildAt(0);
int bottom = firstView.getBottom();
// intitemHeight = firstView.getHeight();
int headerHeight = mHeaderView.getHeight();
int y;
int alpha;
if (bottom < headerHeight) {
y = (bottom - headerHeight);
alpha = MAX_ALPHA * (headerHeight + y) / headerHeight;
} else {
y = 0;
alpha = MAX_ALPHA;
}
mAdapter.configureTreeHeader(mHeaderView, groupPosition,
childPosition, alpha);
if (mHeaderView.getTop() != y) {
mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight
+ y);
}
mHeaderViewVisible = true;
break;
}
}
}
/**
* Get the ExpandableListAdapter associated with this activity's ExpandableListView.
*/
public ExpandableListAdapter getExpandableListAdapter() {
return adapter;
}