点击侦听器上的可扩展列表子项无法正常工作

时间:2015-02-02 07:55:27

标签: android android-linearlayout expandablelistview

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

扩展LinearLayout:

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>

2 个答案:

答案 0 :(得分:0)

你在listview上试过setOnChildClickListener听众吗?获取更多帮助Here,同时查看this

答案 1 :(得分:0)

我必须将父布局设置为可通过代码点击:

parent.setClickable(真);

解决了它。

感谢您提供所有帮助