扩展列表视图中的Andr​​oid:编辑特定子/视图问题 [英] Expandable List View in Android: edit specific child/view issue

查看:224
本文介绍了扩展列表视图中的Andr​​oid:编辑特定子/视图问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挫折和越来越接近解决问题的天后,我从来没有真正成功地做到这一点。我的想法是编辑特定的孩子的int onChildClick方法的颜色(和其他属性)。在这个例子中,我用一个TextView(但我也想用复选框的expandablelistview并控制孩子的选中状态)。

after days of frustration and getting close to solving the problem, I never really managed to do it. My idea is to edit the color (and other properties) of a specific child int the onChildClick method. For this example I was using a TextView (but I also wanted to use an expandablelistview of checkboxes and control the Checked state of a child).

适配器是这样:

    package org.ksinstitute.arsenieboca;

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

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

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

    //ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();


    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> 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(final 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);
        }

        //or CheckBox
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        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) {
        return this._listDataHeader.get(groupPosition);
    }

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

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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


}

和主要活动的部分:

    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                View _lastColored;
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {
                    Toast.makeText(
                            getApplicationContext(),
                            listDataHeader.get(groupPosition)
                                    + " : "
                                    + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition)
                                    + ", "
                                    + groupPosition + "." + childPosition + "\n"
                                    + "id=" + (int)id, Toast.LENGTH_SHORT)
                            .show();

                    **v.setBackgroundColor(Color.BLUE);**

                    listAdapter.notifyDataSetChanged();


                    return false;
                }
            });

好了,有什么用发生在: v.setBackgroundColor(Color.BLUE); ?我收到以下行为:

当我点击一个孩子,原来自己变成蓝色,但在对方类另一个孩子(好像几乎是随机的)变成蓝色!

When I click a child, it turns itself into blue, but in each other category another child (seems like almost random) turns blue!

我怎么只能编辑pressed孩子的财产,这样用户就知道他是什么$ $ p pssed,

How can I edit the property of ONLY the pressed child, so the user knows what he pressed,

感谢您!

推荐答案

您可以参考以下逻辑:

/* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // selected item's color (semi-transparent)
            for (int i = 0; i < parent.getChildCount(); i++) {
                if (position == i) {
                    parent.getChildAt(i).setBackgroundColor(Color.parseColor("#66868686"));
                } else {
                    parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
            }
        }
    }

然后在的onCreate ,可以使用以下内容:

...
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, itemTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
...

希望这有助于!

这篇关于扩展列表视图中的Andr​​oid:编辑特定子/视图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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