我需要在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);
}
}
如何提前实现此标题的部分标题
答案 0 :(得分:2)
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;
}
}
您也可以参考
答案 1 :(得分:-1)
检查出https://developer.android.com/reference/android/widget/AlphabetIndexer.html
这似乎可以解决您的问题。为了按字母顺序排序数据,您可以在查询本身中指定排序顺序。