上效果图
一 布局文件 item_good_tree.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:theme="@style/customTheme">
<LinearLayout
android:layout_marginTop="@dimen/dp_20"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="产品规格"
android:padding="@dimen/dp_10"
android:textStyle="bold"
android:textColor="#333"
android:textSize="@dimen/dp_16"/>
<Button
android:id="@+id/good_tree_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="规格 +"
android:background="@drawable/shape_btn_share"
android:layout_marginLeft="@dimen/dp_20"
android:layout_marginRight="@dimen/dp_20"
android:layout_marginTop="@dimen/dp_30"
android:layout_marginBottom="@dimen/dp_20"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:background="@color/white"
android:minHeight="100dp"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
android:orientation="vertical">
<TextView
android:id="@+id/good_weight_tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:text="暂无记录"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/mTreeListRecy"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
二、布局文件 popup_good_tree.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_overlay"
android:gravity="bottom">
<LinearLayout
android:id="@+id/pop_main_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white2"
android:paddingLeft="@dimen/dp_10"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingRight="@dimen/dp_10"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/dp_20"
android:text=""
android:gravity="center"/>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<ImageView
android:id="@+id/cancel_button"
android:layout_width="@dimen/dp_40"
android:layout_height="@dimen/dp_40"
android:src="@drawable/delete"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingRight="@dimen/dp_10"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_30">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="@dimen/dp_16"
android:textColor="#222"
android:textStyle="bold"
android:text="规格排序"/>
<EditText
android:id="@+id/good_tree_weigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="@dimen/dp_10"
android:minWidth="@dimen/dp_50"
android:hint="0"
android:textColorHint="#999"
android:gravity="right"
android:inputType="number"
android:textColor="#666"
android:background="@null"
android:textCursorDrawable="@null"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="#80cccccc"
android:layout_marginTop="@dimen/dp_10"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#222"
android:textSize="@dimen/dp_16"
android:textStyle="bold"
android:text="规格名称"/>
<EditText
android:id="@+id/good_tree_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="@dimen/dp_10"
android:text=""
android:hint="请输入规格名称"
android:gravity="right"
android:inputType="text"
android:textColor="#666"
android:background="@null"
android:textCursorDrawable="@null"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="#80cccccc"
android:layout_marginTop="@dimen/dp_10"
/>
<TextView
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="确定"
android:textSize="@dimen/sp_18"
android:gravity="center"
android:padding="@dimen/dp_12"
android:textStyle="bold"
android:textColor="@color/white"
android:background="@color/color_goopse"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
三、主要代码:recyleview适配器。该部分代码,借鉴网上案例实现思路,部分进行了优化。
public class TreeRecyclerViewAdapter extends BaseTreeRecyclerViewAdapter {
private OnTreeCheckedChangeListener checkedChangeListener;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = View.inflate(mContext, R.layout.tree_header_item, null);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
inflate.setLayoutParams(params);
return new ViewHolder(inflate);
}
//点击每个条目展开
public interface OnTreeCheckedChangeListener {
//void onCheckChange(NodeBean nodeBean, int position, boolean isChecked);
void onAddItem(View view,NodeBean nodeBean, int position);
void onEditItem(NodeBean nodeBean, int position);
void onDeleItem(NodeBean nodeBean, int position);
}
public void setTreeCheckedChangeListener(OnTreeCheckedChangeListener checkedChangeListener) {
this.checkedChangeListener = checkedChangeListener;
}
public TreeRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<NodeBean> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
super(recyclerView, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
}
public TreeRecyclerViewAdapter(RecyclerView mTree, Context context, List<NodeBean> datas, int defaultExpandLevel) {
super(mTree, context, datas, defaultExpandLevel);
}
@Override
public void onBindViewHolder(final NodeBean nodeBean, @SuppressLint("RecyclerView") final RecyclerView.ViewHolder holder, final int position) {
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.mTreeHeaderName.setText(nodeBean.getName());
if (nodeBean.getIcon() == -1) {
viewHolder.mTreeHeaderExpand.setVisibility(View.INVISIBLE);
} else {
viewHolder.mTreeHeaderExpand.setVisibility(View.VISIBLE);
viewHolder.mTreeHeaderExpand.setImageResource(nodeBean.getIcon());
}
//增加
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//ToastUtils.s(mContext,"点击增加"+nodeBean.getName());
//onTreeAddListener(nodeBean, position);
checkedChangeListener.onAddItem(view,nodeBean, position);
/**
View v = LayoutInflater.from(mContext).inflate(R.layout.popup_good_tree, null);
CommonPopupWindow popupWindow= new CommonPopupWindow.Builder(mContext)
.setView(v)
.setWidthAndHeight(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT)
.setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
@Override
public void getChildView(View view, int layoutResId) {
}
})
.setOutsideTouchable(true)
.create();
// 使用WindowManager设置弹窗的位置,使其从手机最底部弹出
popupWindow.showAtLocation(view,Gravity.BOTTOM,0,0);**/
}
});
//点击编辑
viewHolder.btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//onTreeCheckedChangeListener(nodeBean, position, viewHolder.mTreeHeaderCheckBox.isChecked());
//ToastUtils.s(mContext,"点击编辑");
checkedChangeListener.onEditItem(nodeBean, position);
}
});
//点击删除
viewHolder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//ToastUtils.s(mContext,"点击删除");
checkedChangeListener.onDeleItem(nodeBean, position);
}
});
/**
viewHolder.mTreeHeaderCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setChecked(nodeBean, viewHolder.mTreeHeaderCheckBox.isChecked());
//接口调用
onTreeCheckedChangeListener(nodeBean, position, viewHolder.mTreeHeaderCheckBox.isChecked());
}
});
//
if (nodeBean.isChecked()) {
viewHolder.mTreeHeaderCheckBox.setChecked(true);
} else {
viewHolder.mTreeHeaderCheckBox.setChecked(false);
}**/
}
static class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox mTreeHeaderCheckBox;
private TextView mTreeHeaderName;
private ImageView mTreeHeaderExpand;
private TextView btnAdd;
private TextView btnEdit;
private TextView btnDelete;
public ViewHolder(View itemView) {
super(itemView);
mTreeHeaderCheckBox = itemView.findViewById(R.id.mTreeHeaderCheckBox);
mTreeHeaderName = itemView.findViewById(R.id.mTreeHeaderName);
mTreeHeaderExpand = itemView.findViewById(R.id.mTreeHeaderExpand);
btnAdd=itemView.findViewById(R.id.tree_node_add);
btnEdit=itemView.findViewById(R.id.tree_node_edit);
btnDelete=itemView.findViewById(R.id.tree_node_delete);
}
}
}
四、TreeHeaderView,借鉴网上思路。来源
public class TreeHeaderView extends LinearLayout {
private TreeRecyclerViewAdapter adapter;
private List<NodeBean> dataList = new ArrayList<>();
private Context mContext;
public TreeHeaderView(Context context) {
super(context);
this.mContext=context;
initView(context);
initData();
}
public TreeRecyclerViewAdapter getAdapter() {
return adapter;
}
public TreeHeaderView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext=context;
initView(context);
initData();
}
public TreeHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext=context;
initView(context);
initData();
}
private void initView(Context context) {
//树结构布局
View headerLayout = LayoutInflater.from(context).inflate(R.layout.tree_header_layout, this, true);
// LinearLayoutManager manager = new LinearLayoutManager(context);
//禁止滑动 布局管理器
LinearLayoutManager manager = new LinearLayoutManager(context) {
//禁止竖向滑动 RecyclerView 为垂直状态(VERTICAL)
@Override
public boolean canScrollVertically() {
return false;
}
//禁止横向滑动 RecyclerView 为水平状态(HORIZONTAL)
/*@Override
public boolean canScrollHorizontally() {
return false;
}*/
};
RecyclerView mTreeHeaderViewRecy = headerLayout.findViewById(R.id.mTreeHeaderViewRecy);
mTreeHeaderViewRecy.setLayoutManager(manager);
//第一个参数 ListView & RecyclerView
//第二个参数 上下文
//第三个参数 数据集
//第四个参数 默认展开层级数 0为不展开
//第五个参数 展开的图标
//第六个参数 闭合的图标
adapter = new TreeRecyclerViewAdapter(mTreeHeaderViewRecy, context, dataList, 0, R.drawable.svg_expand_more, R.drawable.svg_navigate_next);
mTreeHeaderViewRecy.setAdapter(adapter);
}
private void initData() {
//根节点0 1 2
dataList.add(new NodeBean<>("0", "-1", "A 1级节点"));
dataList.add(new NodeBean<>("1", "-1", "B 1级节点"));
dataList.add(new NodeBean<>("2", "-1", "C 1级节点"));
// //根节点1的二级节点
dataList.add(new NodeBean<>("3", "0", "A 2-1级节点"));
dataList.add(new NodeBean<>("4", "0", "A 2-2级节点"));
dataList.add(new NodeBean<>("5", "0", "A 2-3级节点"));
//根节点2的二级节点
dataList.add(new NodeBean<>("6", "1", "B 2级节点"));
dataList.add(new NodeBean<>("7", "1", "B 2级节点"));
dataList.add(new NodeBean<>("8", "1", "B 2级节点"));
//根节点3的二级节点
dataList.add(new NodeBean<>("9", "2", "C 2级节点"));
dataList.add(new NodeBean<>("10", "2", "C 2级节点"));
dataList.add(new NodeBean<>("11", "2", "C 2级节点"));
//三级节点
dataList.add(new NodeBean<>("12", "3", "A 3-1级节点"));
dataList.add(new NodeBean<>("13", "3", "A 3-2级节点"));
dataList.add(new NodeBean<>("14", "3", "A 3-1级节点"));
dataList.add(new NodeBean<>("15", "4", "A 3级节点"));
dataList.add(new NodeBean<>("16", "4", "A 3级节点"));
dataList.add(new NodeBean<>("17", "4", "A 3级节点"));
dataList.add(new NodeBean<>("18", "5", "A 3级节点"));
dataList.add(new NodeBean<>("19", "5", "A 3级节点"));
dataList.add(new NodeBean<>("20", "5", "A 3级节点"));
//四级节点
dataList.add(new NodeBean<>("21", "12", "A 4级节点"));
//五级节点
dataList.add(new NodeBean<>("22", "21", "A 5级节点"));
//六级节点
dataList.add(new NodeBean<>("23", "22", "A 6级节点"));
//七级节点
dataList.add(new NodeBean<>("24", "23", "A 7级节点"));
//八级节点
dataList.add(new NodeBean<>("25", "24", "A 8级节点"));
adapter.addData(dataList);
//总item点击状态监听
adapter.setOnTreeClickListener(new BaseTreeRecyclerViewAdapter.OnTreeClickListener() {
@Override
public void onClick(NodeBean nodeBean, int position) {
Log.e("TAG1231", "setOnTreeClickListener: " + nodeBean.getName());
}
});
}
}
115.Android 简单的多级树结构列表+正常列表结合使用(RecyclerView) 两种方式实现(自定义adapter实现和使用BaseQuickAdapter库实现)_recyclerview实现多级列表_小妞得意的博客-CSDN博客
五、fragement片段处理接口事件,主要核心思路 :
public class GoodItemTreeFragment extends BaseFragment {
..........略........
@Override
public void onActivityCreated(@NonNull Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button btn_add=mActivity.findViewById(R.id.good_tree_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindows = new CommonPopupWindow.Builder(getContext())
.setView(R.layout.popup_good_tree)
.setWidthAndHeight(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
.setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
@Override
public void getChildView(View view, int layoutResId) {
Log.e("TAG1231", "getChildView position: ");
ImageView cancel=view.findViewById(R.id.cancel_button);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindows.dismiss();
}
});
//
TextView btnConfirm=view.findViewById(R.id.btn_confirm);
EditText treeWeigh=view.findViewById(R.id.good_tree_weigh);
EditText nameWeigh=view.findViewById(R.id.good_tree_name);
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("TAG1231", "我被点击 position: "+treeWeigh.getText().toString()+nameWeigh.getText().toString());
}
});
}
})
.setOutsideTouchable(true)
.create();
popupWindows.showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
});
}
}
如遇其他问题,欢迎留言讨论。