我在 expandablelistview
项目中有一个 edittext
. 然后,我在 edittext
中输入了一个文本,但每当我点击其他 expandablelistview
Headers 时,我输入的文字就会消失 .
我已经在我的Manifest.xml中使用了 android:windowSoftInputMode="adjustPan"
以及许多其他可能的修复,但是没有用 .
这是我的适配器:
public class ExpendableAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<ExpandItemModel>> _listDataChild;
public ExpendableAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<ExpandItemModel>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@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(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
final EditText etTotal = (EditText)convertView
.findViewById(R.id.et_total);
etTotal.setText(childText.getTotal);
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.list_group, null);
}
TextView tvHeader = (TextView) convertView
.findViewById(R.id.tv_header);
tvHeader.setTypeface(null, Typeface.BOLD);
tvHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是布局:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rl_expen_but"
android:layout_below="@+id/rl_ats_draft2"
android:layout_above="@+id/selanjutnya"
>
<ExpandableListView
android:background="@color/cardview_light_background"
android:id="@+id/lvExp"
android:descendantFocusability="beforeDescendants"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:animateLayoutChanges="true"/>
<View
android:layout_below="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#bebebe"/>
</RelativeLayout>
我用上面的代码做错了吗?谢谢 .
EDIT:
按照chandil03的建议编辑后,我的代码就是这样的 . 展开 Headers 时,expandablelistview不再刷新 . 但奇怪的是,当我在集合0和子位置0的edittext中键入值时,在不同的goupposition和childposition中的其他一些edittext中出现与我在前一个edittext中键入的相同的确切值 . 甚至更奇怪的是,以前的edittext上的值有时会消失 .
static class ViewHolder {
protected View et;
public void addView(View et){
this.et = et;
}
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandItemModel childText = (ExpandItemModel) getChild(groupPosition, childPosition);
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.addView(convertView
.findViewById(R.id.et_total));
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
final EditText etTotal = (EditText)convertView
.findViewById(R.id.et_total);
etTotal.setText(childText.getTotal);
return convertView;
}
4 回答
每当你展开另一个组列表刷新,你得到另一个没有文本的EditText实例 . 您输入文本的EditText将成为另一个
groupView
的子项 .因此,我建议您创建一个ViewHolder类并保留您的视图并将其设置为标记,并在
getView()
中检查convertView
是否为空 . 只需从convertView
添加else part和getTag并相应地设置值 .例如:
EDIT 1
我修改了你的代码 . 查看评论 .
EDIT 2
在
getChildView()
方法中添加以下代码 .每次向下滚动或单击ExpandableListView中的另一个 Headers 时,EditText都会被回收 .
您最好的选择是保存用户在模型中输入的文本,例如:
首先创建一个虚拟模型类来存储来自edittext的数据,如下所示:
然后创建一个静态arraylist来存储您的数据:
然后在你的适配器类中更改你的getChildView()方法,如下所示:
这段代码将要做的是,当您在edittext中键入字符时,它们将存储在arraylist中的DummyModel对象中 .
您可以通过以下方式检索此数据
我终于能够通过使用这个库来处理这个问题:
http://bignerdranch.github.io/expandable-recycler-view/
然后通过使用AddTextChangedListener,我插入了值I type . 之后,检查ViewHolderChild中的插入值 . 例如: