Sorting a Recyclerview

大憨熊 提交于 2019-12-09 02:05:19

问题


Hi guys How would i be able to sort a recyclerview by the data that is within a certain textview which is inside each item inside the recyclerview. I also would love to know how to add a header to each item that is different in the data. ie.

Im sorting trying to sort by a date and I want all the dates of xyz to be grouped under one header and the dates of 123 to be grouped under anther geader. Is this possible thanks.

Ill post my adapter for now if you need other information just comment under this. Cheers.

Here is the code which is stated in the fragment the recyclerview is in.

 mAdapter = new RecyclerViewAdapterTransactions(getActivity(), account, amount, incomeExpense, type, date, category, uid, description);
             ((RecyclerViewAdapterTransactions) mAdapter).setMode(Attributes.Mode.Single);
             recyclerView.setHasFixedSize(true);
             recyclerView.setAdapter(mAdapter);

RecyclerViewAdapterTransactions adapter:

    public class RecyclerViewAdapterTransactions extends RecyclerSwipeAdapter<RecyclerViewAdapterTransactions.SimpleViewHolder>{

   public static class SimpleViewHolder extends RecyclerView.ViewHolder {

      SwipeLayout swipeLayout;
      ImageButton buttonDelete;
      ImageButton buttonEdit;

      TextView textViewAccount;
      TextView textViewAmount;
      TextView textViewIncomeExpense;
      TextView textViewDate;
      TextView textViewUID;
      TextView textViewCategory;
      TextView textViewDescription;
      TextView textViewType;

      public SimpleViewHolder(View itemView) {
         super(itemView);
         swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipeLayoutTransactions);
         buttonDelete = (ImageButton) itemView.findViewById(R.id.imageButtonTransactionDelete);
         buttonEdit = (ImageButton) itemView.findViewById(R.id.imageButtonTransactionEdit);

         textViewAmount = (TextView) itemView.findViewById(R.id.textViewAmount);
         textViewAccount = (TextView) itemView.findViewById(R.id.textViewAccount);
         textViewIncomeExpense = (TextView) itemView.findViewById(R.id.textViewIncomeExpense);
         textViewType = (TextView) itemView.findViewById(textViewTransactionType);
         textViewDate = (TextView) itemView.findViewById(R.id.textViewDateOfTransaction);
         textViewCategory = (TextView) itemView.findViewById(R.id.textViewCategory);
         textViewUID = (TextView) itemView.findViewById(R.id.id);
         textViewDescription = (TextView) itemView.findViewById(R.id.textViewDescription);


      }
   }

   private Context mContext;
   private ArrayList<String> account;
   private ArrayList<String> amount;
   private ArrayList<String> incomeExpense;
   private ArrayList<String> type;
   private ArrayList<String> date;
   private ArrayList<String> category;
   private ArrayList<String> uid;
   private ArrayList<String> description;


   public RecyclerViewAdapterTransactions(Context context, ArrayList<String> account, ArrayList<String> amount, ArrayList<String> incomeExpense, ArrayList<String> type, ArrayList<String> date, ArrayList<String> category, ArrayList<String> uid, ArrayList<String> description) {
      this.mContext = context;
      this.account = account;
      this.amount = amount;
      this.incomeExpense = incomeExpense;
      this.type = type;
      this.date = date;
      this.category = category;
      this.uid = uid;
      this.description = description;
   }



   @Override
   public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_transactions, parent, false);
      return new SimpleViewHolder(view);
   }


   @Override
   public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {

      String itemAccount = account.get(position);
      String itemAmount = amount.get(position);
      String itemIncomeExpense = incomeExpense.get(position);
      String itemType = type.get(position);
      String itemDate = date.get(position);
      String itemCategory = category.get(position);
      String itemDescription = description.get(position);
      final String itemID = uid.get(position);

      viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);

      viewHolder.buttonDelete.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            mItemManger.removeShownLayouts(viewHolder.swipeLayout);
            account.remove(position);
            amount.remove(position);
            incomeExpense.remove(position);
            type.remove(position);
            date.remove(position);
            category.remove(position);
            uid.remove(position);
            description.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, account.size());
            mItemManger.closeAllItems();

            TransactionsDatabase transactionsDatabase = new TransactionsDatabase(mContext);
            transactionsDatabase.open();
            transactionsDatabase.deleteEntry(itemID);
            transactionsDatabase.close();

         }
      });

      viewHolder.buttonEdit.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {

            Intent intent = new Intent();
            intent.setClass(mContext, EditTransaction.class);

            intent.putExtra("account", account.get(position));//
            intent.putExtra("amount", amount.get(position));//
            intent.putExtra("incomeExpense", incomeExpense.get(position));//
            intent.putExtra("type", type.get(position));//
            intent.putExtra("date", date.get(position));//
            intent.putExtra("category", category.get(position));//
            intent.putExtra("uid", uid.get(position));
            intent.putExtra("description", description.get(position));//
            mContext.startActivity(intent);


         }
      });


      Typeface robotoTypeFace = Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Thin.ttf");
      viewHolder.textViewAccount.setText(itemAccount);
      viewHolder.textViewAmount.setText(itemAmount);
      viewHolder.textViewIncomeExpense.setText(itemIncomeExpense);
      viewHolder.textViewType.setText(itemType);
      viewHolder.textViewDate.setText(itemDate);
      viewHolder.textViewCategory.setText(itemCategory);
      viewHolder.textViewUID.setText(itemID);
      viewHolder.textViewDescription.setText(itemDescription);

      viewHolder.textViewAccount.setTypeface(robotoTypeFace);
      viewHolder.textViewAmount.setTypeface(robotoTypeFace);
      viewHolder.textViewIncomeExpense.setTypeface(robotoTypeFace);
      viewHolder.textViewType.setTypeface(robotoTypeFace);
      viewHolder.textViewDate.setTypeface(robotoTypeFace);
      viewHolder.textViewCategory.setTypeface(robotoTypeFace);
      viewHolder.textViewUID.setTypeface(robotoTypeFace);
      viewHolder.textViewDescription.setTypeface(robotoTypeFace);

      mItemManger.bindView(viewHolder.itemView, position);
   }

   @Override
   public int getItemCount() {
      return uid.size();
   }

   @Override
   public int getSwipeLayoutResourceId(int position) {
      return R.id.swipeLayoutTransactions;
   }

}

回答1:


First of all Sort the data using Comparator before setting adapter. using Collections. import java.util.Collections like

Collections.sort(categories, new Comparator<AnyClass>() {
            @Override
            public int compare(AnyClasslhs, AnyClassrhs) {
                return lhs.label.compareTo(rhs.label);
            }
        });

In above I sort a lable in AnyClass alphabetically. And In order to add a header you have to convert the label into charArray and compare it with last item in array. if it is not that then add a header.

Some Helpful links

https://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/

https://github.com/amalChandran/ListviewAlphabetIndexer

Display Namelist In Recyclerview under each letter in alphabetic Order Android




回答2:


This library makes it easy to group your items and display headers without having to compare indexes.

First you create a Section class:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, List<String> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position));
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with your Sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MySection section1 = new MySection("Header 1", section1DataList);
MySection section2 = new MySection("Header 2", section2DataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);


来源:https://stackoverflow.com/questions/30347402/sorting-a-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!