ExpandableListView groupView按钮在展开时切换行

时间:2017-11-22 14:49:24

标签: android listview expandablelistview

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个单独的按钮(搜索,传递和检索),以及仅包含文本的子视图。现在,按钮应该从绿色变为红色,反之亦然随机点击,这是有效的。 的 预展开:

enter image description here

展开后:

enter image description here 但是当我展开列表视图时,问题出现了,按钮上的颜色随着另一行上的按钮切换。它似乎每次都用同一行切换,但我不确定为什么......任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:0)

最后我决定尝试在项目中使用idunnololz的AnimatedExpandableListView,它确实有效!我仍然不知道原来的ExpandableListView为什么会被淘汰,尽管我认为这可能与使用按钮搞乱的listview的回收有关。