gpt4 book ai didi

java - ExpandableListView 单击时顺序发生变化

转载 作者:行者123 更新时间:2023-12-01 05:42:22 31 4
gpt4 key购买 nike

当我单击组 View 时,可扩展列表的顺序会发生变化

我发现有人有类似的问题 here

顺序似乎是随机的,我不明白发生了什么。

这是我的代码,我知道它很乱,我已经重写了很多次。

public class ManageListAdapter extends BaseExpandableListAdapter{

public static final int SERVER_TYPE=0x000001;
public static final int HISTORY_TYPE=0x000002;

private List<ServerElement> mData;

private LayoutInflater mInflater;
private int viewType;


public ManageListAdapter (Context c,int type,ArrayList<ServerElement> data){
mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData=(List<ServerElement>) Collections.synchronizedList(new ArrayList<ServerElement>());
mData=data;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return mData.get(groupPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView==null){
convertView=mInflater.inflate(R.layout.managelistchildelement, null);

EditText IP=(EditText) convertView.findViewById(R.id.manageIP);
IP.setText(mData.get(groupPosition).mIp);

EditText Port=(EditText) convertView.findViewById(R.id.managePort);
Port.setText(mData.get(groupPosition).mPort);

EditText Username=(EditText) convertView.findViewById(R.id.manageUsername);
Username.setText(mData.get(groupPosition).mUsername);

EditText Password=(EditText) convertView.findViewById(R.id.managePWD);
Password.setText(mData.get(groupPosition).mPassword);

ImageButton btnUpdate=(ImageButton) convertView.findViewById(R.id.okBtn);

viewHolder=new ViewHolder(IP,groupPosition);
switch(this.viewType){
case HISTORY_TYPE:
IP.setFocusable(false);
Port.setFocusable(false);
Username.setFocusable(false);
Password.setFocusable(false);
btnUpdate.setVisibility(View.INVISIBLE);
break;
case SERVER_TYPE:
btnUpdate.setFocusable(false);
break;
}
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 1;
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mData.get(groupPosition);
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

public void deleteItem(int groupPosition){
if(mData.size()<=0) return;
int i=0;
while (groupPosition+i+1<mData.size()) {
mData.set(groupPosition+i, mData.get(groupPosition+i+1));
i++;
}
mData.remove(mData.size()-1);
notifyDataSetChanged();
}

public void addItem(ServerElement element){

mData.add(element);
notifyDataSetChanged();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView==null){
convertView=mInflater.inflate(R.layout.managelistgroupelement, null);
TextView view=(TextView) convertView.findViewById(R.id.elementCaption);
ImageView viewDelete=(ImageView) convertView.findViewById(R.id.deleteBtn);
//viewDelete.setFocusable(false);
view.setText(mData.get(groupPosition).mName);
view.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
view.setPadding(36, 0, 0, 0);
if(groupPosition==0){
Log.v("ManageListAdapter", "Still Can be 0");
}
viewHolder=new ViewHolder(view,groupPosition);
viewDelete.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
deleteItem(getViewHolder(v).position);
}

});
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
return convertView;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}

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


public static class ViewHolder{
private TextView txtView;
public final int position;

public ViewHolder(TextView view,int position){
this.txtView=view;
this.position=position;
}
}

public ViewHolder getViewHolder(View v)
{
if(v.getTag() == null)
{
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}

private class OnDeleteClickListener implements OnClickListener{
private int mGroupPosition;
OnDeleteClickListener(int groupPosition){
mGroupPosition = groupPosition;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("ManageListAdapter","Deleting Group "+mGroupPosition);
deleteItem(mGroupPosition);
}
}

private class OnUpdateClickListener implements OnClickListener{
private int mGroupPosition;
private int mChildrenPosition;
OnUpdateClickListener(int groupPosition,int childrenPosition){
mGroupPosition = groupPosition;
mChildrenPosition=childrenPosition;

}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addItem(mData.get(mGroupPosition));
}
}

}

和我的布局

managelistchildelement.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:text="@string/Ip:port"></TextView>
<EditText android:id="@+id/manageIP" android:layout_width="wrap_content" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:text="@string/:"></TextView>
<EditText android:id="@+id/managePort" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:text="@string/username"></TextView>
<EditText android:id="@+id/manageUsername" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
</TableRow>
<TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:text="@string/password"></TextView>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:id="@+id/managePWD"></EditText>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ok" android:id="@+id/okBtn" android:focusable="false" android:focusableInTouchMode="false"></ImageButton>
</TableRow>
</TableLayout>

managelistgroupelement.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:orientation="horizontal"
android:layout_width="wrap_content" android:baselineAligned="false">
<TextView android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:text="TextView"
android:id="@+id/elementCaption" android:layout_weight="1"
android:layout_width="0dp"></TextView>
<ImageView android:src="@drawable/delete" android:id="@+id/deleteBtn"
android:layout_gravity="right" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:focusable="false" android:focusableInTouchMode="false"></ImageView>

</LinearLayout>

最佳答案

我也遇到了同样的问题。当我展开一个项目时,下一个项目会跳到底部。这是我的解决方案。在 BaseExpandableListAdapter 中将此流从 false 更改为 true。

@Override
public boolean hasStableIds() {
return true; //Previous value was false
}

BR,阿基拉

关于java - ExpandableListView 单击时顺序发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6781887/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com