更改列中项目的顺序

时间:2018-08-01 01:54:59

标签: android arraylist android-recyclerview

我有这张图片:original image 但我想这样更改。 final image 我该怎么做?我正在使用recyclerView

我的代码是:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    //Boton Atrás
    if(getSupportActionBar() != null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    //codigo de lista
    lista2ParaNotas = findViewById(R.id.lista2);
    lista2ParaNotas.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL,
          false));

    //lista2ParaNotas.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    lista2ParaNotas.addItemDecoration(
            new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    //ArrayList<PuntoNota> puntoNotaList = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista");
    listaFinal = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista");

    AdapterDatos adapter = new AdapterDatos(this, listaFinal);
    lista2ParaNotas.setAdapter(adapter);

}

我有这张图片:原始图片,但我想这样更改。最终图像我该怎么做?我正在使用recyclerView。 我的代码AdapterDatos

public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolderDatos>{

private Context context;
private ArrayList<PuntoNota> puntoNotaList;

public AdapterDatos(Context context, ArrayList<PuntoNota> puntoNotaList) {
    this.context = context;
    this.puntoNotaList = puntoNotaList;
}

@NonNull
@Override
public ViewHolderDatos onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
    ViewHolderDatos viewHolder = new ViewHolderDatos(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolderDatos holder, int position) {

    holder.punto.setText(puntoNotaList.get(position).getPunto());
    holder.nota.setText(puntoNotaList.get(position).getNota());


    }

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

public class ViewHolderDatos extends RecyclerView.ViewHolder {

    TextView punto, nota;

    public ViewHolderDatos(View item) {
        super(item);
        punto = item.findViewById(R.id.idPunto);
        nota = item.findViewById(R.id.idNota);
    }
}
}

1 个答案:

答案 0 :(得分:2)

在进入适配器之前,请对您的收藏进行排序。

listaFinal = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista"); 

Collections.sort(listaFinal , new Comparator<PuntoNota>() {
            @Override
            public int compare(PuntoNota o1, PuntoNota o2) {
                return o1.getNumber().compareTo(o2.getNumber());
            }
        });

AdapterDatos adapter = new AdapterDatos(this, listaFinal);
lista2ParaNotas.setAdapter(adapter)

并用您的自定义类字段替换getNumber()