当前位置: 首页 > 知识库问答 >
问题:

实现3级可扩展的ListView Android?

宗政颖逸
2023-03-14

趋势

长相

男人

----西式服装

-----伤员

--鞋类

    listDataHeader.add("Trending");
    listDataHeader.add("Looks");
    listDataHeader.add("Men");
    listDataHeader.add("Women");
    listDataHeader.add("Leaderboard");
    List<String> Men=new ArrayList<>();
    Men.add("Clothing");
    Men.add("Footwear");
    Men.add("Accesories");

    List<String> Women=new ArrayList<>();
    Women.add("Western wear");
    Women.add("Footwear");
    Women.add("Ethnic wear");
    Women.add("Lingerie");
    Women.add("Accesories");

    listDataChild.put(listDataHeader.get(2),Men); // Header, Child data
    listDataChild.put(listDataHeader.get(3),Women);

    //Men sub sub categories

    //listDataSubChildHeader.add("Clothing");
    //listDataSubChildHeader.add("Clothing");

    List<String> menClothing=new ArrayList<>();
    menClothing.add("Casuals");
    menClothing.add("Ethnic wear");

    List<String> menFootwear=new ArrayList<>();
    menFootwear.add("Boots");
    menFootwear.add("Casual Shoes");

    List<String> menAccesories=new ArrayList<>();
    menAccesories.add("Bags and wallets");
    menAccesories.add("Caps and hats");

    //sub sub categories
            //listDataSubChildHeader=new ArrayList<String>();
    menListDataSubChild=new HashMap<String,List<String>>();
    womenListDataSubChild=new HashMap<String,List<String>>();

    menListDataSubChild.put("Clothing",menClothing);//0
    menListDataSubChild.put("Footwear", menFootwear);//1
    menListDataSubChild.put("Accesories", menAccesories);//2

    //Women sub sub categories
    List<String> WomenWestern=new ArrayList<>();
    menClothing.add("Bottom Wear");
    menClothing.add("Dresses");

    List<String> WomenFootwear=new ArrayList<>();
    menFootwear.add("Bellies and Loafers");
    menFootwear.add("Foots");

    List<String> WomenEthnic=new ArrayList<>();
    menAccesories.add("Kurtas and Kurtis");
    menAccesories.add("Leggings");

    List<String> WomenAccesories=new ArrayList<>();
    menAccesories.add("Bags and wallets");
    menAccesories.add("Belts");

    List<String> WomenLingerie=new ArrayList<>();
    menAccesories.add("Night wear");
    menAccesories.add("Swim wear");

    womenListDataSubChild.put("Western wear",WomenWestern);//0
    womenListDataSubChild.put("Footwear",WomenFootwear);//1
    womenListDataSubChild.put("Ethnic wear",WomenEthnic);//2
    womenListDataSubChild.put("Lingerie",WomenLingerie);//3
    womenListDataSubChild.put("Accesories",WomenAccesories);//4





}`
    expListView.setAdapter(listAdapter);
    //Calling fuction for adding icon for Navigation Bar
    addingNavIconOnActionBar();

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
             //"Group Clicked " + listDataHeader.get(groupPosition),
             //Toast.LENGTH_SHORT).show();
            if(groupPosition==0)
            {
                Intent trendingIntent=new Intent(getApplicationContext(), Trending.class);
                startActivity(trendingIntent);
            }
            else if(groupPosition==1)
            {
                Intent trendingIntent=new Intent(getApplicationContext(), Looks.class);
                startActivity(trendingIntent);

            }

            return false;
        }
    });

 // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

 // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });


//  mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                       listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();`enter code here`
            return false;
        }
    });
}` 

enter code here
private Context _context;
List<String> _listDataHeader; // header titles
// child data in format of header title, child title
HashMap<String, List<String>> _listDataChild;

//sub sub category of men and women
private HashMap<String, List<String>> _menListDatasubChild,_womenListDatasubChild;

ExpandableListView expChildList;
ChildExpandableListAdapter childAdapter;
public ExpandableListAdapter(Context context,
        List<String> listDataHeader,
        HashMap<String, List<String>> listDataChild, HashMap<String, List<String>> menListDataSubChild, HashMap<String, List<String>> womenListDataSubChild) {
    // TODO Auto-generated constructor stub
    this._context=context;
    this._listDataChild=listDataChild;
    this._listDataHeader=listDataHeader;
    this._menListDatasubChild=menListDataSubChild;
    this._womenListDatasubChild=womenListDataSubChild;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

   // TextView txtListChild = (TextView) convertView
     //      .findViewById(R.id.ListHeader1);

    //calling sub level Adapter

    Log.e("groupPos", String.valueOf(groupPosition));
    Log.e("childText", childText);
    expChildList=(ExpandableListView) convertView.findViewById(R.id.expChildList);

    childAdapter=new ChildExpandableListAdapter(this._context,groupPosition);
    expChildList.setAdapter(childAdapter);
 // Listview Group click listener
    expChildList.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
             //"Group Clicked " + listDataHeader.get(groupPosition),
             //Toast.LENGTH_SHORT).show();

            return false;
        }
    });

 // Listview Group expanded listener
    expChildList.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {

        }
    });

 // Listview Group collasped listener
    expChildList.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {


        }
    });


//  mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // Listview on child click listener
    expChildList.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub

            return false;
        }
    });
    //txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {

    if(groupPosition==0)
        return 0;
    else if(groupPosition==1)
        return 0;
    else if(groupPosition==4)
        return 0;
    else
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@SuppressLint("InflateParams")
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group,null);
    }

    TextView lblListHeader = (TextView) convertView
           .findViewById(R.id.ListHeader);

    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    ImageView iconExpand = (ImageView) convertView.findViewById(R.id.icon_expand);
    ImageView iconCollapse = (ImageView) convertView.findViewById(R.id.icon_collapse);

    if(getChildrenCount(groupPosition)==0)
    {
    iconExpand.setVisibility(View.INVISIBLE);
    iconCollapse.setVisibility(View.INVISIBLE);
    }
else if(isExpanded)
{
    iconExpand.setVisibility(View.INVISIBLE);
    iconCollapse.setVisibility(View.VISIBLE);
}
else
{
    iconExpand.setVisibility(View.VISIBLE);
    iconCollapse.setVisibility(View.INVISIBLE);
}
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}


//********sub sub child adapter

public class ChildExpandableListAdapter extends BaseExpandableListAdapter {


    Context context;
    int _groupPosition;
    public ChildExpandableListAdapter(
            Context context, int groupPosition) {
        // TODO Auto-generated constructor stub
        this.context=context;

        this._groupPosition=groupPosition;

        Log.e("gP",""+_groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        if(_groupPosition==2)//men sub category
        {
            if(groupPosition==0)
                return "Clothing";
            else if(groupPosition==1)
                return "Footwear";
            else
                return "Accesories";
        }
        else if(_groupPosition==3)//female sub category
        {
            if(groupPosition==0)
                return "Western wear";
            else if(groupPosition==1)
                return "Footwear";
            else if(groupPosition==2)
                return "Ethnic wear";
            else if(groupPosition==2)
                return "Lingerie";
            else
                return "Accesories";

        }

        return null;
    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        //return this.__listDataChild.get(groupPosition)
        if(_groupPosition==2)
        {
            return _menListDatasubChild.get(getGroup(groupPosition)).get(childPosition);
        }
        else if(_groupPosition==3)
        {
            return _womenListDatasubChild.get(getGroup(groupPosition)).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        String headerTitle =(String)getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group1,null);
        }

        TextView lblListHeader = (TextView) convertView
               .findViewById(R.id.ListHeader1);
        Log.e("HeaderTitle",headerTitle);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;     
       }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.sub_explist_row, null);
        }

        TextView txtListChild = (TextView) convertView
               .findViewById(R.id.textView1);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }



enter code here




}

}

共有1个答案

颜嘉誉
2023-03-14

注意:您不应该将任何可滚动的小部件(比如listview组合在另一个listview)中。

ExpandableListView被设计为处理两层而不是更多,不要尝试它,它会让你的生活变得糟糕。

备选办法1:

 类似资料:
  • 但是现在,我希望这些接口实现也可用于扩展,例如。 我如何设置我的课程来实现这一点?(或者,反对这样做的固有缺陷或代码气味是什么?)

  • SPI 扩展接口仅用于系统集成,或 Contributor 扩展功能插件。

  • 本文向大家介绍Android实现可收缩和扩展的TextView,包括了Android实现可收缩和扩展的TextView的使用技巧和注意事项,需要的朋友参考一下 在Android原生的TextView的基础上,可收缩/扩展的TextView:PhilExpandableTextView。 实现原理:核心是控制TextView的max lines。在TextView的初始化阶段但尚未绘制出View的时

  • 问题内容: 我有2个Maven项目在他们各自的POM中共享一个JPA项目作为依赖项。JPA项目在2.1版上,并且成功实现了hibernateenvers。但是,我现在需要创建一个自定义的RevisionEntity,因为我需要审核其他属性,例如登录的用户ID。问题是: 一世。我不能在JPA项目中直接实现 ii。父maven项目的检索登录用户的实现不同。 真正的挑战实际上是实现RevisionLis

  • 问题内容: 为什么实现Runnable比从Thread类扩展更好的选择? 问题答案: 这样,您就可以 将 计算( what )与执行( when 和/或 how )分离。 使用或,您可以例如向提交许多工作/计算,这将使您计划工作。这是ExecutorService的摘录形式: 与直接使用线程相比,使用/ 给您更大的灵活性。

  • 本文向大家介绍PyTorch中的C++扩展实现,包括了PyTorch中的C++扩展实现的使用技巧和注意事项,需要的朋友参考一下 今天要聊聊用 PyTorch 进行 C++ 扩展。 在正式开始前,我们需要了解 PyTorch 如何自定义module。这其中,最常见的就是在 python 中继承torch.nn.Module,用 PyTorch 中已有的 operator 来组装成自己的模块。这种方式