我在组项目的 ExpandableListView
中添加了 EditText
,之后当我单击组项目时,不会出现包含子项目的列表。可能是什么问题?在这种情况下,这个列表是否可以扩展?或者禁止在ExpandedListView
中的组项中添加EditText
?
适配器代码:
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>
最佳答案
我找到了解决这个问题的三个解决方案:
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) 创建一个 TextView
或 Button
,而不是 EditText
。按下后会出现带有 EditText 的警报对话框,用户可以在其中插入输入。
关于java - ExpandableListView 中的编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38890059/