android 树形菜单列表

版权声明:未经博主允许不得转载。 https://blog.csdn.net/qq_28934205/article/details/79971310

这里写图片描述

这里写图片描述

package com.zhjy.mytree;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.zhjy.mytree.treeutils.Node;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    SimpleTreeAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv);


        List<Node> mlist = new ArrayList<>();
        mlist.add(new Node("1","0","总部"));
        mlist.add(new Node("2","1","领导"));
        mlist.add(new Node("3","1","分公司"));
        mlist.add(new Node("5","1","技术部"));
        mlist.add(new Node("6","1","商务部"));
        mlist.add(new Node("4","1","财务部"));
        mlist.add(new Node("7","1","后勤部"));
        mlist.add(new Node("8","3","分公司 技术部"));
        mlist.add(new Node("9","3","分公司 商务部"));


        mAdapter = new SimpleTreeAdapter(listView, this,
                mlist, 1,R.mipmap.tree_ex,R.mipmap.tree_ec);
        listView.setAdapter(mAdapter);

    }
}
package com.zhjy.mytree;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zhjy.mytree.treeutils.Node;
import com.zhjy.mytree.treeutils.TreeListViewAdapter;

import java.util.List;

/**
 * Created by Administrator on 2018/3/1.
 */

public class SimpleTreeAdapter extends TreeListViewAdapter
{
    public SimpleTreeAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(mTree, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
    }

    public SimpleTreeAdapter(ListView mTree, Context context, List<Node> datas,
                             int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
    }

    @Override
    public View getConvertView(final Node node , int position, View convertView, ViewGroup parent)
    {

        final ViewHolder viewHolder ;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.label = (TextView) convertView
                    .findViewById(R.id.id_treenode_label);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (node.getIcon() == -1) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
            viewHolder.img.setImageResource(R.mipmap.icon_file);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
            viewHolder.img.setImageResource(R.mipmap.icon_files);
        }


        viewHolder.label.setText(node.getName());

        return convertView;
    }

    private final class ViewHolder
    {
        ImageView icon;
        ImageView img;
        TextView label;
    }


}

文件地址 https://download.csdn.net/download/qq_28934205/10354193

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页