AndroidMultilevel expandableListView,第4级

时间:2016-01-25 21:16:05

标签: java android listview expandablelistview

我正在努力使用多级可扩展ListViews。我设法制作一个3级可扩展ListView,它是:

- GrandParent ---父母 -----孩子

但每当我尝试添加第4个子孩子时,视图似乎出现在最后一个级别之后。以下是我用来实现这一目标的要素:

CustomExpandableListView.java

    public class CustomExpandableListView extends ExpandableListView {

    public CustomExpandableListView(Context context, AttributeSet attrs,
                                    int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomExpandableListView(Context context) {
        super(context);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDetachedFromWindow() {
        try {
            super.onDetachedFromWindow();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}

ExpandableListAdapter.java

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataHeader; // header titles
    private HashMap<String, List<ExpandableItem>> listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                      HashMap<String, List<ExpandableItem>> listChildData) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @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) {

        if (listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getNumberOfChildren() > 0) {

            DPPCustomExpandableListView subList = new DPPCustomExpandableListView(context);
            List<ExpandableItem> list = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getItems();
            ArrayList<String> headers = new ArrayList<String>();
            HashMap<String, List<ExpandableItem>> newMap = new HashMap<>();
            String name = "Expandable";
            headers.add(name);
            newMap.put(name, list);

            MultiExpandableListAdapter multiAdapter = new MultiExpandableListAdapter(context, headers, newMap);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            subList.setLayoutParams(params);
            subList.setAdapter(multiAdapter);
            MySalesFragment currentFragment = (MySalesFragment) ((MainActivity) context).getFragmentManager().findFragmentById(R.id.container);

            if (currentFragment.getCurrentPagerFragment().getClass().equals(SalesDeclarationFragment.class)) {
                SalesDeclarationFragment salesDeclarationFragment = (SalesDeclarationFragment) currentFragment.getCurrentPagerFragment();
                subList.setOnChildClickListener(salesDeclarationFragment);
            }
            return subList;
        }
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);

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

        //txtListChild.setText(childText);

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {

        List<ExpandableItem> items = listDataChild.get(listDataHeader.get(groupPosition));
        return items;
    }

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

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

    @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.sales_declaration_expandable_list_view_group_header, null);
        }


        return convertView;
    }

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

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

MultiExpandableListAdapter.java

    public class MultiExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataHeader; // header titles
    private HashMap<String, List<ExpandableItem>> listDataChild;
    public static int count = 0;

    public MultiExpandableListAdapter(Context context, List<String> listDataHeader,
                                      HashMap<String, List<ExpandableItem>> listChildData) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @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) {


        if(listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getNumberOfChildren() > 0) {

 subList = new DPPNLevelCustomEpandableListView(context);
            List<ExpandableItem> list = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getItems();
            ArrayList<String> headers =  new ArrayList<String>();

            HashMap<String, List<ExpandableItem>> newMap = new HashMap<>();
            headers.add(name);
            newMap.put(name, list);

            MultiExpandableListAdapter multiAdapter = new MultiExpandableListAdapter(context, headers, newMap);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            subList.setLayoutParams(params);
            subList.setAdapter(multiAdapter);
            subList.setBackgroundColor(context.getResources().getColor(R.color.loyalty_store_theme_yellow));
            MySalesFragment currentFragment = (MySalesFragment)((MainActivity)context).getFragmentManager().findFragmentById(R.id.container);

            if (currentFragment.getCurrentPagerFragment().getClass().equals(SalesDeclarationFragment.class)) {
                SalesDeclarationFragment salesDeclarationFragment = (SalesDeclarationFragment)currentFragment.getCurrentPagerFragment();
                subList.setOnChildClickListener(salesDeclarationFragment);
            }
            return subList;
        }

        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);
        convertView.setBackgroundColor(context.getResources().getColor(R.color.cpb_blue));


        return convertView;


    }


    @Override
    public int getChildrenCount(int groupPosition) {
        int count = this.listDataChild.get(listDataHeader.get(groupPosition)).size();
        return count;
    }

    @Override
    public Object getGroup(int groupPosition) {

        List<ExpandableItem> items = listDataChild.get(listDataHeader.get(groupPosition));
        return items;
    }

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

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

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

        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.sales_declaration_expandable_listview_item, null);
        convertView.setBackgroundColor(context.getResources().getColor(R.color.cpb_green));
        return convertView;
    }

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

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

ExpandableItem.java

    public interface ExpandableItem {
    int getNumberOfChildren();
    ArrayList<ExpandableItem> getItems();
    void addExpandableItem(ExpandableItem item);
    String getName();
}

ListTextItem.java

    public class ListTextItem implements ExpandableItem{

    private ArrayList<ExpandableItem> items;
    private String name;

    public ListTextItem(String name){
        this.name = name;
        items = new ArrayList<ExpandableItem>();
    }

    @Override
    public int getNumberOfChildren() {
        return items.size();
    }

    public ArrayList<ExpandableItem> getItems() {
        return items;
    }

    public void addExpandableItem(ExpandableItem itemToAdd) {
        items.add(itemToAdd);
    }

    public String getName() {
        return name;
    }
}

ExampleFragment.java

    public class ExampleFragment extends Fragment implements ExpandableListView.OnChildClickListener {

    private View root;

    ExpandableListAdapter listAdapter;

    @InjectView(R.id.sales_declaration_list)
    ExpandableListView expListView;

    List<String> listDataHeader;
    HashMap<String, List<ExpandableItem>> listDataChild;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        this.root = inflater.inflate(R.layout.fragment_sales_declaration, container, false);
        ButterKnife.inject(this, root);
        return root;
    }

    @Override
    public void onStart() {
        super.onStart();

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
        expListView.setGroupIndicator(null);
        expListView.setDividerHeight(0);
        expListView.setOnChildClickListener(this);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<ExpandableItem>>();

        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<ExpandableItem> top250 = new ArrayList<ExpandableItem>();
        top250.add(new ListTextItem("The Shawshank Redemption"));
        top250.add(new ListTextItem("The Godfather"));
        top250.add(new ListTextItem("The Godfather: Part II"));
        top250.add(new ListTextItem("Pulp Fiction"));
        top250.add(new ListTextItem("The Good, the Bad and the Ugly"));
        top250.add(new ListTextItem("The Dark Knight"));
        top250.add(new ListTextItem("12 Angry Men"));

        List<ExpandableItem> nowShowing = new ArrayList<ExpandableItem>();
        nowShowing.add(new ListTextItem("The Conjuring"));
        nowShowing.add(new ListTextItem("Despicable Me 2"));
        nowShowing.add(new ListTextItem("Turbo"));
        nowShowing.add(new ListTextItem("Grown Ups 2"));
        nowShowing.add(new ListTextItem("Red 2"));
        nowShowing.add(new ListTextItem("The Wolverine"));
        nowShowing.add(new ListTextItem("The Wolverine2"));
        nowShowing.add(new ListTextItem("The Wolverine3"));
        nowShowing.add(new ListTextItem("The Wolverine4"));


        List<ExpandableItem> comingSoon = new ArrayList<ExpandableItem>();
        comingSoon.add(new ListTextItem("2 Guns"));
        comingSoon.add(new ListTextItem("The Smurfs 2"));
        comingSoon.add(new ListTextItem("The Spectacular Now"));
        comingSoon.add(new ListTextItem("The Canyons"));
        comingSoon.add(new ListTextItem("Europa Report"));

        for(ExpandableItem item : comingSoon) {
            top250.get(1).addExpandableItem(item);
        }
        for(ExpandableItem item : nowShowing) {
            top250.get(1).getItems().get(0).addExpandableItem(item);
        }

        for(ExpandableItem item : comingSoon) {
            top250.get(3).addExpandableItem(item);
        }
        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        if(!(((ExpandableItem)parent.getExpandableListAdapter().getChild(groupPosition, childPosition)).getNumberOfChildren() > 0)){

            //Toast.makeText(getActivity(), listAdapter.getChild(groupPosition, childPosition).toString(), Toast.LENGTH_SHORT).show();
            LSFragmentManager fragmentManager = new LSFragmentManager(getActivity());
            fragmentManager.showFragment(Fragment2.class.getName());
        }
        return false;
    }

}

抱歉,对于这篇长篇文章,希望有人能帮助我!

0 个答案:

没有答案