我正在尝试创建一个类似于Spotify的可扩展列表(ExpandableListView)。但是,我不知道如何禁用LinearLayout以使其不像按钮一样操作(展开列表)。我创建了一张图片,它应该描述我喜欢的东西。我希望能够像正常交互一样处理点击文本/图像(父级)。右侧按钮的点击应该像Spotify一样展开列表...
我正在尝试创建一个类似于Spotify的可扩展列表(ExpandableListView)。但是,我不知道如何禁用LinearLayout以使其不像按钮一样操作(展开列表)。我创建了一张图片,它应该描述我喜欢的东西。我希望能够像正常交互一样处理点击文本/图像(父级)。右侧按钮的点击应该像Spotify一样展开列表...
这是一个有点老的问题,但这个答案可能会帮助到一些人。
如果您想通过单击特定的
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
m_ListView = new ExpandableListView(getActivity());
m_ListView.setId(android.R.id.list);
m_ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
m_ListView.setMultiChoiceModeListener(m_MultiChoiceModeListener);
m_ChannelListAdapter = new ChannelListAdapter(getActivity(), this);
m_ListView.setAdapter(m_ChannelListAdapter);
m_ListView.setGroupIndicator(null);
m_ListView.setOnGroupClickListener(this);
return m_ListView;
}
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null)
{
view = inflater.inflate(R.layout.layout_channelwithimage, viewGroup, false);
}
TextView textView = (TextView) view.findViewById(R.id.labelwithimage);
textView.setText(getGroup(i).toString());
ImageButton imbu = (ImageButton) view.findViewById(R.id.imageButton);
//imbu.setOnClickListener(this);
imbu.setFocusable(false);
return view;
}
虽然不是特别优雅的解决方案,但是它帮了我一个大忙:
我保留了原始的groupIndicator。我喜欢它之前的行为。
在groupItem布局中,我只是用空视图阻挡了原来的空间,这样原始的groupIndicator仍然可以点击:
<LinearLayout>
....
<!--just dummy space underneath the default expand_collapse group icon - this way original expand collapse behavior is preserved on the icon-->
<View
android:layout_width="25dp"
android:layout_height="match_parent">
</View>
<!-- text view will have actionListener -->
<TextView
android:id="@+id/category_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
.... />
....
</LinearLayout>
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public interface OnCategoryActionListener {
boolean onCategorySelected(long categoryId);
}
....
public ExpandableListAdapter(Activity context, Category rootCategory,
OnCategoryActionListener callBack) {
this.context = context;
this.rootCategory = rootCategory;
this.callBack = callBack;
}
....
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View
convertView, ViewGroup parent) {
String categoryName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category, null);
}
TextView item = (TextView) convertView.findViewById(R.id.category_name);
item.setTypeface(null, Typeface.BOLD);
item.setText(categoryName);
item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
callBack.onCategorySelected(getGroupId(groupPosition));
}
});
.....
expListAdapter = new ExpandableListAdapter(getActivity(), this.rootCategory,
new OnCategoryActionListener());
expListView = (ExpandableListView) getActivity().findViewById(R.id.categories_list);
expListView.setAdapter(expListAdapter);