安卓可扩展列表视图洗牌儿童 [英] Android Expandable Listview Shuffles Children

查看:42
本文介绍了安卓可扩展列表视图洗牌儿童的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在使用Android Expanable Listview,并在它里面用不同的视图膨胀Childs。我遇到的问题是,当我展开一个视图,然后打开另一个父视图时,布局中的子视图变得混乱,并在代码中放大了错误的布局。以下是我为这两个项目编写的示例代码。

这是我的活动。

 public class MainActivity extends Activity {

List<String> groupList;
List<Integer> childList;
Map<String, List<Integer>> laptopCollection;
ExpandableListView expListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    createGroupList();

    createCollection();

    expListView = (ExpandableListView) findViewById(R.id.laptop_list);
    final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
            this, groupList, laptopCollection);
    expListView.setAdapter(expListAdapter);
    // setGroupIndicatorToRight();

    // setGroupIndicatorToRight();

    expListView.setOnChildClickListener(new OnChildClickListener() {

        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            final String selected = (String) expListAdapter.getChild(
                    groupPosition, childPosition);
            Toast.makeText(getBaseContext(), selected,  
Toast.LENGTH_LONG)
                    .show();

            return true;
        }
    });

    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        private int lastExpandedGroupPosition;

        @Override
        public void onGroupExpand(int groupPosition) {
            // TODO Auto-generated method stub

            if (groupPosition != lastExpandedGroupPosition) {
                expListView.collapseGroup(lastExpandedGroupPosition);
            }

            lastExpandedGroupPosition = groupPosition;
            expListAdapter.notifyDataSetChanged();

        };
    });
}

private void createGroupList() {
    groupList = new ArrayList<String>();
    groupList.add("General Settings");
    groupList.add("Name");
    groupList.add("Password");
    groupList.add("Notifications");
    groupList.add("Profile Settings");
    groupList.add("Change Picture");
    groupList.add("Disable Account");
    /*
     * groupList.add("Sony"); groupList.add("HCL");
     * groupList.add("Samsung");
     */
}

private void createCollection() {
    // preparing laptops collection(child)
    // int[] hpModels = { R.layout.settings_notification,
    // R.layout.settings_newpassword,
    // R.layout.settings_name};
    // int[] dellModels = { R.layout.settings_name ,
    // R.layout.settings_newpassword , R.layout.settings_notification};
    // String[] hclModels = { "HCL S2101", "HCL L2102", "HCL V2002" };
    //
    //

    int[] emptyList = {};
    int[] password = { R.layout.settings_password,
            R.layout.settings_newpassword,        R.layout.settings_repeatpassword };
    int[] noti = { R.layout.settings_notis };
    int[] pic = { R.layout.settings_displaypicture };
    int[] dellModels = { R.layout.settings_name,
            R.layout.settings_newpassword,   R.layout.settings_notification };
    // String[] lenovoModels = { "IdeaPad Z Series", "Essential G Series",
    // "ThinkPad X Series", "Ideapad Z Series" };
    // String[] sonyModels = { "VAIO E Series", "VAIO Z Series",
    // "VAIO S Series", "VAIO YB Series" };
    //
    // String[] samsungModels = { "NP Series", "Series 5", "SF Series" };

    laptopCollection = new LinkedHashMap<String, List<Integer>>();

    for (String laptop : groupList) {
        if (laptop.equals("General Settings")) {
            loadChild(emptyList);
        } else if (laptop.equals("Name"))
            loadChild(emptyList);
        else if (laptop.equals("Password"))
            loadChild(password);
        else if (laptop.equals("Notifications"))
            loadChild(noti);

        else {
            loadChild(emptyList);
        }
        /*
         * else if (laptop.equals("Sony")) loadChild(sonyModels); else if
         * (laptop.equals("HCL")) loadChild(hclModels); else if
         * (laptop.equals("Samsung")) loadChild(samsungModels); else
         * loadChild(lenovoModels);
         */

        laptopCollection.put(laptop, childList);
    }
}

private void loadChild(int[] laptopModels) {
    childList = new ArrayList<Integer>();
    for (Integer model : laptopModels)
        childList.add(model);
}

/*
 * private void setGroupIndicatorToRight() { Get the screen width
 * DisplayMetrics dm = new DisplayMetrics();
 * getWindowManager().getDefaultDisplay().getMetrics(dm); int width =
 * dm.widthPixels;
 * 
 * expListView.setIndicatorBounds(width - getDipsFromPixel(35), width -
 * getDipsFromPixel(5)); }
 */

// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}

}

这是可扩展列表视图的我的适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Activity context;
private Map<String, List<Integer>> laptopCollections;
private List<String> laptops;

public ExpandableListAdapter(Activity context, List<String> laptops,
        Map<String, List<Integer>> laptopCollection) {
    this.context = context;
    this.laptopCollections = laptopCollection;
    this.laptops = laptops;
}

public Object getChild(int groupPosition, int childPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}

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

public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final int laptop = (Integer) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(laptop ,null);
    }

   TextView item = (TextView) convertView.findViewById(R.id.laptop);
    item.setText(laptop);
    return convertView;
}

public int getChildrenCount(int groupPosition) {
    return laptopCollections.get(laptops.get(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    return laptops.get(groupPosition);
}

public int getGroupCount() {
    return laptops.size();
}

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

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String laptopName = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.grouup_item,
                null);
    }
    TextView item = (TextView) convertView.findViewById(R.id.laptop);
    item.setTypeface(null, Typeface.BOLD);
    item.setText(laptopName);
    return convertView;
}

public boolean hasStableIds() {
    return true;
}

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



}

推荐答案

试试这个.. 在ExpanablelistAdapter Expandes BaseExpanablelistAdapter下写入此内容

  public int getViewTypeCount() 
        {
            return 2;//layout number
        }

        public int getItemViewType(int groupposition,int childposition) {


            if (getChildId(groupposition, childposition)!=3) //set condition when it changed
                return 1;//retun layout number


            else 
                return 0;//retun layout number
        }

这篇关于安卓可扩展列表视图洗牌儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆