public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
HashMap<String, List<ExpandableListItem>> _child;
String response;
String serviceName, serviceIcon, serviceId;
ExpandableListView expandableListView;
CustomExpandableListAdapter expandableListAdapter;
String groupName;
String childName;
List<ExpandableListItem> listtG1;
JSONArray jsonArray;
List<ExpandableListItem> _header;
ExpandableListItem rowData;
List<String> _groupNameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(getResources().getString(R.string.user_name));
new CategoryServices().execute("");
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
groupName = _groupNameList.get(groupPosition);
listtG1 = _child.get(groupName);
Log.d("ListSize", String.valueOf(listtG1.size()));
Toast.makeText(MainActivity.this,groupName+""+childName,Toast.LENGTH_LONG).show();
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this,groupName+""+childName,Toast.LENGTH_LONG).show();
}
});
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
groupName = _groupNameList.get(groupPosition);
listtG1 = _child.get(groupName);
Toast.makeText(MainActivity.this, groupName,Toast.LENGTH_LONG).show();
Log.d("ListSize", String.valueOf(listtG1.size()));
if (listtG1.size() == 1 && listtG1.get(0).getServiceName().equals(groupName)) {
expandableListView.setGroupIndicator(null);
return true;
}
return false;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
childName = listtG1.get(childPosition).getServiceName();
Toast.makeText(
getApplicationContext(),
childName, Toast.LENGTH_SHORT
).show();
return false;
}
});
dynamicToolbarColor();
toolbarTextAppernce();
}
private void dynamicToolbarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.profile_pic);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(R.attr.colorPrimary));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(R.attr.colorPrimaryDark));
}
});
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
class CategoryServices extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
response = JSONFunctions.getJSONfromURL("http://cpanel.smartindiaservice.com/api/categoryservices?categoryid=13");
return response;
}
@Override
protected void onPostExecute(String result) {
try {
setListAdapter(result, expandableListView);
// return false;
} catch (NullPointerException e) {
}
super.onPostExecute(result);
}
}
private void setListAdapter(String response, ExpandableListView expandableListView) {
_child = new HashMap<>();
_groupNameList = new ArrayList<>();
if (!response.isEmpty()) {
try {
jsonArray = new JSONArray(response);
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject productObj = jsonArray.getJSONObject(i);
String groupName = productObj.getString("Service");
JSONArray servicesArray = productObj.getJSONArray("SubServices");
if (servicesArray != null && servicesArray.length() > 0) {
_header = new ArrayList<>();
for (int j = 0; j < servicesArray.length(); j++) {
JSONObject serviceObj = servicesArray.getJSONObject(j);
rowData = new ExpandableListItem();
serviceName = serviceObj.getString("SubServiceName");
serviceIcon = serviceObj.getString("SubServiceDescription");
serviceId = serviceObj.getString("SubServiceID");
rowData.setGroupName(groupName);
rowData.setServiceIcon(serviceIcon);
rowData.setServiceId(serviceId);
rowData.setServiceName(serviceName);
_header.add(rowData);
}
}
_child.put(groupName, _header);
_groupNameList.add(groupName);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
expandableListAdapter = new CustomExpandableListAdapter(this, _groupNameList, _child);
expandableListView.setAdapter(expandableListAdapter);
}
}
}
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<ExpandableListItem>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<ExpandableListItem>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// final String expandedListText = getChild(listPosition, expandedListPosition).g;
ExpandableListItem rowData = (ExpandableListItem) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(rowData.getServiceName());
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
这是我的活动和适配器我能够在可扩展列表视图中显示数据我希望当组包含多个子项时显示指示符,并且当组只包含一个子组时我想要隐藏组指示符,组名称和子名称是同样我想在活动expandableListView.setGroupIndicator(null);在setOnGroupClickListener上使用这个代码,这个点击但是什么时候它显示所有指标,当我点击其子项为1且名称相同的组时,它隐藏所有指标请告诉我如何解决此问题