gpt4 book ai didi

android - ExpandableListView 子项目在触摸时不会获得焦点

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:59:46 24 4
gpt4 key购买 nike

好的,所以我写了一个 ExpandableListView 和子类化的 BaseExpandableListAdapter...一切正常,除了我无法让 subview 在单击时获得焦点。如果我使用轨迹球,一切正常。但是,如果我尝试点击一个 child ,我将得不到任何反馈。

我试过设置 android:focusable、a​​ndroid:focusableInTouchMode 和 android:clickable(我也试过通过代码设置它)但我什么都做不了。有什么想法吗?

这是我的适配器代码:

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;
}
}

提前致谢!

最佳答案

好吧,我终于明白了!

它实际上与我的 XML 文件有关,而不是我的适配器(我显示适配器以表明我在创建 subview 时调用了 setFocusableInTouchMode)。

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

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

一旦我将其更改为 wrap_content,一切就开始正常工作了。

关于android - ExpandableListView 子项目在触摸时不会获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2651818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com