近日公司产品迭代,提了个新需求,之前有个功能最多要点8级才能点到最后详情页面,用户反馈层级太深,公司决定合二为一变成四级。废话不多说,在此记录下实现方式,这个是请求一个接口返回父级数据和子级数据的,还有个是父级请求一个接口,子级请求一个接口的下一篇再赘述。
首先父级布局方面:
子级布局:
Activity代码删除了部分代码关键看适配器就可以了:
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 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 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 group; //父级
public CarTwoTypeAdapter(Context context, List 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;
}
}
}