编辑文本可扩展ListView而不回收输入和动态子视图不起作用

时间:2016-01-14 14:26:40

标签: android listview

我在我的expandablelistview的childView中编辑了文本,但是当我在其中键入一些值时,该值与其他edittext重复了

我想知道也许是因为expandablelistview的可重用性,编辑文本也可以重用,我应该禁用它的可重用性吗?如果是这样的话?

我的子视图也是动态的,当孩子没有图像或标题时,我想将可见性设置为已消失。

如果打开其子项包含所有图像和标题的组,则会正确显示,但是当我打开其子项没有图像或标题的groupview时,我又回去了,图像和标题消失了 那是为什么?

这是我的活动

public class HelpActivity extends AppCompatActivity {

    private ExpandableListView mExpandableListView;
    private List<HelpMenuModel> mHelpMenuModels;
    private Map<Integer, List<HelpSubMenuModel>> mMapSubMenu;
    private HelpAdapter mHelpAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goapp_help_layout);

        renderView();

        init();

    }

    private void renderView() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandlistview_help_goapp);

    }

    private void init() {
        populateData();
        mHelpAdapter = new HelpAdapter(this, mHelpMenuModels, mMapSubMenu);
        mExpandableListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
        mExpandableListView.setAdapter(mHelpAdapter);
        //setGroupIndicatorToRight(mExpandableListView);


    }

这是我的可扩展列表适配器

public class HelpAdapter implements ExpandableListAdapter  {

    private static final String TAG = HelpAdapter.class.getSimpleName();
    private Context mContext;
    private List<HelpMenuModel> mHelpMenuModels;
    private Map<Integer, List<HelpSubMenuModel>> mMapSubMenu;

    private Map<Integer, HelpSubMenuModel> mapData;

    public HelpAdapter(Context context, List<HelpMenuModel> helpMenuModels, Map<Integer, List<HelpSubMenuModel>> mapSubMenu) {
        this.mContext = context;
        this.mHelpMenuModels = helpMenuModels;
        this.mMapSubMenu = mapSubMenu;

        mapData = new HashMap<>();
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return mMapSubMenu.get(mHelpMenuModels.get(groupPosition).getIdMenu()).size();
    }

    @Override
    public HelpMenuModel getGroup(int groupPosition) {
        return mHelpMenuModels.get(groupPosition);
    }

    @Override
    public HelpSubMenuModel getChild(int groupPosition, int childPosition) {
        return mMapSubMenu.get(mHelpMenuModels.get(groupPosition).getIdMenu()).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        HelpMenuModel helpMenuModel = getGroup(groupPosition);
        HelpMenuHolder helpMenuHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.goapp_help_group_layout, parent, false);
            helpMenuHolder = new HelpMenuHolder(convertView);
            convertView.setTag(helpMenuHolder);
        } else {
            helpMenuHolder = (HelpMenuHolder) convertView.getTag();
        }

        helpMenuHolder.mImageViewMenuIcon.setImageResource(helpMenuModel.getIconMenu());
        helpMenuHolder.mTextViewMenuTitle.setText(helpMenuModel.getTitleMenu());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final HelpSubMenuModel helpSubMenuModel = getChild(groupPosition, childPosition);
        final HelpSubMenuHolder helpSubMenuHolder;

        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.goapp_help_child_layout, parent, false);
            helpSubMenuHolder = new HelpSubMenuHolder(convertView);
            convertView.setTag(helpSubMenuHolder);
        } else {
            helpSubMenuHolder = (HelpSubMenuHolder) convertView.getTag();
        }

        if (helpSubMenuModel.getIconSubMenu() == 0) {
            helpSubMenuHolder.mImageViewHelpSubMenuIcon.setVisibility(View.GONE);
        } else {
            helpSubMenuHolder.mImageViewHelpSubMenuIcon.setImageResource(helpSubMenuModel.getIconSubMenu());
        }

        helpSubMenuHolder.mTextViewHelpSubMenuTitle.setText(helpSubMenuModel.getTitleSubMenu());
        Log.d(TAG, "data " + mapData.toString());


        helpSubMenuHolder.mMutableWatcher = new MutableWatcher();

        helpSubMenuHolder.mMutableWatcher.setPosition(groupPosition, childPosition);

        if ( isChildSelectable(groupPosition, childPosition) ) {
            helpSubMenuHolder.mMutableWatcher.setActive(true);
        } else helpSubMenuHolder.mMutableWatcher.setActive(false);
        helpSubMenuHolder.mEditTextHelpSubMenuForm.addTextChangedListener(helpSubMenuHolder.mMutableWatcher);


        if(mapData.containsKey(groupPosition) && mapData.get(groupPosition) != null)  {
            Log.d(TAG, "get contain key for "+groupPosition + " - "+ childPosition +" _ "+getCombinedChildId(groupPosition, childPosition) );
            helpSubMenuHolder.mEditTextHelpSubMenuForm.setText(mapData.get(groupPosition).getHelpReason());
        }

        helpSubMenuHolder.mButtonHelpSubMenuCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse(helpSubMenuModel.getCsPhoneSubMenu()));
                mContext.startActivity(intent);*/
            }
        });

        helpSubMenuHolder.mButtonHelpSubMenuSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, helpSubMenuHolder.mEditTextHelpSubMenuForm.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });

        return convertView;
    }

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

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return childId;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return groupId;
    }

    public class MutableWatcher implements TextWatcher {

        private int groupPosition;
        private int childPosition;
        private boolean mActive;

        void setPosition(int groupPosition, int childPosition) {
            this.childPosition = childPosition;
            this.groupPosition = groupPosition;
        }

        void setActive(boolean active) {
            mActive = active;
        }

        @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) {

            if (mActive) {
                /*
                if(mMapSubMenu.containsKey(getGroup(groupPosition).getIdMenu())) {
                    mMapSubMenu.get((getChild(groupPosition, childPosition)).getIdSubMenu()).get(0).setTitleSubMenu(s.toString());
                    ;//.notes = s.toString();
                }
                */
                if(isChildSelectable(groupPosition, childPosition)) {
                    if (mapData.containsKey(groupPosition)) {
                        if (!s.toString().isEmpty())
                            mapData.get(groupPosition).setHelpReason(s.toString());
                    } else {
                        if (!s.toString().isEmpty()) {
                            HelpSubMenuModel data = new HelpSubMenuModel();
                            data.setHelpReason(s.toString());
                            mapData.put(groupPosition, data);
                        }
                    }
                }
            }

        }
    }

}

这是活动布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/expandlistview_help_goapp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ExpandableListView>


</LinearLayout>

这是我的小组布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/linearlayout_help_group"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageview_help_group_icon"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/textview_help_group_title"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="6"
        android:gravity="center_vertical"/>

    <ImageView
        android:id="@+id/imageview_help_group_arrow"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/help_expand_arrow_goapp"
        android:visibility="gone"/>

</LinearLayout>

这是我的孩子布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearlayout_help_child_goapp"
    android:padding="16dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageview_help_child_icon_goapp"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/textview_help_child_title_goapp"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="6"
            android:gravity="center_vertical"/>

        <ImageView
            android:id="@+id/imageview_help_child_arrow_goapp"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:src="@drawable/help_expand_arrow_goapp"
            android:visibility="gone"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearlayout_help_child_form_goapp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:gravity="center"
            android:text="tell us more"/>

        <EditText
            android:id="@+id/edittext_help_child_goapp"
            android:layout_width="match_parent"
            android:layout_height="100dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/button_help_call_goapp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="call us"/>

            <Button
                android:id="@+id/button_help_submit_goapp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="submit"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

0 个答案:

没有答案