在RecyclerView Android

时间:2016-09-11 04:28:16

标签: android sorting android-recyclerview recycler-adapter

RecyclerView填充了ArrayList个自定义对象。我已经实现了Comparable接口来根据索引对数组列表中的项进行排序。排序为我提供了所需的结果,但是当我填充RecyclerView时,我的项目按照排序前的顺序显示。

我在Collection.sort(arrayList)的构造函数中调用Adaper,如下所示:

public MyRecyclerViewListAdapter(List<Something> somethings, OnStartDragListener mDragStartListener) {
    this.mDragStartListener = mDragStartListener;
    this.somethings = somethings;
    Collections.sort(this.somethings);
}

即使我在将列表作为参数发送到构造函数之前对列表进行了排序:

    Collections.sort(this.somethings);
    myRecyclerViewListAdapter = new myRecyclerViewListAdapter(somethings, this);

我的适配器代码:

public class DefaultCurrenciesAdapter extends RecyclerView.Adapter<DefaultCurrenciesAdapter.MyViewHolder> implements ItemTouchHelperAdapter{

private List<Currency> currencies;
DataManager dataManager = new DataManager();
private OnStartDragListener mDragStartListener;

public DefaultCurrenciesAdapter(List<Currency> currencies, OnStartDragListener mDragStartListener) {
    this.mDragStartListener = mDragStartListener;
    this.currencies = currencies;
    Collections.sort(this.currencies);
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View currencyView = inflater.inflate(R.layout.currency_list,parent,false);
    MyViewHolder viewHolder = new MyViewHolder(currencyView);

    return viewHolder;
}

//populate data
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Currency currency = currencies.get(position);
    TextView tvCurrencyValue = holder.tvCurrencyValue;

    TextView tvCurrencyName=holder.tvCurrencyName;
    tvCurrencyName.setText(currencies.get(position).getCode() + " - " + currencies.get(position).getName());
    TextView tvUnitValue = holder.tvUnitValue;
    tvUnitValue.setText("1 " + currencies.get(position).getCode()+ " = " +Double.toString(round(currencies.get(position).getRate()/ Currency.getBaseCurrencyRate(),2)) +" "+ Currency.getBaseCurrencyCode());
    holder.ivFlag.setImageResource(App.getmContext().getResources().getIdentifier("drawable/"+currencies.get(position).getCountryCode().toLowerCase(),null,App.getmContext().getPackageName()));
}

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

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    Collections.swap(currencies, fromPosition, toPosition);
    int tempSort = currencies.get(fromPosition).getSortOrder();
    dataManager.setSortOrder(currencies.get(fromPosition).getCode(), currencies.get(toPosition).getSortOrder());
    dataManager.setSortOrder(currencies.get(toPosition).getCode(), tempSort);
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

@Override
public void onItemDismiss(int position) {
    dataManager.removeDefaultCurrency(currencies.get(position).getCode());
    currencies.remove(position);
    notifyItemRemoved(position);
}


public static class MyViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder,View.OnClickListener{

    public TextView tvCurrencyValue;
    public TextView tvCurrencyDecimals;
    public TextView tvCurrencyName;
    public TextView tvUnitValue;
    public ImageView ivFlag;

    public MyViewHolder(View itemView) {
        super(itemView);

        tvCurrencyValue = (TextView) itemView.findViewById(R.id.tvCurrencyValue);
        tvCurrencyDecimals = (TextView) itemView.findViewById(R.id.tvCurrencyValueDecimals);
        tvCurrencyName = (TextView)itemView.findViewById(R.id.tvCurrencyExp);
        tvUnitValue = (TextView)itemView.findViewById(R.id.tvUnitValue);
        ivFlag = (ImageView)itemView.findViewById(R.id.ivFlag);

        Typeface fontMontserratSemiBold = Typeface.createFromAsset(App.getmContext().getAssets(), "fonts/Montserrat-SemiBold.otf");
        Typeface fontMontserratRegular = Typeface.createFromAsset(App.getmContext().getAssets(), "fonts/Montserrat-Regular.otf");

        tvCurrencyValue.setTypeface(fontMontserratSemiBold);
        tvCurrencyDecimals.setTypeface(fontMontserratSemiBold);
        tvCurrencyName.setTypeface(fontMontserratRegular);
        tvUnitValue.setTypeface(fontMontserratRegular);
    }

    @Override
    public void onItemSelected() {
    }

    @Override
    public void onItemClear() {
        itemView.setBackgroundColor(0);
    }

    @Override
    public void onClick(View view) {

    }
}

} `

0 个答案:

没有答案