好吧,我正在尝试创建一个像 Spotify 那样的 ExpandableListView...但是我不知道如何禁用 LinearLayout 使其像按钮一样工作(展开列表)我创建了一个应该描述的图像我喜欢什么。我希望有可能将对文本/图像(父级)的点击作为正常交互进行处理。单击右键应该像在 Spotify 中一样展开列表...
最佳答案
这是一个有点老的问题,但这个答案可能会对某人有所帮助。
如果你想通过点击特定的 Button
或其他一些 View
展开/折叠组,你必须在 getGroupView
中获取那个 Button Adapter 类中的方法。然后,在 Button
的 onClick
方法中,要么将 parent
转换为 ExpandableListView
,要么传递 List 的创建适配器时在构造函数中引用。
我更喜欢第一种方法。这是代码,假设您有一个 TextView
和一个作为箭头的 ImageView
。我也添加了更改箭头状态。
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded,
View convertView, final ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.left_drawer_list_group, parent, false);
}
TextView listHeaderText = (TextView) convertView
.findViewById(R.id.left_menu_list_header_text);
ImageView listHeaderArrow = (ImageView) convertView.findViewById(R.id.left_menu_list_header_arrow);
listHeaderText.setText(headerTitle);
//Set the arrow programatically, so we can control it
int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float;
listHeaderArrow.setImageResource(imageResourceId);
listHeaderArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition);
else ((ExpandableListView) parent).expandGroup(groupPosition, true);
}
});
return convertView;
}
此外,您希望在 onGroupClick
监听器中禁用展开/折叠。
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
//Do some other stuff, but you shall not expand or collapse
return true;
}
还有另一种方法,但相当糟糕,那就是将整个 Adapter
类复制到创建 ExpandableListView
并设置 Adapter 的类中
。但不要那样做。
严重地! ;)
关于android - ExpandableListView 仅在特定 Button 上展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14587973/