繁体   English   中英

ExpandableListView groupView按钮在展开时切换行

[英]ExpandableListView groupView buttons switching rows when expanded

public class CustomExpandableListAdapter extends BaseExpandableListAdapter 
implements View.OnClickListener {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
private Button cancel;
private Drawable resId;
private static ClaimActivity cA = new ClaimActivity();
public static Button selectedButton;
public static ProgressBar loadingCircle;


public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<String>> expandableListDetail) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);

    cancel = (Button) convertView.findViewById(R.id.CancelButton);

    cancel.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View v) {
            try {
                ClaimActivity.setUpConfirmationAlertDialog(ClaimActivity.context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(final int listPosition, boolean isExpanded,  // getGroupView sets the title for the expandableListView ( Tag Id, Store Room, Room Number)
                         View convertView, ViewGroup parent) {

    Log.w("Convert View", String.valueOf(convertView));

    String listTitle = (String) getGroup(listPosition);
    GroupHolder mHolder;
    ExpandableListView exListView = (ExpandableListView) parent;

    //Create new View is one does not exist
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);

        mHolder = new GroupHolder();
        mHolder.searchButton = (Button) convertView.findViewById(R.id.SearchButton);
        mHolder.retrieveButton = (Button) convertView.findViewById(R.id.RetreivedButton);
        mHolder.deliverButton = (Button) convertView.findViewById(R.id.deliveredButton);
        mHolder.loadingCircle = (ProgressBar) convertView.findViewById(R.id.loadingCircle);
        mHolder.listTitleTextView = (TextView) convertView.findViewById(R.id.listTitle);

        convertView.setTag(mHolder);
    }

    mHolder = (GroupHolder) convertView.getTag();

    // Setting all variables in
    mHolder.listTitleTextView.setText(listTitle);

    mHolder.searchButton.setFocusable(false);
    mHolder.retrieveButton.setFocusable(false);
    mHolder.deliverButton.setFocusable(false);

    mHolder.searchButton.setOnClickListener(this);
    mHolder.deliverButton.setOnClickListener(this);
    mHolder.retrieveButton.setOnClickListener(this);

    return convertView;
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {

    //Getting Button position
    View parentRow = (View) v.getParent();
    ExpandableListView listView = (ExpandableListView) parentRow.getParent();
    final int position = listView.getPositionForView(parentRow);

    int expandedRowsCount = 0;
    for (int i = 0; i < position; i++) {
        if (listView.isGroupExpanded(i)) {
            expandedRowsCount++;
        }
    }

    int newPosition = position - expandedRowsCount;

    loadingCircle = (ProgressBar) parentRow.findViewById(R.id.loadingCircle);

    int randomNum = ThreadLocalRandom.current().nextInt(0, 1 + 1);

    switch (v.getId()) {
        case R.id.SearchButton:
            selectedButton = (Button) parentRow.findViewById(R.id.SearchButton);

            try {
                if (randomNum == 0) {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
                    buttonCheck(1);
                } else {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            selectedButton.setBackgroundDrawable(resId);
             /*else {
                try {
                    ClaimActivity.showTagErrorDialog(ClaimActivity.context);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
            break;


        case R.id.deliveredButton:
            selectedButton = (Button) parentRow.findViewById(R.id.deliveredButton);
            try {
                if (randomNum == 0) {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
                    buttonCheck(1);
                } else {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            selectedButton.setBackgroundDrawable(resId);
            break;


        case R.id.RetreivedButton:
            try {
                selectedButton = (Button) parentRow.findViewById(R.id.RetreivedButton);
                if (randomNum == 0) {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_green);
                    buttonCheck(2);
                } else {
                    resId = ContextCompat.getDrawable(ClaimActivity.getApplicationUsingReflection(), R.drawable.button_outline_grey);
                }
                selectedButton.setBackgroundDrawable(resId);
            } catch(Exception e){
                e.printStackTrace();
            }
            break;


        default:
            break;
    }

}

static class GroupHolder {
    public Button searchButton, retrieveButton, deliverButton;
    public ProgressBar loadingCircle;
    public TextView listTitleTextView;
}

public static void showTagErrorDialogw(Context a) {
    try {

        final AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(a, android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(a);
        }
        LayoutInflater inflater = LayoutInflater.from(a);
        View dialogView = inflater.inflate(R.layout.custom_dialog_error, null);
        builder.setView(dialogView);

        TextView tv = (TextView) dialogView.findViewById(R.id.Confirmation_Texts);
        tv.setText("Please stop the current search\nBefore starting a new one.");

        final AlertDialog alertDialog = builder.create();
        Button cancelButton = (Button) dialogView.findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.dimAmount = 0.1f;

        alertDialog.show();

    } catch (Exception ex) {

    }
}

}

上面是我的自定义ExpandableListAdapter的代码。 它由具有标题和3个独立按钮(搜索,传递和检索)的组视图以及仅由文本组成的子视图组成。 目前,单击时按钮应从绿色随机变为红色,反之亦然(有效)。 展开前:

在此处输入图片说明

展开后:

在此处输入图片说明 但是问题是当我展开列表视图时,按钮上的颜色与另一行上的按钮一起切换的。 似乎每次都切换同一行,但是我不确定为什么...任何帮助将不胜感激!

好吧,最后我决定在项目中尝试使用idunnololz的AnimatedExpandableListView,它成功了! 我仍然不知道为什么原来的ExpandableListView会出错,尽管我认为这可能与回收listview并弄乱按钮有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM