Android實(shí)現(xiàn)樹形層級ListView
直接貼代碼,代碼中有相應(yīng)注釋:
主界面Activity,布局就只一個(gè)ListView:
public class MainActivity extends Activity {
private ListView mListView;
private TreeListViewAdapter<TestBean> mAdapter;
private List<TestBean> mDatas = new ArrayList<TestBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mListView = (ListView) findViewById(R.id.listview);
initTestDatas();
try {
mAdapter = new TreeListViewAdapter<TestBean>(mListView, this, mDatas, 0);
}
catch (Exception e) {
e.printStackTrace();
}
this.mListView.setAdapter(mAdapter);
mAdapter.setmTreeListener(new TreeViewOnItemClick() {
@Override
public void onTreeItemClick(int position, Node node) {
Toast.makeText(MainActivity.this, "你點(diǎn)擊的是:" + node.getName(), Toast.LENGTH_SHORT).show();
}
});
this.mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
final EditText edt = new EditText(MainActivity.this);
new AlertDialog.Builder(MainActivity.this).setTitle("Insert").setView(edt).setPositiveButton("submit", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (TextUtils.isEmpty(edt.getText().toString())) {
Toast.makeText(MainActivity.this, "請?zhí)顚懱砑觾?nèi)容", Toast.LENGTH_SHORT).show();
}
else {
mAdapter.insertNodeData(arg2, edt.getText().toString());
}
}
}).setNegativeButton("Cancel", null).create().show();
return true;
}
});
}
private void initTestDatas() {
TestBean bean = null;
bean = new TestBean(1, 0, "文件目錄1");
mDatas.add(bean);
bean = new TestBean(2, 0, "文件目錄2");
mDatas.add(bean);
bean = new TestBean(3, 0, "文件目錄3");
mDatas.add(bean);
bean = new TestBean(4, 1, "文件目錄4");
mDatas.add(bean);
bean = new TestBean(5, 1, "文件目錄5");
mDatas.add(bean);
bean = new TestBean(6, 2, "文件目錄6");
mDatas.add(bean);
bean = new TestBean(7, 2, "文件目錄7");
mDatas.add(bean);
bean = new TestBean(8, 3, "文件目錄8");
mDatas.add(bean);
bean = new TestBean(9, 3, "文件目錄9");
mDatas.add(bean);
bean = new TestBean(10, 0, "文件目錄10");
mDatas.add(bean);
}
}
數(shù)據(jù)適配器基類:
**
* 樹形ListView的數(shù)據(jù)適配器類
* @description:
* @author ldm
* @date 2015-10-9 上午9:47:01
*/
public abstract class TreeViewBaseAdapter<T> extends BaseAdapter {
protected Context context;
protected List<T> datas;
protected List<Node> mAllNodes;
protected List<Node> mVisibleNodes;
protected LayoutInflater mInflater;
protected ListView treeLv;
protected TreeViewOnItemClick mTreeListener;
public TreeViewBaseAdapter(ListView treeLv, Context context, List<T> datas, int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException {
this.context = context;
this.treeLv = treeLv;
mInflater = LayoutInflater.from(context);
mAllNodes = TreeHelperTools.getSortedNodes(datas, defaultExpandLevel);
mVisibleNodes = TreeHelperTools.filterVisibleNodes(mAllNodes);
this.treeLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
expandOrCollapse(position);
if(mTreeListener!=null){
mTreeListener.onTreeItemClick(position, mVisibleNodes.get(position));
}
}
});
}
public void setmTreeListener(TreeViewOnItemClick mTreeListener) {
this.mTreeListener = mTreeListener;
}
/**
* 設(shè)置ListView點(diǎn)擊item節(jié)點(diǎn)時(shí),是否應(yīng)該展開
* @description:
* @author ldm
* @date 2015-10-10 上午9:05:08
*/
protected void expandOrCollapse(int position) {
Node n=mVisibleNodes.get(position);
if(n!=null){
if(n.isLeaf()){
return;
}
n.setExpand(!n.isExpand());
mVisibleNodes=TreeHelperTools.filterVisibleNodes(mAllNodes);
notifyDataSetChanged();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mVisibleNodes.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mVisibleNodes.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Node node=mVisibleNodes.get(position);
convertView = getContentView(node,position, convertView, parent);
return convertView;
}
public abstract View getContentView(Node node,int position, View convertView, ViewGroup parent);
public interface TreeViewOnItemClick{
void onTreeItemClick(int position,Node node);
}
}
我們使用的Adapter
public class TreeListViewAdapter<T> extends TreeViewBaseAdapter<T> {
public TreeListViewAdapter(ListView treeLv, Context context, List<T> datas, int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException {
super(treeLv, context, datas, defaultExpandLevel);
}
@Override
public View getContentView(Node node, int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.tree_listview_item, parent, false);
holder.mItemIv = (ImageView) convertView.findViewById(R.id.mItemIv);
holder.mItemTv = (TextView) convertView.findViewById(R.id.mItemTv);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mItemIv.setPadding(node.getLevel()*30, 3, 3, 3);
if (node.getIcon() == -1) {
holder.mItemIv.setVisibility(View.INVISIBLE);
}
else {
holder.mItemIv.setVisibility(View.VISIBLE);
holder.mItemIv.setImageResource(node.getIcon());
}
holder.mItemTv.setText(node.getName());
return convertView;
}
private static class ViewHolder {
ImageView mItemIv;
TextView mItemTv;
}
/**
* 動(dòng)態(tài)插入數(shù)據(jù)
* @description:
* @author ldm
* @date 2015-10-10 上午10:08:03
*/
public void insertNodeData(int position,String label) {
Node node=mVisibleNodes.get(position);
int indexOf=mAllNodes.indexOf(node);
Node insertNode=new Node(-1, node.getId(), label);
insertNode.setParent(node);
node.getChildren().add(insertNode);
mAllNodes.add(indexOf+1,insertNode);
mVisibleNodes=TreeHelperTools.filterVisibleNodes(mVisibleNodes);
notifyDataSetChanged();
}
}
數(shù)據(jù)處理的工具類:
public class TreeDatasHelperTools {
/**
* 將用戶提供的數(shù)據(jù)轉(zhuǎn)化成樹層級上可用數(shù)據(jù)
* @description:
* @date 2015-10-9 下午4:07:24
*/
public static <T> List<Node> datas2Nodes(List<T> datas) throws IllegalAccessException, IllegalArgumentException {
List<Node> nodes = new ArrayList<Node>();
Node node = null;
for (T t : datas) {
int id = -1;
int pid = -1;
String label = "";
// node = new Node();
Class clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();// 反射獲取類中的字段
for (Field field : fields) {
if (field.getAnnotation(TreeNodeId.class) != null) {// 根據(jù)字段上的注解來獲取對應(yīng)的值
field.setAccessible(true);// 在java的反射使用中,如果字段是私有的,那么必須要對這個(gè)字段設(shè)置才能正常使用,否則報(bào)錯(cuò)
id = field.getInt(t);
}
if (field.getAnnotation(TreeNodePid.class) != null) {
field.setAccessible(true);
pid = field.getInt(t);
}
if (field.getAnnotation(TreeNodeLabel.class) != null) {
field.setAccessible(true);
label = (String) field.get(t);
}
}
node = new Node(id, pid, label);
nodes.add(node);
}
// 設(shè)置nodes中的父子節(jié)點(diǎn)關(guān)系
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node m = nodes.get(j);
if (m.getPid() == n.getId()) {// 如果m的父節(jié)點(diǎn)pid==n的id,則m是父節(jié)點(diǎn),n是子節(jié)點(diǎn)
n.getChildren().add(m);
m.setParent(n);
}
else if (m.getId() == n.getPid()) {
m.getChildren().add(n);
n.setParent(m);
}
}
}
// 設(shè)置節(jié)點(diǎn)圖片
for (Node n : nodes) {
setNodeIcon(n);
}
return nodes;
}
/**
* 為我們的node數(shù)據(jù)設(shè)置對應(yīng)的圖標(biāo)
* @description:
* @date 2015-10-9 下午4:46:29
*/
private static void setNodeIcon(Node n) {
if (n.getChildren().size() > 0 && n.isExpand()) {// 如果有子節(jié)點(diǎn)且展開狀態(tài)
n.setIcon(R.drawable.icon_unable);
}
else if (n.getChildren().size() > 0 && !n.isExpand()) {
n.setIcon(R.drawable.icon_enable);
}
else {
n.setIcon(-1);
}
}
public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException {
List<Node> result = new ArrayList<Node>();
List<Node> nodes = datas2Nodes(datas);
// 首先獲取根節(jié)點(diǎn)數(shù)據(jù)
List<Node> rootNodes = getRootNodes(nodes);
for (Node node : rootNodes) {
addNode(result, node, defaultExpandLevel, 1);
}
return result;
}
/**
* 獲取數(shù)據(jù)中的所有根節(jié)點(diǎn)數(shù)據(jù)
* @description:
* @date 2015-10-9 下午5:00:32
*/
private static List<Node> getRootNodes(List<Node> nodes) {
List<Node> root = new ArrayList<Node>();
for (Node node : nodes) {
if (node.isRoot()) {
root.add(node);
}
}
return root;
}
/**
* 獲取到可見的節(jié)點(diǎn)數(shù)據(jù)
* @description:
* @date 2015-10-9 下午5:12:58
*/
public static List<Node> filterVisibleNodes(List<Node> mAllNodes) {
List<Node> nodes = new ArrayList<Node>();
for (Node node : mAllNodes) {
if (node.isRoot() || node.isParentExpand()) {
setNodeIcon(node);
nodes.add(node);
}
}
return nodes;
}
/**
* 把一個(gè)節(jié)點(diǎn)的所有子節(jié)點(diǎn)都放入result中
* @description:
* @date 2015-10-9 下午5:05:57
*/
private static void addNode(List<Node> result, Node node, int defaultExpandLevel, int currentLevel) {
result.add(node);
if (defaultExpandLevel >= currentLevel) {
node.setExpand(true);
}
if (node.isLeaf()) { return; }
for (int i = 0; i < node.getChildren().size(); i++) {
addNode(result, node.getChildren().get(i), defaultExpandLevel, currentLevel + 1);
}
}
}
數(shù)據(jù)實(shí)體Bean:
public class TestBean {
@TreeNodeId
private int id;//添加對應(yīng)的注解
@TreeNodePid
private int pid;
@TreeNodeLabel
private String label;
private String desc;
public TestBean(int id, int pid, String label) {
super();
this.id = id;
this.pid = pid;
this.label = label;
}
public TestBean() {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
數(shù)據(jù)展示中的Node類,我們可以通過反射+注解把任意實(shí)體bean如TestBean映射成我們想要的Node
public class Node {
private int id;// 所在節(jié)點(diǎn)id
private int pid = 0;// 父節(jié)點(diǎn)的id
private String name;// 對應(yīng)的內(nèi)容
private int level;// 所在ListView中樹層級
private boolean isExpand = false;// 所在節(jié)點(diǎn)是否展開
private int icon;// 圖標(biāo)icon
private Node parent;// 父節(jié)點(diǎn)Node
private List<Node> children = new ArrayList<Node>();// 對應(yīng)的子節(jié)點(diǎn)數(shù)據(jù)集
public Node() {
}
public Node(int id, int pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 當(dāng)前節(jié)點(diǎn)所在的層級
* @description:
* @date 2015-10-9 下午4:02:29
*/
public int getLevel() {
return parent == null ? 0 : parent.getLevel() + 1;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isExpand() {
return isExpand;
}
public void setExpand(boolean isExpand) {
this.isExpand = isExpand;
if (!isExpand && children.size() > 0) {// 如果當(dāng)前節(jié)點(diǎn)沒有展開,則其子節(jié)點(diǎn)展開狀態(tài)也是:沒展開
for (Node node : children) {
node.setExpand(false);
}
}
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
/**
* 判斷當(dāng)前節(jié)點(diǎn)有沒有子節(jié)點(diǎn)
* @description:
* @author ldm
* @date 2015-10-9 下午3:59:42
*/
public boolean isLeaf() {
return children.size() == 0;
}
/**
* 是不是根節(jié)點(diǎn)
* @description:
* @author ldm
* @date 2015-10-9 下午3:58:15
*/
public boolean isRoot() {
return parent == null;
}
/**
* 當(dāng)前節(jié)點(diǎn)所在父節(jié)點(diǎn)是否展開
* @description:
* @author ldm
* @date 2015-10-9 下午3:58:34
*/
public boolean isParentExpand() {
if (parent == null) {
return false;
}
else {
return parent.isExpand;
}
}
}
用到的注解類:
@Target(ElementType.FIELD)//定義注解的作用目標(biāo):字段、枚舉的常量
@Retention(RetentionPolicy.RUNTIME)//注解策略: 注解會(huì)在class字節(jié)碼文件中存在,在運(yùn)行時(shí)可以通過反射獲取到
public @interface TreeNodeId {
}
@Target(ElementType.FIELD)//定義注解的作用目標(biāo):字段、枚舉的常量
@Retention(RetentionPolicy.RUNTIME)//注解策略: 注解會(huì)在class字節(jié)碼文件中存在,在運(yùn)行時(shí)可以通過反射獲取到
public @interface TreeNodeLabel {
}
``
@Target(ElementType.FIELD)//定義注解的作用目標(biāo):字段、枚舉的常量
@Retention(RetentionPolicy.RUNTIME)//注解策略: 注解會(huì)在class字節(jié)碼文件中存在,在運(yùn)行時(shí)可以通過反射獲取到
public @interface TreeNodePid {
}
以上就是Android實(shí)現(xiàn)樹形層級ListView的相關(guān)代碼,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android控件BottomSheet實(shí)現(xiàn)底邊彈出選擇列表
這篇文章主要介紹了Android控件BottomSheet實(shí)現(xiàn)底邊彈出選擇列表,比較常用的選擇條件或跳轉(zhuǎn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Listview異步加載網(wǎng)絡(luò)圖片并動(dòng)態(tài)更新的方法,結(jié)合實(shí)例形式詳細(xì)分析了ListView異步加載數(shù)據(jù)的操作步驟與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android用SharedPreferences實(shí)現(xiàn)登錄注冊注銷功能
這篇文章主要為大家詳細(xì)介紹了Android用SharedPreferences實(shí)現(xiàn)登錄注冊注銷功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果
這篇文章主要介紹了Android使用BottomTabBar實(shí)現(xiàn)底部導(dǎo)航頁效果,本文通過實(shí)例代碼結(jié)合文字說明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-03-03
Android ListView 單條刷新方法實(shí)踐及原理解析
這篇文章主要介紹了Android ListView 單條刷新方法實(shí)踐及原理解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android動(dòng)態(tài)加載Activity原理詳解
這篇文章主要介紹了Android動(dòng)態(tài)加載Activity原理詳解的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android SharedPreference存儲(chǔ)文件三步走
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級的存儲(chǔ)類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再從SharedPreferences中將值取出2023-01-01
淺談Android App開發(fā)中Fragment的創(chuàng)建與生命周期
這篇文章主要介紹了Android App開發(fā)中Fragment的創(chuàng)建與生命周期,文中詳細(xì)地介紹了Fragment的概念以及一些常用的生命周期控制方法,需要的朋友可以參考下2016-02-02
Android開發(fā)高級組件之自動(dòng)完成文本框(AutoCompleteTextView)用法示例【附源碼下載】
這篇文章主要介紹了Android開發(fā)高級組件之自動(dòng)完成文本框(AutoCompleteTextView)用法,簡單描述了自動(dòng)完成文本框的功能并結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)自動(dòng)完成文本框功能的具體步驟與相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01

