ExpandableListView中的EditText

时间:2016-08-11 07:33:17

标签: java android expandablelistview

我在组项目的EditText中添加了ExpandableListView,之后当我点击组项目时,没有显示包含子项目的列表。可能是什么问题?在这种情况下,是否可以扩展此列表?或者禁止在EditText中的小组项目中添加ExpandedListView

适配器代码:

public class ClaimEqpExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    public List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ClaimEqpExpandableListAdapter(Context context, List<String> listDataHeader,
                                         HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    public ClaimEqpExpandableListAdapter(Context context, List<String> listDataHeader) {
        this._context = context;
        this._listDataHeader = listDataHeader;
    }


    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.claim_explist_item, null);
        }
        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.claim_eq_list_group, null);
           // convertView = infalInflater.inflate(R.layout.claim_list_group, null);
        }

        TextView tvWorkNumber = (TextView) convertView
                .findViewById(R.id.tvWorkNumber);
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tvClaimWorkName);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        int displayPosition=groupPosition+1;
        tvWorkNumber.setText(String.valueOf(displayPosition));

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    public void updateData(List<String> groups,
                           HashMap<String, List<String>> children) {
        this._listDataHeader = groups;
        this._listDataChild = children;
    }
}

claim_eq_list_group.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/tvWorkNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="10dp"
        android:text="1"
        android:textSize="@dimen/doc_title_font_size" />

    <TextView
        android:id="@+id/tvClaimWorkName"
        android:layout_width="match_parent"
        android:layout_height="26dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/tvWorkNumber"
        android:textSize="@dimen/doc_title_font_size" />

    <EditText
    android:id="@+id/tvClaimEquip"
    android:layout_width="80dp"
    android:layout_height="26dp"
    android:layout_marginRight="60dp"
    android:layout_marginTop="10dp"
    android:layout_toRightOf="@+id/tvClaimWorkName"
    android:inputType="number"
    android:textSize="@dimen/doc_title_font_size" />

    </RelativeLayout>

claim_explist_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="5dp"
        android:textSize="17dip" />


</LinearLayout>

3 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的三个方法:

1)首先使EditText不可聚焦,然后在键入文本时使其可聚焦。

适配器中的代码:

editText.setFocusable(false);
editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        editText.setFocusableInTouchMode(true);
        return false;
    }
});
editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable s) {
        editText.setFocusable(false);
        editText.setFocusableInTouchMode(false);
    }

2)自定义Expandable indicator(按钮),在其上设置onClickListener,这将展开并折叠ExpandableListView

适配器中的代码:

btnIndicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExpandableListView mExpandableListView = (ExpandableListView) mParetn;
                if(mExpandableListView.isGroupExpanded(mGroupPosition)){
                    mExpandableListView.collapseGroup(mGroupPosition);
                }else{
                    mExpandableListView.expandGroup(mGroupPosition);
                }
            }
        });

3)创建EditTextTextView而不是Button。在按下时会出现带有EditText的Alert Dialog,用户可以在其中插入输入。

答案 1 :(得分:0)

groupheader中的editText是可聚焦的,并且正在窃取所有点击事件。尝试将focusable设置为false: android:focusable="false"yourEditText.setFocusable(false);

答案 2 :(得分:0)

在xml Edittext中设置以下属性。

android:focusable="false"
android:focusableInTouchMode="true"
android:descendantFocusability="blocksDescendants"

如果仍然无效,请添加listview

android:descendantFocusability="beforeDescendants

并在该活动中显示添加。

android:windowSoftInputMode="adjustPan"