android - 有什么方法可以使用 cursoradapter 在 recyclerview 中实现字母部分索引?

标签 android android-recyclerview simplecursoradapter

我需要在我的 recyclerview 中实现按字母顺序排列的 sectionindexer。我找到了很多库,但没有一个使用 Cursoradapter 如何使用 cursoradapter 为 recyclerview 实现 sectionindexer 在这里让我发布我的适配器类有人可以告诉我如何开始这是我的适配器类:

  import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Color;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.amulyakhare.textdrawable.TextDrawable;
    import com.amulyakhare.textdrawable.util.ColorGenerator;

    import java.util.ArrayList;
    import java.util.List;

    import model.CustomerModel;
    import timertracker.precision.timetracker.R;

    public class CustomerListAdapter extends CursorRecycleViewAdapter<CustomerListAdapter.MyViewHolder>  implements View.OnClickListener {
        private List<CustomerModel> dataSet;
        String companygrp;
        int position;
        ColorGenerator generator = ColorGenerator.MATERIAL;
        private OnItemClickListener onItemClickListener;

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


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



        public static class MyViewHolder extends RecyclerView.ViewHolder {

            // Model_Task_List Model_Task_List=new Model_Task_List();
            TextView textname;
            TextView textaddress;
            TextView textphnum;
            TextView textdegree;
            TextView textemail;
            ImageView call;

            public MyViewHolder(final View itemView) {
                super(itemView);
                this.textname = (TextView) itemView.findViewById(R.id.subject);
                this.textaddress = (TextView) itemView.findViewById(R.id.customerstatus);
                this.textphnum = (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;
        }

        public void addModelClass(CustomerModel modelClass) {
            dataSet = new ArrayList<CustomerModel>();
            dataSet.add(modelClass);
            notifyDataSetChanged();
        }


        @Override
        public void onBindViewHolder(MyViewHolder holder, Cursor cursor) {
            TextView textViewName = holder.textname;
            TextView textViewaddress = holder.textaddress;
            TextView textid = holder.textphnum;
            TextView textemail=holder.textdegree;
            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")){
                textViewaddress.setTextColor(Color.parseColor("#006400"));
            }
            else {
                textViewaddress.setTextColor(Color.parseColor("#8B0000"));
            }
            textViewaddress.setText(status);
            textid.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);
        }

    }

如何为此实现节标题提前致谢

最佳答案

是的,你可以做到

这是一个使用 AlphabetIndexer 的适配器

public class MyCursorAdapter extends CursorAdapter  implements SectionIndexer{

AlphabetIndexer mAlphabetIndexer;

public MyCursorAdapter(Context context, int simpleListItem1,
        Cursor cursor, String[] strings, int[] is)
{
    super(context, cursor);

    mAlphabetIndexer = new AlphabetIndexer(cursor,
            cursor.getColumnIndex("person"),
            " ABCDEFGHIJKLMNOPQRTSUVWXYZ");
    mAlphabetIndexer.setCursor(cursor);//Sets a new cursor as the data set and resets the cache of indices.

}

/**
 * Performs a binary search or cache lookup to find the first row that matches a given section's starting letter.
 */
@Override
public int getPositionForSection(int sectionIndex)
{
    return mAlphabetIndexer.getPositionForSection(sectionIndex);
}

/**
 * Returns the section index for a given position in the list by querying the item and comparing it with all items
 * in the section array.
 */
@Override
public int getSectionForPosition(int position)
{
    return mAlphabetIndexer.getSectionForPosition(position);
}

/**
 * Returns the section array constructed from the alphabet provided in the constructor.
 */
@Override
public Object[] getSections()
{
    return mAlphabetIndexer.getSections();
}

/**
 * Bind an existing view to the data pointed to by cursor
 */
@Override
public void bindView(View view, Context context, Cursor cursor)
{
    TextView txtView = (TextView)view.findViewById(android.R.id.text1);
    txtView.setText(cursor.getString(
            cursor.getColumnIndex("person")));
}

/**
 * Makes a new view to hold the data pointed to by cursor.
 */
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
    LayoutInflater inflater = LayoutInflater.from(context);
    View newView = inflater.inflate(
            android.R.layout.simple_list_item_1, parent, false);
    return newView;
}
}

这是一个完整的例子http://www.rogcg.com/blog/2013/02/10/Implementing-a-ListView-with-AlphabetIndexer-and-CursorAdapter-on-Android

也可以引用

  1. http://hello-android.blogspot.com/2010/11/sideindex-for-android.html
  2. https://github.com/emilsjolander/StickyListHeaders
  3. https://github.com/JimiSmith/PinnedHeaderListView

关于android - 有什么方法可以使用 cursoradapter 在 recyclerview 中实现字母部分索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39222575/

相关文章:

android - Android 中的 Intent 附加功能

android - RecyclerView 中的 EditText 值在第 5 个位置后给出相同的值

android - 在 recyclerView android 中显示来自 hereNow Pubnub 的结果数据

java - 从 Android 中的 SQL 查询中的 CursorAdapter 支持的 Spinner 获取字符串值

android - 如何将 SimpleCursorAdapter 与 single_choice 和 multiple_choice 一起使用

android - 在 Android 中将文本重力更改为顶部

java - 无法启动服务? (语音识别)

java - 当我单击弹出菜单中的项目时,如何展开 AlertDialog 的布局?

android - 当适配器注册了观察者时,无法更改此适配器是否具有稳定 ID

android - 如何使用 Android SimpleCursorAdapter 合并两个或多个列