【发布时间】:2015-02-02 07:55:27
【问题描述】:
onChildClickListener 不适用于我的展开式列表。
子布局是通过代码从自定义 LinearLayout 生成的,它会从 xml 中添加一些其他子视图。
对于 xml,所有视图都设置为 clickable=false。 扩展布局(其他视图的父布局)没有设置为任何可点击的东西 - 明智的,因为它在代码中。
我相信我也应该将自定义线性布局设置为可点击 false,但由于它在代码中,我不确定是否这样做
这是我的 getChildView 方法:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = new ExtendedContentLinearLayout(context);
convertView).addViewToContentLayout(text);
ImageView leftImage = (ImageView) convertView.findViewById(R.id.child_left_image);
leftImage.setImageDrawable(context.getResources().getDrawable(R.drawable.folder_));
TextView childTextView = (TextView) convertView.findViewById(R.id.childTextView);
childTextView.setText(childText);
return convertView;
}
扩展线性布局:
public class ExtendedContentLinearLayout extends LinearLayout implements View.OnClickListener {
private View mTrigger;
private LinearLayout mContent;
public ExtendedContentLinearLayout(Context context) {
super(context);
mContent = new LinearLayout(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mTrigger = inflater.inflate(R.layout.nav_menu_board_expandable_children_outer, this, true);
this.addView(mContent);
mTrigger.setOnClickListener(this);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedContentLinearLayout(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
}
@Override
public void onClick(View v) {
App.disableTouchEventForDefaultDuration();
toggleVisibility();
}
public void addViewToContentLayout(View view) {
mContent.addView(view);
toggleVisibility();
}
private void toggleVisibility() {
if (mContent.getVisibility() == View.GONE) {
mContent.setVisibility(View.VISIBLE);
} else {
mContent.setVisibility(View.GONE);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mContent = null;
mTrigger = null;
}
}
我添加到自定义布局的子视图 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:animateLayoutChanges="true"
android:layout_gravity="center_vertical"
android:clickable="false"
android:gravity="center_vertical"
android:id="@+id/triggerLayout"
android:background="@color/da_nav_drawer_dark"
android:layout_height="60dp">
<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="20dp"
android:clickable="false"
android:id="@+id/child_left_image"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:textColor="@color/da_white"
android:id="@+id/childTextView" />
</LinearLayout>
【问题讨论】:
标签: android android-linearlayout expandablelistview