簡體   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