ExpandableListView子项没有得到焦点时感动焦点、ExpandableListView

2023-09-06 03:31:37 作者:我们的爱太搁浅 //.

好了,我写了一个ExpandableListView和子类BaseExpandableListAdapter ......一切工作正常,只是我不能让孩子意见,采取集中点击时。如果我使用轨迹球一切工作正常。但是,如果我尝试点击一个孩子,我无论如何没有得到反馈。

Ok, so I wrote an ExpandableListView and subclassed BaseExpandableListAdapter... Everything works fine except I cannot get the child views to take focus when clicked. If I use the trackball everything works fine. But if I try to click on a child I get no feedback whatsoever.

我已经尝试设置的android:作为焦点,机器人:focusableInTouchMode,和android:点击(我自己也尝试通过code设置此),但我不能得到任何工作。任何想法?

I have tried setting android:focusable, android:focusableInTouchMode, and android:clickable (and I have also tried setting this via code) but I can't get anything to work. Any ideas?

下面是我的适配器code:

Here is my Adapter code:

public class ExpandableAppAdapter extends BaseExpandableListAdapter
{
    private PackageManager m_pkgMgr;
    private Context m_context;
    private List<ApplicationInfo> m_groups;
    private List<List<ComponentName>> m_children;

    public ExpandableAppAdapter(Context context, List<ApplicationInfo> groups, List<List<ComponentName>> children)
    {
        m_context = context;
        m_pkgMgr = m_context.getPackageManager();
        m_groups = groups;
        m_children = children;
    }

    @Override
    public Object getChild(int groupPos, int childPos) 
    {
        return m_children.get(groupPos).get(childPos);
    }

    @Override
    public long getChildId(int groupPos, int childPos) 
    {
        return childPos;
    }

    @Override
    public int getChildrenCount(int groupPos) 
    {
        return m_children.get(groupPos).size();
    }

    @Override
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) 
    {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(m_context);
            convertView = inflater.inflate(R.layout.expandable_app_child_row, null);
        }

        ComponentName child = (ComponentName)getChild(groupPos, childPos);
        TextView txtView = (TextView)convertView.findViewById(R.id.item_app_pkg_name_id);
        if (txtView != null)
            txtView.setText(child.getPackageName());

        txtView = (TextView)convertView.findViewById(R.id.item_app_class_name_id);
        if (txtView != null)
            txtView.setText(child.getClassName());

        convertView.setFocusableInTouchMode(true);
        return convertView;
    }

    @Override
    public Object getGroup(int groupPos) 
    {
        return m_groups.get(groupPos);
    }

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

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

    @Override
    public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
    {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(m_context);
            convertView = inflater.inflate(R.layout.expandable_app_group_row, null);
        }

        ApplicationInfo group = (ApplicationInfo)getGroup(groupPos);
        ImageView imgView = (ImageView)convertView.findViewById(R.id.item_selection_icon_id);
        if (imgView != null)
        {
            Drawable img = m_pkgMgr.getApplicationIcon(group);
            imgView.setImageDrawable(img);
            imgView.setMaxWidth(20);
            imgView.setMaxHeight(20);
        }

        TextView txtView = (TextView)convertView.findViewById(R.id.item_app_name_id);
        if (txtView != null)
            txtView.setText(m_pkgMgr.getApplicationLabel(group));

        return convertView;
    }
    @Override
    public boolean hasStableIds() 
    {
        return false;
    }
    @Override
    public boolean isChildSelectable(int groupPos, int childPos) 
    {
        return true;
    }
}

在此先感谢!

Thanks in advance!

推荐答案

好吧,我终于想通了!

Ok, I finally figured it out!

实际上,它不得不与我的XML文件,而不是我的适配器(我显示适配器以显示我打电话 setFocusableInTouchMode 创建子视图时)。

It actually had to do with my XML file rather than my adapter (I displayed the adapter to show that I was calling setFocusableInTouchMode when the child view was created).

我使用教程(不记得是哪一个)让我设置子高度:

The tutorial I was using (can't remember which one it was) had me set the child height to:

android:layout_height="?android:attr/listPreferredItemHeight"

当我改变它只是WRAP_CONTENT一切开始工作的罚款。

As soon as I changed it to just wrap_content everything started working fine.