活动布局:
安卓:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_list_tasks2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list_tasks"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
... something else...
</RelativeLayout>
活动类 :
rv.setHasFixedSize(true(;
public class AdminActivity2 extends AppCompatActivity {
RecyclerView rv;
private DatabaseReference myRef;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_activity);
rv = (RecyclerView) findViewById(R.id.rv_list_tasks);
myRef = FirebaseDatabase.getInstance().getReference();
FirebaseRecyclerAdapter<Machinist, AdminActivity2.MachinistsViewHolder2> adapter;
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
rv.post(this::supportStartPostponedEnterTransition);
Query query = myRef.child("Машинисты").orderByChild("points/itogo");
adapter = new FirebaseRecyclerAdapter<Machinist, MachinistsViewHolder2>(
Machinist.class,
R.layout.machinist_item,
MachinistsViewHolder2.class,
query){
@Override
public void onBindViewHolder(MachinistsViewHolder2 viewHolder, int position) {
}
@Override
protected void populateViewHolder(MachinistsViewHolder2 viewHolder, Machinist model, int position) {
Log.d("djuyf", "populate");
Typeface mTypeface;
mTypeface = Typeface.create("sans-serif-regular", Typeface.NORMAL);
DatabaseReference itemRef = getRef(position);
viewHolder.cvRemove.setOnClickListener(view -> {
AlertDialog diaBox = askOption(itemRef);
diaBox.show();
});
viewHolder.cvArchive.setOnClickListener(view -> {
Intent startArchiveActivity = new Intent(AdminActivity2.this, Archive.class);
startArchiveActivity.putExtra("Name", viewHolder.tvName.getText().toString());
Gson gson = new Gson();
String myJson = gson.toJson(model);
startArchiveActivity.putExtra("myjson", myJson);
startActivity(startArchiveActivity);
});
viewHolder.tvPosition.setText(String.format(Locale.getDefault(), "%02d", position + 1));
viewHolder.tvName.setText(model.getLastName() + " " + model.getFirstName().charAt(0) + model.getMiddleName().charAt(0));
viewHolder.tvName.setTypeface(mTypeface);
viewHolder.cvTo.setTitleText(pointCount(model.getArchive(), Constants.header.to, false));
viewHolder.cvTo.setOnClickListener(view -> showDialog("Цикл ТО-1", itemRef, model, TO, viewHolder.cvItogo));
viewHolder.cvKs.setTitleText(pointCount(model.getArchive(), Constants.header.ks, false));
viewHolder.cvKs.setOnClickListener(view -> showDialog("Культурное состояние локомотива", itemRef, model, KS, viewHolder.cvItogo));
viewHolder.cvTz.setTitleText(pointCount(model.getArchive(), Constants.header.tz, false));
viewHolder.cvTz.setOnClickListener(view -> showDialog("Посещение технических занятий", itemRef, model, TZ, viewHolder.cvItogo));
viewHolder.cvRl.setTitleText(pointCount(model.getArchive(), Constants.header.rl, false));
viewHolder.cvRl.setOnClickListener(view -> showDialog("Расшифровка лент", itemRef, model, RL, viewHolder.cvItogo));
viewHolder.cvOt.setTitleText(pointCount(model.getArchive(), Constants.header.ot, false));
viewHolder.cvOt.setOnClickListener(view -> showDialog("Охрана труда", itemRef, model, OT, viewHolder.cvItogo));
viewHolder.cvOtk.setTitleText(pointCount(model.getArchive(), Constants.header.oz, false));
viewHolder.cvOtk.setOnClickListener(view -> showDialog("Отказы", itemRef, model, OTK, viewHolder.cvItogo));
viewHolder.cvItogo.setTitleText(pointCount(model.getArchive(), null, true));
viewHolder.cvItogo.setOnClickListener(view -> setSelected(true, true, viewHolder));
}
};
rv.setAdapter(adapter);
}
public static class MachinistsViewHolder2 extends RecyclerView.ViewHolder {
SwipeLayout sl;
RelativeLayout rlDataContainer;
TextView tvPosition;
TextView tvName;
CircleView cvTo;
CircleView cvKs;
CircleView cvTz;
CircleView cvRl;
CircleView cvOt;
CircleView cvOtk;
CircleView cvItogo;
CircleImageView cvRemove;
CircleImageView cvArchive;
public MachinistsViewHolder2(View itemView) {
super(itemView);
rlDataContainer = (RelativeLayout)itemView.findViewById(R.id.rlDataContainer);
sl = (SwipeLayout) itemView.findViewById(R.id.sl);
sl.setShowMode(SwipeLayout.ShowMode.LayDown);
sl.addDrag(SwipeLayout.DragEdge.Left, itemView.findViewById(R.id.bottom_wrapper));
tvPosition = (TextView) itemView.findViewById(R.id.tvPosition);
tvName = (TextView) itemView.findViewById(R.id.tvName);
cvTo = (CircleView) itemView.findViewById(R.id.cvTo);
cvKs = (CircleView) itemView.findViewById(R.id.cvKs);
cvTz = (CircleView) itemView.findViewById(R.id.cvTz);
cvRl = (CircleView) itemView.findViewById(R.id.cvRl);
cvOt = (CircleView) itemView.findViewById(R.id.cvOt);
cvOtk = (CircleView) itemView.findViewById(R.id.cvOtk);
cvItogo = (CircleView) itemView.findViewById(R.id.cvItogo);
cvRemove = (CircleImageView) itemView.findViewById(R.id.cvRemove);
cvArchive = (CircleImageView) itemView.findViewById(R.id.cvArchive);
}
}
}
结果,我看到了列表。但是我不能在文本视图上调用 setText (( 方法。因为从不调用 populateViewHolder。我哪里犯了错误?
我忘记了这个:
super.onBindViewHolder(viewHolder, position(;
inside method onBindViewHolder(MachinistsViewHolder viewHolder, int position(;
我想你需要像这样从onBindViewHolder
中调用populateViewHolder
@Override
public void onBindViewHolder(MachinistsViewHolder2 viewHolder, int position) {
Machinist model = getItem(position);
populateViewHolder(viewHolder, model, position);
}
参考这里。