【问题标题】:How to create ExpandableListView keeping some child fixed?如何创建 ExpandableListView 保持一些孩子固定?
【发布时间】:2014-11-12 13:11:01
【问题描述】:

我想创建一个可扩展的列表视图保持一些孩子可见,我想在点击时显示休息。 如果有任何自定义元素或任何教程,请建议在这种情况下最好的方法是什么。

非常感谢,

【问题讨论】:

  • 可以有更多列表,以同样的方式,就像家庭需求是一个列表,电影可以是另一个。厨具,MOP 是 HomeNeeds 的子项

标签: android-listview expandablelistview android expandablelistadapter


【解决方案1】:

这将对您有所帮助。 适配器类:-

public class MyExpandableAdapter extends BaseExpandableListAdapter {
    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    public MyExpandableAdapter(ArrayList<String> parents,
            ArrayList<Object> childern) {
        this.parentItems = parents;
        this.childtems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) {
        this.inflater = inflater;
        this.activity = activity;
    }

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

        child = (ArrayList<String>) childtems.get(groupPosition);

        TextView textView = null;


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group, null);
        }

        textView = (TextView) convertView.findViewById(R.id.textView1);
        textView.setText(child.get(childPosition));

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(activity, child.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.row, null);

        }

        ((CheckedTextView) convertView).setText(parentItems
                .get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);

        return convertView;

    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return null;

    }

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

        return 0;

    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return ((ArrayList<String>) childtems.get(groupPosition)).size();

    }

    @Override
    public Object getGroup(int groupPosition) {

        return null;

    }

    @Override
    public int getGroupCount() {

        return parentItems.size();

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

        super.onGroupCollapsed(groupPosition);

    }

    @Override
    public void onGroupExpanded(int groupPosition) {

        super.onGroupExpanded(groupPosition);

    }

    @Override
    public long getGroupId(int groupPosition) {

        return 0;

    }

    @Override
    public boolean hasStableIds() {

        return false;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return false;

    }

}

MainActivity 在这里。

public class MainActivity extends ExpandableListActivity {

    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // this is not really necessary as ExpandableListActivity contains
        // an ExpandableList
        // setContentView(R.layout.main);

        ExpandableListView expandableList = getExpandableListView(); // you
                                                                        // can
                                                                        // use
                                                                        // (ExpandableListView)
                                                                        // findViewById(R.id.list)

        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        setGroupParents();
        setChildData();

        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
                childItems);
        adapter.setInflater(
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                this);
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    public void setGroupParents() {
        parentItems.add("Android");
        parentItems.add("Core Java");
        parentItems.add("Desktop Java");
        parentItems.add("Enterprise Java");
    }

    public void setChildData() {

        // Android
        ArrayList<String> child = new ArrayList<String>();
        child.add("Core");
        child.add("Games");
        childItems.add(child);

        // Core Java
        child = new ArrayList<String>();
        child.add("Apache");
        child.add("Applet");
        child.add("AspectJ");
        child.add("Beans");
        child.add("Crypto");
        childItems.add(child);

        // Desktop Java
        child = new ArrayList<String>();
        child.add("Accessibility");
        child.add("AWT");
        child.add("ImageIO");
        child.add("Print");
        childItems.add(child);

        // Enterprise Java
        child = new ArrayList<String>();
        child.add("EJB3");
        child.add("GWT");
        child.add("Hibernate");
        child.add("JSP");
        childItems.add(child);
    }
}

【讨论】:

    【解决方案2】:

    在开始时打开您要修复的组,然后执行此操作,指定组位置

    expandableList.setOnGroupClickListener(new OnGroupClickListener() {
      @Override
      public boolean onGroupClick(ExpandableListView parent, View v,
                                  int groupPosition, long id) { 
        if(groupPosition==your group position){
        return true; // This way the expander cannot be collapsed 
        }else{
              return false;     
         }
    
      }
    });
    

    【讨论】:

      【解决方案3】:

      根据您的需要覆盖 ExpandableListView 的 onGroupCollapsed 和 onGroupExpanded。
      已编辑:另外:实现上述 setOnGroupClickListener,将 groupID 存储在您的视图中,并抑制 onGroupCollapsed 中的折叠。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 2015-05-27
        • 1970-01-01
        • 1970-01-01
        • 2021-03-25
        相关资源
        最近更新 更多