Dynamic Section Header Of RecyclerView Using Current Date&Time

后端 未结 4 1918
轮回少年
轮回少年 2021-02-08 07:46

I\'ll use RecyclerView for a section header

I want to create section header when i insert each Date & Time data in SQLite Database

I followed

4条回答
  •  悲哀的现实
    2021-02-08 08:33

    I had Recently done this for sorting something on Monthly Basis. Its By overriding ViewType Function .

    For doing this you have to use something like getViewType() in Recycler Adapter.

    public class LastTransAdapter extends RecyclerView.Adapter {
        private ArrayList lastTransBeans;
        private Context context;
    
        private MyCustomTextView textViewTitle, tv_Desc, tv_Date, tv_Amount;
        private LinearLayout layout1;
        private View customView, myView;
    
        private LayoutParams layoutParams;
    
        private PopupWindow popUpWindow = null;
    
        private RelativeLayout layout;
    
        private int MONTHHEADER = 1;
        private int DATAVIEW = 2;
    
    
        public LastTransAdapter(ArrayList lastTransBeans, Context context, View myView, RelativeLayout layout) {
            this.lastTransBeans = lastTransBeans;
            this.context = context;
            this.myView = myView;
            this.layout = layout;
    
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    
            if (i == DATAVIEW) {
    
                 // view for normal data.
    
    
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_last_transactions, viewGroup, false);
                return new ViewHolder(view);
            } else {
    
                // view type for month or date header
    
                View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row_month_header, viewGroup, false);
                return new ViewHolder(view);
            }
        }
    
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int i) {
    
            final int position = i;
    
            if (viewHolder.getItemViewType() == DATAVIEW) {
    
                //fill data for normal view
    
    
            } else {
                //set your date or month header
            }
    
        }
    
        @Override
        public int getItemCount() {
            return lastTransBeans.size();
        }
    
        @Override
        public int getItemViewType(int position) {
            if (lastTransBeans.get(position).getMonth()) {
                return MONTHHEADER;
            } else {
                return DATAVIEW;
            }
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            MyCustomTextView transtile, transdate, transamont, tv_monthHeader;
            LinearLayout acctype;
            CardView mlastTransCardView;
    
    
            public ViewHolder(View itemView) {
                super(itemView);
    
               // cast all the textviews , buttonsetc used ion view Holder.
    
            }
        }
    }
    

    You can also use multiple viewholder types. Like this example.

    Hope this may help you to solve your problem

提交回复
热议问题