我有RecyclerView
,其中包含我最新到最旧的数据。在按日期对数据进行排序之前,此onclick RecyclerView
做得很好。它将发送一些变量到另一个意图。
但是在按日期对RecyclerView
进行排序后,RecyclerView
的排序很好,但是addOnItemTouchListener
发送的数据位置错误,这是对RecyclerView
进行排序之前的数据。
我的意思是addOnItemTouchListener
在RecyclerView
被排序之前发送职位。
mRecycleView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecycleView, new RecyclerTouchListener.ClickListener() {
@Override
public void onClick(View view, int position) {
DaftarTugas tugas = listTugas.get(position);
Intent intent = new Intent(getActivity(), PengumumanTugasDetail.class);
Log.e("idtugasput","asd"+idTugasPut.get(position));
daftarTugasRef.child(idTugasPut.get(position)).child("flag").setValue("o");
intent.putExtra("flag", "tugas");
intent.putExtra("namamatkultugas", namaMatkulPut.get(position));
intent.putExtra("deskripsitugas", deskripsiTugasPut.get(position));
intent.putExtra("judultugas", judulTugasPut.get(position));
intent.putExtra("tanggalkumpul", tanggalKumpulPut.get(position));
intent.putExtra("tanggaltugas", tanggalTugasPut.get(position));
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
以及添加列表时的
DaftarTugas tugas = new DaftarTugas();
tugas = dataSnapshot3.getValue(DaftarTugas.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
try {
Date mDate = sdf.parse(tugas.getTanggal_tugas());
long timeInMilliseconds = mDate.getTime();
tugas.setDate_milisecond(timeInMilliseconds);
//store this timeInMilliseconds in your POJO object
Log.e("timestamp2","asd"+tugas.getDate_milisecond());
} catch (ParseException e) {
e.printStackTrace();
}
listTugas.add(tugas);
idTugasPut.add(idTugas);
namaMatkulPut.add(tugas.getNama_tugas());
deskripsiTugasPut.add(tugas.getDeskripsi_tugas());
tanggalKumpulPut.add(tugas.getTanggal_kumpul());
tanggalTugasPut.add(tugas.getTanggal_tugas());
judulTugasPut.add(tugas.getJudul_tugas());
Collections.sort(listTugas, new Comparator<DaftarTugas>() {
public int compare(DaftarTugas o1, DaftarTugas o2) {
if (o1.getDate_milisecond() > o2.getDate_milisecond()) {
return -1;
} else if (o1.getDate_milisecond() < o2.getDate_milisecond()) {
return 1;
} else {
return 0;
}
}
});
mAdapter.notifyDataSetChanged();
RecyclerTouchListenerClass
package com.example.yehezkiel.eclassapp;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildAdapterPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
myAdapter
package com.example.yehezkiel.eclassapp;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Yehezkiel on 7/15/2018.
*/
public class myAdapterTugas extends RecyclerView.Adapter<myAdapterTugas.MyViewHolder> {
private List<DaftarTugas> listTugas;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tanggal_tugas,judul_tugas,tanggal_kumpul,nama_matkul;
public MyViewHolder(View view) {
super(view);
nama_matkul = (TextView) view.findViewById(R.id.judul_p);
judul_tugas = (TextView) view.findViewById(R.id.deskripsi_p);
tanggal_tugas = (TextView) view.findViewById(R.id.tanggal_tugas);
}
}
public myAdapterTugas(List<DaftarTugas> listTugas) {
this.listTugas = listTugas;
}
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.costume_row_tugas, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
DaftarTugas tugas = listTugas.get(position);
holder.nama_matkul.setText(tugas.getNama_tugas());
holder.judul_tugas.setText(tugas.getJudul_tugas());
holder.tanggal_tugas.setText(tugas.getTanggal_tugas());
if(tugas.getFlag().equals("a")){
holder.nama_matkul.setTextColor(Color.BLUE);
}else{
holder.nama_matkul.setTextColor(Color.BLACK);
}
}
@Override
public int getItemCount() {
return listTugas.size();
}
}
答案 0 :(得分:0)
我从@pskink找到了解决方案,我将OnClickListener移到这样的适配器上:
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final DaftarTugas tugas = listTugas.get(position);
holder.nama_matkul.setText(tugas.getNama_tugas());
holder.judul_tugas.setText(tugas.getJudul_tugas());
holder.tanggal_tugas.setText(tugas.getTanggal_tugas());
if(tugas.getFlag().equals("a")){
holder.nama_matkul.setTextColor(Color.BLUE);
}else if(tugas.getFlag().equals("o")){
holder.nama_matkul.setTextColor(Color.BLACK);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), PengumumanTugasDetail.class);
intent.putExtra("flag", "tugas");
intent.putExtra("namamatkultugas", tugas.getNama_tugas());
intent.putExtra("deskripsitugas", tugas.getDeskripsi_tugas());
intent.putExtra("judultugas", tugas.getJudul_tugas());
intent.putExtra("tanggalkumpul", tugas.getTanggal_kumpul());
intent.putExtra("tanggaltugas", tugas.getTanggal_tugas());
view.getContext().startActivity(intent);
}
});