1

我想通过在 recycleview 中使用 cardview 在 android 上添加子列表来查看。

如果对象有子项,我已经创建了类来存储这些项并在对象的每个父项上创建数组列表,因此我可以区分列表是否有子项。

但是我很难渲染相同的布局以使子列表低于父列表。我务实地逐一制作布局,但很难做到。因此,每当对象/类在该对象中具有子列表的数组列表时,我想为我的适配器呈现相同的布局。

这是我现在的适配器

public class NotificationListAdapter extends BaseListAdapter<NotificationData, NotificationListAdapter.CustomViewHolder> {

    private List<NotificationData> notificationData;
    private ArrayList<Integer> indexForHide;

    public NotificationListAdapter(Context context) {
        super(context);
    }

    @Override
    public void addAll(List<NotificationData> items) {
        super.addAll(items);
        this.notificationData = items;
    }

    void setChildNotif(){
        ArrayList<NotificationData> childDataItems;
        List<NotificationData> notificationDataChild = new ArrayList<NotificationData>();
        notificationDataChild.addAll(notificationData);
        indexForHide = new ArrayList<Integer>();
        for (int i = 0; i < notificationData.size(); i++) {

            childDataItems = new ArrayList<>();
            if (notificationDataChild.size() == 1) {
                notificationData.get(i).setChildNotification(childDataItems);
                break;
            }
              for (int j = i; j < notificationDataChild.size(); j++){
                if(i==j){
                    continue;
                }else if (notificationDataChild.size() == 1) {
                    notificationData.get(i).setChildNotification(childDataItems);
                    break;
                }else{

                    if(notificationDataChild.get(j).getRfqId()!=0){
                        if(notificationDataChild.get(j).getRfqId()==notificationData.get(i).getRfqId()){
                            childDataItems.add(notificationDataChild.get(j));
                            indexForHide.add(j);

                        }
                    }
                    if(!notificationDataChild.get(j).getInvoiceNumber().equalsIgnoreCase("")) {
                        if (notificationDataChild.get(j).getInvoiceNumber().equalsIgnoreCase(notificationData.get(i).getInvoiceNumber())) {
                            childDataItems.add(notificationDataChild.get(j));
                            indexForHide.add(j);
                        }
                    }

                }
            }

            notificationData.get(i).setChildNotification(childDataItems);
            Log.d("sizeNotif", i+" :"+notificationData.get(i).getChildNotification().size());
        }

        Log.d("sizeNotification", " :"+notificationData.size());
        Log.d("sizeNotificationDummy", " :"+notificationData.size());
        Log.d("isiHideAdapter", Arrays.toString(indexForHide.toArray()));
        Log.d("isiHideAdapterSize", indexForHide.size()+"");


    }

    @Override
    protected int getItemResourceLayout(int viewType) {
        return R.layout.item_notification_list;
    }

    int position;

    @Override
    public int getItemViewType(int position) {
        this.position = position;
        return position;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        setChildNotif();
        return new CustomViewHolder(getView(parent, viewType), onItemClickListener);
    }

    public class CustomViewHolder extends BaseViewHolder<NotificationData> {

        private TextView tvNotifTitle, tvNotifBody, tvNotifStatus, tvNotifTime, tvSignUnread, tvHeaderTitle;
        private CardView formContent;
        private LinearLayout linearLayout_childItems;
        private ImageView expandList;

        public CustomViewHolder(View itemView, OnItemClickListener onItemClickListener) {
            super(itemView, onItemClickListener);
            tvNotifTitle = itemView.findViewById(R.id.tvNotifTitle);
            tvNotifBody = itemView.findViewById(R.id.tvNotifBody);
//            tvNotifStatus = itemView.findViewById(R.id.tvNotifStatus);
            tvNotifTime = itemView.findViewById(R.id.tvNotificationTime);
            formContent = itemView.findViewById(R.id.formContent);
            tvSignUnread = itemView.findViewById(R.id.tvSignUnread);
            tvHeaderTitle = itemView.findViewById(R.id.tvHeaderNotifList);
            linearLayout_childItems = itemView.findViewById(R.id.ll_child_items);
            expandList = itemView.findViewById(R.id.arrow_expand_notif_list);


            //SET CHILD
            int intMaxNoOfChild = 0;
            for (int index = 0; index < notificationData.size(); index++) {
                int intMaxSizeTemp = notificationData.get(index).getChildNotification().size();
                if (intMaxSizeTemp > intMaxNoOfChild) intMaxNoOfChild = intMaxSizeTemp;

            }
            linearLayout_childItems.removeAllViews();
            for (int indexView = 0; indexView < intMaxNoOfChild; indexView++) {
                TextView textView = new TextView(context);
                textView.setId(indexView);
                textView.setPadding(20, 20, 0, 20);
                textView.setGravity(Gravity.LEFT);
                textView.setTextSize(14);
                textView.setTextColor(ContextCompat.getColor(context, R.color.colorText));
                textView.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_sub_notif_text));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.leftMargin = 16;
                textView.setOnClickListener(this);
                linearLayout_childItems.addView(textView, layoutParams);
            }
        }

        @Override
        public void bind(NotificationData item) {
            String title = splitString(item.getTitle());
            tvNotifTitle.setText(title);
            tvNotifBody.setText(item.getContent());

            String txtTime = item.getCreatedAtDisplay().replaceAll("WIB", "");
            tvNotifTime.setText(txtTime);

            if (position == 0) {
                ((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).topMargin = dpToPx(12);
            }

            ((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).bottomMargin = dpToPx(12);

            if (position == getItemCount() - 1) {
                ((ViewGroup.MarginLayoutParams) formContent.getLayoutParams()).bottomMargin = dpToPx(80);
            }

            if(item.getIsRead()==1){
                formContent.setCardBackgroundColor(context.getResources().getColor(R.color.gray1));
                formContent.setCardElevation(5);
                formContent.setRadius(15);
                tvSignUnread.setVisibility(View.GONE);
            }else if(item.getIsRead()==0){
            }

            //setHeader
            Calendar calendar;
            SimpleDateFormat dateFormat;
            calendar = Calendar.getInstance();
            dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
            String date, yesterdayDate, dateDisplayData, dateData;
            date = dateFormat.format(calendar.getTime());
            calendar.add(Calendar.DATE, -1);
            yesterdayDate = dateFormat.format(calendar.getTime());
            String[] arrDateDisplayData = item.getCreatedAtDisplay().split(",", 5);
            dateDisplayData = arrDateDisplayData[0];
            String[] arrDateData = item.getCreatedAt().split(" ", 5);
            dateData = arrDateData[0];
            Log.d("datenow", date);
            Log.d("dateyesterday", yesterdayDate);
            Log.d("dateDisplayData", dateDisplayData);
            Log.d("dateData", dateData);
            if(dateData.equalsIgnoreCase(date)){
                tvHeaderTitle.setText("Hari ini");
            }else if(dateData.equalsIgnoreCase(yesterdayDate)){
                tvHeaderTitle.setText("Kemarin");
            }else{
                tvHeaderTitle.setText(dateDisplayData);
//                tvHeaderTitle.setVisibility(View.GONE);
            }

            if (getAdapterPosition() > 0) {
                String[] timePrev = notificationData.get(getAdapterPosition() - 1).getCreatedAt().split(" ");
                if (dateData.equalsIgnoreCase(timePrev[0])) {
                    tvHeaderTitle.setVisibility(View.GONE);
                }
            }

            if(indexForHide.contains(getAdapterPosition())){
                formContent.setVisibility(View.GONE);
            }

            //SET CHILD
            NotificationData dummyParentDataItem = notificationData.get(position);

            int noOfChildTextViews = linearLayout_childItems.getChildCount();
            for (int index = 0; index < noOfChildTextViews; index++) {
                TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(index);
                currentTextView.setVisibility(View.VISIBLE);
            }

            int noOfChild = 0;
            if(dummyParentDataItem.getChildNotification()==null){
                noOfChild = 0;
            }else{
                noOfChild = dummyParentDataItem.getChildNotification().size();
            }



            if (noOfChild < noOfChildTextViews) {
                for (int index = noOfChild; index < noOfChildTextViews; index++) {
                    TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(index);
//                    currentTextView.setVisibility(View.GONE);
                }
            }
            for (int textViewIndex = 0; textViewIndex < noOfChild; textViewIndex++) {
                TextView currentTextView = (TextView) linearLayout_childItems.getChildAt(textViewIndex);
                currentTextView.setText(dummyParentDataItem.getChildNotification().get(textViewIndex).getTitle());
                /*currentTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(mContext, "" + ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });*/
            }

            if (noOfChild > 0) {
                expandList.setVisibility(View.VISIBLE);
                expandList.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (linearLayout_childItems.getVisibility() == View.VISIBLE) {
                            linearLayout_childItems.setVisibility(View.GONE);
//                            Toast.makeText(context, "expand", Toast.LENGTH_SHORT).show();
                            expandList.setImageResource(R.drawable.ic_arrow_up_blue);
                        }
                        else {
                            linearLayout_childItems.setVisibility(View.VISIBLE);
                            expandList.setImageResource(R.drawable.ic_arrow_down_blue);
                        }
                    }
                });
            }else {
                linearLayout_childItems.setVisibility(View.GONE);
            }

        }

        public int dpToPx(int dp) {
            Resources r = context.getResources();
            int px = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    dp, r.getDisplayMetrics()
            );
            return px;
        }

        public String splitString(String tempString){
            StringBuilder finalString = new StringBuilder(tempString);
            int i = 0;
            while ((i = finalString.indexOf(" ", i + 20)) != -1) {
                finalString.replace(i, i + 1, "\n");
            }

            return finalString.toString();
        }

    }


}

每当我想在我的父列表下创建一个子列表时,请帮助我渲染我的布局,这样我就可以简化上面的代码,而不是以编程方式制作子布局

4

0 回答 0