开发者

ExpandableListView Order Changed When Click

When I click the groupview the order of the expandablelist changes

I've found some one with similiar problem 开发者_JAVA百科here

The order seems random, i don't understand what happened.

Here's my code, I know it's a mess, I've rewritten it for a lot of times.

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));
    }               
}

}

and my layout

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>


I had the same problem. When i expand one item, the next item to it jumps to bottom. Here is my solution. Change this flowing from false to true in BaseExpandableListAdapter.

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

BR, Akila

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜