Recently, the company's product iteration has raised a new requirement. Previously, there was a function that required up to level 8 to reach the final details page. User feedback was that the level was too deep, so the company decided to combine the two into one and turn it into level 4. Without further ado, let’s record the implementation method here. This one requests an interface to return parent data and child data, and the other one requests an interface from the parent, and the child requests an interface, which will be discussed in the next article.
First, the parent layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bgcolor"
android:orientation="vertical">
<!--标题-->
<include layout="@layout/title_layout" />
<TextView
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@color/cut_off_rule_line" />
<!--列表-->
<ExpandableListView
android:id="@+id/re_car_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bgcolor"
android:cacheColorHint="#00000000"
android:childDivider="@color/white"
android:divider="@color/white"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:groupIndicator="@null" />
<!--无数据显示-->
<include
android:id="@+id/il_nodata"
layout="@layout/nodata_view_layout"
android:visibility="gone" />
</LinearLayout>
</layout>
Child layout:
<?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="wrap_content"
android:background="@color/bgcolor"
android:gravity="center_vertical"
android:orientation="horizontal">
<!--标题-->
<TextView
android:id="@+id/tv_car_circuit"
style="@style/content_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="left|center"
android:text="车型"
android:textColor="@color/gray_66" />
<!--右边图标-->
<ImageView
android:id="@+id/car_manual_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@mipmap/right"
android:gravity="center" />
</LinearLayout>
The Activity code has deleted part of the code. Just look at the adapter:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.umeng.analytics.MobclickAgent;
import org.apache.http.HttpException;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* 手册-车型列表-新版
* Created by admin on 2017/6/28.
*/
public class CarTypeActivity extends BaseActivity {
public CartypeLayoutBinding cartypeLayoutBinding;
private String carid, type, is_vip;
View nodataView;
private CarTypeAdapter adapter;
List<CartypeResponse.DataBean.ListBean> group_data = new ArrayList<>(); //父级
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cartypeLayoutBinding = DataBindingUtil.setContentView(this, R.layout.cartype_layout);
oncreaview();
carid = getIntent().getExtras().getString("pinpai");
type = getIntent().getExtras().getString("type");
is_vip = getIntent().getExtras().getString("is_vip");
String title_name = getIntent().getExtras().getString("title_name");
initEven();
if (carid != null) {
setTitle(title_name);
initGetCarData();
}
adapter = new CarTwoTypeAdapter(this, group_data);
cartwotypeLayoutBinding.reCarType.setAdapter(adapter);
/**
* 子级点击事件
*/
cartypeLayoutBinding.reCarType.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent intent = new Intent();
if (type.equals("18")) { //手册
ManualCatalogListActivity.class);
intent.putExtra("book_id", group_data.get(groupPosition).getSon().get(childPosition).getId());
intent.putExtra("pid", "0");
intent.putExtra("manual_title", group_data.get(groupPosition).getSon().get(childPosition).getShouce_name());
intent.putExtra("type", type);
startActivity(intent);
}
return true;
}
});
}
//详情请求参数
private String postrequest() {
String data = "";
try {
JSONObject ClientKey = new JSONObject();
ClientKey.put("xxx", carid);
ClientKey.put("xxx", type);
data = NetInterfaceEngine.getEngine().jsonData(ClientKey);
} catch (JSONException e) {
e.printStackTrace();
}
return data;
}
private void initGetCarData() {
// showProgressDialog();
String url = Constant.GETTWOBOOK;
String json = postrequest();
NetInterfaceEngine.getEngine().commitOKHttpJson(url, json, new NetHelper() {
@Override
public void onSuccess(String result) {
if (result == null) {
Toast.makeText(CarTwoTypeActivity.this, R.string.net_response, Toast.LENGTH_SHORT).show();
return;
}
dismissProgressDialog();
CarTwotypeResponse emty = JSONUtil.parse(result, CarTwotypeResponse.class);
if (emty.getData() != null && emty.getStatus().equals("ok")) {
List<CarTwotypeResponse.DataBean.ListBean> cardata = emty.getData().getList();//第一级数据
group_data.addAll(cardata);
adapter.notifyDataSetChanged();
if (emty.getData().getList().size() == 0) {
nodataView.setVisibility(View.VISIBLE);
cartwotypeLayoutBinding.reCarType.setVisibility(View.GONE);
}
} else {
if (emty != null && emty.getMessage() != null) { //添加错误为空判断
toast(emty.getMessage());
} else {
toast("服务器繁忙,请稍候再试");
}
}
}
@Override
public void onFail(HttpException e, String err) {
Toast.makeText(CarTwoTypeActivity.this, "网络异常!", Toast.LENGTH_SHORT).show();
dismissProgressDialog();
}
});
}
/**
* Expandable适配器
*/
class CarTwoTypeAdapter extends BaseExpandableListAdapter {
private Context context;
private List<CarTwotypeResponse.DataBean.ListBean> group; //父级
public CarTwoTypeAdapter(Context context, List<CarTwotypeResponse.DataBean.ListBean> group) {
this.context = context;
this.group = group;
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public int getChildrenCount(int groupPosition) {
return group.get(groupPosition).getSon().size();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return group.get(groupPosition).getSon().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;
}
/**
* 获取显示指定组的视图对象
*
* @param groupPosition 组位置
* @param isExpanded 该组是展开状态还是伸缩状态
* @param convertView 重用已有的视图对象
* @param parent 返回的视图对象始终依附于的视图组
* @return
* @see android.widget.ExpandableListAdapter#getGroupView(int, boolean,
* View, ViewGroup)
*/
@SuppressLint("SetTextI18n")
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.recycleview_item_parent, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.tv_car_parent_list);
holder.is_new = convertView.findViewById(R.id.is_new);
holder.car_up_img = convertView.findViewById(R.id.car_up_img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText("[ " + group.get(groupPosition).getPid_name() + " ] " + group.get(groupPosition).getKeyword1());
if (type.equals("18")) {
if (group.get(groupPosition).getIs_new().equals("1")) {
holder.is_new.setVisibility(View.VISIBLE);
} else {
holder.is_new.setVisibility(View.GONE);
}
}
holder.textView.setPadding(10, 10, 0, 10);
//判断isExpanded就可以控制是按下还是关闭,同时更换图片
if (isExpanded) {
holder.car_up_img.setBackgroundResource(R.mipmap.arrow_up);
} else {
holder.car_up_img.setBackgroundResource(R.mipmap.arrow_down);
}
return convertView;
}
/**
* 获取一个视图对象,显示指定组中的指定子元素数据。
*
* @param groupPosition 组位置
* @param childPosition 子元素位置
* @param isLastChild 子元素是否处于组中的最后一个
* @param convertView 重用已有的视图(View)对象
* @param parent 返回的视图(View)对象始终依附于的视图组
* @return
* @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean,
* View, ViewGroup)
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.recycleview_item_child, null);
holder = new ViewHolder();
holder.tv_car_child_list = (TextView) convertView.findViewById(R.id.tv_car_child_list);
holder.car_vip = convertView.findViewById(R.id.car_vip);
holder.circuit_img_line = convertView.findViewById(R.id.circuit_img_line);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//是否VIP
if (group.get(groupPosition).getSon().get(childPosition).getIs_check_vip().equals("1")) {
holder.car_vip.setVisibility(View.VISIBLE);
} else {
holder.car_vip.setVisibility(View.GONE);
}
holder.tv_car_child_list.setText(group.get(groupPosition).getSon().get(childPosition).getShouce_name());
holder.tv_car_child_list.setPadding(40, 10, 0, 10);
return convertView;
}
/**
* 是否选中指定位置上的子元素。
* 让isChildSelectable方法返回true,解决ExpandableListView中子元素无法点击,OnChildClickListener无效
*
* @param groupPosition
* @param childPosition
* @return
* @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class ViewHolder {
TextView textView, is_new, tv_car_child_list, car_vip;
ImageView car_up_img, circuit_img_line;
}
}
}