如何在cursoradapter recyclerview中添加多个viewType?

时间:2016-08-31 05:30:41

标签: android android-recyclerview adapter

我在使用cursorrecyclerviewadapter发布此问题之前已经研究了很多,我怎么能使用多种视图类型我现在无法找到任何样本让我解释我的问题我需要将标题添加到我的recyclerview项目,如字母标题如何我可以通过游标recyclerview适配器实现这一点,让我发布我的代码:

public class CustomerListAdapter extends CursorRecycleViewAdapter<CustomerListAdapter.MyViewHolder>  implements View.OnClickListener , RecyclerViewFastScroller.BubbleTextGetter{
    int position;
    Cursor cursor;
    ColorGenerator generator = ColorGenerator.MATERIAL;
    private OnItemClickListener onItemClickListener;
    List<CustomerModel>list;



    public CustomerListAdapter(Context mContext, Cursor cursor) {
        super(mContext, cursor);

    }


    public void setOnItemClickListener(final OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public String getTextToShowInBubble(int pos) {
        return Character.toString(cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_Name)).charAt(0));
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textname;
        TextView status;
        TextView textphnum;
        TextView textdegree;
        TextView textemail;
        ImageView call;

        public MyViewHolder(final View itemView) {
            super(itemView);
            this.textname = (TextView) itemView.findViewById(R.id.subject);
            this.status = (TextView) itemView.findViewById(R.id.customerstatus);
            this.textemail = (TextView) itemView.findViewById(R.id.username);
            this.textdegree=(TextView)itemView.findViewById(R.id.status);
            this.call = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.customeradapterview, parent, false);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return myViewHolder;
    }



    @Override
    public void onBindViewHolder(MyViewHolder holder, Cursor cursor) {
        TextView textViewName = holder.textname;
        TextView textstatus = holder.status;
        TextView textphnumbr = holder.textphnum;
        TextView textemail=holder.textemail;
        String  companygrp = cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_Name));
        String email=cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_EmailID));
        if(email.length()>=20){
            String s=  email.substring(0,20)+"...";
            textemail.setText(s);
        }
        else {
            textemail.setText(email);
        }
        //textemail.setText(cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_EmailID)));
        String status=cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_Status));
        if(status.equals("Active")){
            textstatus.setTextColor(Color.parseColor("#006400"));
        }
        else {
            textstatus.setTextColor(Color.parseColor("#8B0000"));
        }
        textstatus.setText(status);
        textphnumbr.setText(cursor.getString(cursor.getColumnIndex(CustomerModel.Customer_MobileNumber)));
        String s1 = companygrp.substring(0, 1).toUpperCase() + companygrp.substring(1).toLowerCase();
        textViewName.setText(s1);
        ColorGenerator generator = ColorGenerator.DEFAULT;
        String com= s1.substring(0,1);
        TextDrawable drawable = TextDrawable.builder()
                .buildRound((com).toUpperCase(), generator.getRandomColor());
        holder.call.setImageDrawable(drawable);

     /*  for (int i = 0; i < companygrp.length(); i++) {
            String s1 = companygrp.substring(0, 1).toUpperCase() + companygrp.substring(1).toLowerCase();
            textViewName.setText(s1);
            ColorGenerator generator = ColorGenerator.DEFAULT;
            String com= s1.substring(0,1);
            TextDrawable drawable = TextDrawable.builder()
                    .buildRound((com).toUpperCase(), generator.getRandomColor());
            holder.call.setImageDrawable(drawable);
        }*/
    }

    @Override
    public void onClick(View v) {
        final RecyclerView recyclerView = (RecyclerView) v.getParent();
        position = recyclerView.getChildLayoutPosition(v);
        if (position != RecyclerView.NO_POSITION) {
            final Cursor cursor = this.getItem(position);
            this.onItemClickListener.onItemClicked(cursor);
        }

    }

    public interface OnItemClickListener {
        void onItemClicked(Cursor cursor);
    }

}

如何向我的recyclerview添加标题我可以告诉我这个解决方案提前感谢!!

0 个答案:

没有答案