Я создал расширяемое представление, содержащее поле editText под каждой группой.
public class ExpandableActivity extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable);
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
expListView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
// preparing list data
//custom function that populates listDataHeader and listDataChild
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
Код адаптера:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
@Override
public View getChildView(final 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.e_list_item, null);
}
TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);
vw_text_sub.setText(childText);
vw_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
//store value?
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
return convertView;
}
@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.e_list_group, null);
}
TextView vw_header = (TextView) convertView.findViewById(R.id.eheader);
vw_header.setTypeface(null, Typeface.BOLD);
vw_header.setText(headerTitle);
return convertView;
}
// other overridden methods here....
}
Из-за функциональности адаптера, которая предназначена для повторного использования представлений, всякий раз, когда я набираю текст в поле редактирования, это значение либо теряется, либо копируется и в других полях редактирования, когда я прокручиваю вверх и вниз.
В представлении списка массив используется для хранения значений полей редактирования.
Как лучше всего это сделать в виде расширяемого списка? Массив массивов? Можете ли вы привести пример расширяемого представления списка, а не представления списка?
РЕДАКТИРОВАТЬ: Пробовал это, но у меня также есть проблемы:
public View getChildView(final 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.e_list_item, null);
}
TextView vw_text_sub = (TextView) convertView.findViewById(R.id.esubt);
EditText vw_edit = (EditText) convertView.findViewById(R.id.eedit);
vw_text_sub.setText(childText);
if(tmp[groupPosition][childPosition] != null) {
vw_edit.setText(tmp[groupPosition][childPosition]);
}else{
vw_edit.setText("");
}
vw_edit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
tmp[groupPosition][childPosition] = arg0.toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
return convertView;
}
tmp — это массив String[10][10]. Моих групп 3, а детей в группе максимум 7, поэтому массив не меньше моих данных.
addTextChangedListener
для сохранения значения перед прокруткой, так как вы не сохраняете значение, ваши данные не сохраняются, и вы его потеряли. - person Shayan Pourvatan   schedule 28.07.2016