原型图(因为公司原因 只贴代码,后期有什么不懂的可以联系我):
要先学会正视自己,再学会审视问题;然后,找一个平衡点,至少让自己体面一点。一味的心不甘情不愿,除了折磨自己,根本没有任何意义
具体实现
第一步:MyExpandableListView
public class MyExpandableListView extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mGroup;
private Boolean isSelect = false;
private List<List<Inf_Area.ResultBean.TblFreightCityiesInfoBean>> mItemList;
int item = 0;
private final LayoutInflater mInflater;
private List<Inf_Area.ResultBean.TblFreightCityiesInfoBean> getList;
private ExpandableListView mRv;
private onItemShowListener mOnItemShowListener;
private LabelsView mLabelsView;
private ArrayList<LabelsView> mLabelsViews = new ArrayList<>();
private List<Integer> mInteger = new ArrayList<>();
private String type;
private OnData mDataListener;
ArrayList<String> list = new ArrayList<>();
public MyExpandableListView(Context mContext, List<String> mGroup, List<List<Inf_Area.ResultBean.TblFreightCityiesInfoBean>> mItemList, ExpandableListView rv) {
this.mContext = mContext;
this.mGroup = mGroup;
this.mItemList = mItemList;
this.mInflater = LayoutInflater.from(mContext);
this.mRv = rv;
}
public void setData(List<Inf_Area.ResultBean.TblFreightCityiesInfoBean> getList) {
this.getList = getList;
}
@Override
public int getGroupCount() {
return mGroup.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return mGroup.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mItemList.get(groupPosition).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;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// convertView = mInflater.inflate(R.layout.child_item, null);
// }
if (convertView == null) {
convertView = mInflater.inflate(R.layout.group_item, null);
}
String group = mGroup.get(groupPosition);
TextView tvGroup = (TextView) convertView.findViewById(R.id.location);
tvGroup.setText(group);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
List<Inf_Area.ResultBean.TblFreightCityiesInfoBean> infoBeans = mItemList.get(groupPosition);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.child_item, null, false);
}
RecyclerView recyclerView = convertView.findViewById(R.id.recy);
recyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
Rlv_Adapter rlv_freightItem = new Rlv_Adapter(infoBeans,mItemList, groupPosition);
recyclerView.setAdapter(rlv_freightItem);
rlv_freightItem.setOnItemShowListener(new Rlv_Adapter.onItemShowListener() {
@Override
public void OnItemShow(int position, TextView holder1) {
// boolean status = mTblFreightCityiesInfoBeans.get(position).isStatus();
for (int i = 0; i <infoBeans.size() ; i++) {
boolean isSelect = infoBeans.get(i).isIsSelect();
if (isSelect) {
if (i!=position) {
infoBeans.get(i).setIsSelect(false);
notifyDataSetChanged();
}
}
}
boolean isSelect = infoBeans.get(position).isIsSelect();
Log.v("xue","点击时候的 是true 还是false-==============="+isSelect);
if (isSelect) {
holder1.setTextColor(Color.parseColor("#6B6B6B"));
infoBeans.get(position).setIsSelect(false);
}else {
//红色
holder1.setTextColor(Color.parseColor("#E53030"));
infoBeans.get(position).setIsSelect(true);
}
for (int i = 0; i <mItemList.size() ; i++) {
for (int j = 0; j <mItemList.get(i).size() ; j++) {
if (position!=j) {
holder1.setTextColor(Color.parseColor("#6B6B6B"));
mItemList.get(i).get(j).setIsSelect(false);
notifyDataSetChanged();
}
}
}
}
});
return convertView;
}
public interface OnData {
void getdata(String list, int poistion);
}
public void setOnDataListener(OnData onDataListener) {
this.mDataListener = onDataListener;
}
//子项是否可选中,如果要设置子项的点击事件,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public interface onItemShowListener {
void OnItemShow(int position);
}
public void setOnItemShowListener(onItemShowListener listener) {
mOnItemShowListener = listener;
}
}
第二步:Rlv_Adapter
public class Rlv_Adapter extends RecyclerView.Adapter {
private ArrayList<Inf_Area.ResultBean.TblFreightCityiesInfoBean> mTblFreightCityiesInfoBeans;
private Context mContext;
private List<List<Inf_Area.ResultBean.TblFreightCityiesInfoBean>> mItemList;
int chouPostion;
private onItemShowListener mOnItemShowListener;
public Rlv_Adapter(List<Inf_Area.ResultBean.TblFreightCityiesInfoBean> tblFreightCityiesInfoBeans, List<List<Inf_Area.ResultBean.TblFreightCityiesInfoBean>> mItemList, int postion ) {
this.mTblFreightCityiesInfoBeans = (ArrayList<Inf_Area.ResultBean.TblFreightCityiesInfoBean>) tblFreightCityiesInfoBeans;
this.mItemList=mItemList;
this.chouPostion=postion;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
mContext = parent.getContext();
View inflate = LayoutInflater.from(mContext).inflate(R.layout.recy_item, null);
return new OneViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
boolean isSelect = mTblFreightCityiesInfoBeans.get(position).isIsSelect();
Log.v("xue","未点击时候的 是true 还是false-==============="+isSelect);
OneViewHolder holder1 = (OneViewHolder) holder;
if (isSelect) {
//红色
holder1.mLocation.setTextColor(Color.parseColor("#E53030"));
}else {
//恢复
holder1.mLocation.setTextColor(Color.parseColor("#6B6B6B"));
}
holder1.mLocation.setText(mTblFreightCityiesInfoBeans.get(position).getCity());
holder1.mLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemShowListener.OnItemShow(position,holder1.mLocation);
}
});
}
@Override
public int getItemCount() {
return mTblFreightCityiesInfoBeans.size();
}
static class OneViewHolder extends RecyclerView.ViewHolder {
private final TextView mLocation;
public OneViewHolder(View itemView) {
super(itemView);
mLocation = itemView.findViewById(R.id.te_revy);
}
}
public interface onItemShowListener {
void OnItemShow(int position,TextView textView);
}
public void setOnItemShowListener(onItemShowListener listener) {
mOnItemShowListener = listener;
}
}
有什么不理解的可以随时联系我