我正在关注此Tutorial,并以某种方式陷入从Firebase检索数据。
我从firebase获取数据,所以我认为这不是问题。
我尝试通过调用
来填充列表loadData();
这是函数
public void loadData() {
for (int i = 0; i <= 20; i++) {
mDataSet.add(new Schedules("1","Riverview " , "09151050674", "MOA" ,"01/01/18-11:00PM" , "1000"));
}
}
它有效!但不知何故,我试图用来自firebase的数据来填充它不起作用!
我的代码片段就在下面
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.RecyclerSwipeAdapter;
import com.daimajia.swipe.util.Attributes;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import lim.bookings.Dialogs.AddScheduleDialog;
import lim.bookings.DividerItemDecoration;
import lim.bookings.Objects.Schedules;
import lim.bookings.R;
public class ScheduleFragment extends Fragment {
private List<Schedules> mDataSet;
private TextView textViewList;
private RecyclerView recyclerView;
private FloatingActionButton fab;
private DatabaseReference databaseSchedule;
@Override
public void onStart() {
super.onStart();
//attaching value event listener
databaseSchedule.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//clearing the previous artist list
mDataSet.clear();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//getting artist
Schedules schedules = postSnapshot.getValue(Schedules.class);
//adding artist to the list
mDataSet.add(schedules);
}
//creating adapter
SwipeRecyclerViewAdapter mAdapter = new SwipeRecyclerViewAdapter(getActivity(), mDataSet);
// Setting Mode to Single to reveal bottom View for one item in List
// Setting Mode to Mutliple to reveal bottom Views for multile items in List
(mAdapter).setMode(Attributes.Mode.Single);
recyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment_packages
final View view = inflater.inflate(R.layout.schedule_fragment, container, false);
fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
AddScheduleDialog addScheduleDialog = new AddScheduleDialog(getActivity());
addScheduleDialog.show();
}
});
databaseSchedule = FirebaseDatabase.getInstance().getReference("Sched");
initScheduleView(view);
return view;
}
private void initScheduleView(View view){
recyclerView = view.findViewById(R.id.recycleView);
textViewList = view.findViewById(R.id.textViewList);
// Layout Managers:
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// Item Decorator:
recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.divider)));
mDataSet = new ArrayList<>();
if (mDataSet.isEmpty()) {
recyclerView.setVisibility(View.INVISIBLE);
textViewList.setVisibility(View.VISIBLE);
} else {
recyclerView.setVisibility(View.VISIBLE);
textViewList.setVisibility(View.GONE);
}
// Creating Adapter object
/* Scroll Listeners */
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Log.e("RecyclerView", "onScrollStateChanged");
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
private class SwipeRecyclerViewAdapter extends RecyclerSwipeAdapter<SwipeRecyclerViewAdapter.SimpleViewHolder> {
private Context mContext;
private List<Schedules> schedulesList;
public SwipeRecyclerViewAdapter(Context context, List<Schedules> objects) {
this.mContext = context;
this.schedulesList = objects;
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.swipe_row_item, parent, false);
return new SimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
final Schedules item = schedulesList.get(position);
viewHolder.tvName.setText("Guest Name:"+item.getName());
viewHolder.tvTime.setText("Time:"+ item.getDateTime());
viewHolder.tvLocation.setText("Pick Up:"+item.getAddressFrom());
viewHolder.tvLocation2.setText("Destination:"+item.getAddressTo());
viewHolder.tvPrice.setText("Price:"+item.getPrice());
viewHolder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
// Drag From Left
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Left, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper1));
// Drag From Right
viewHolder.swipeLayout.addDrag(SwipeLayout.DragEdge.Right, viewHolder.swipeLayout.findViewById(R.id.bottom_wrapper));
// Handling different events when swiping
viewHolder.swipeLayout.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onClose(SwipeLayout layout) {
//when the SurfaceView totally cover the BottomView.
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
//you are swiping.
}
@Override
public void onStartOpen(SwipeLayout layout) {
}
@Override
public void onOpen(SwipeLayout layout) {
//when the BottomView totally show.
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
//when user's hand released.
}
});
/*viewHolder.swipeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((((SwipeLayout) v).getOpenStatus() == SwipeLayout.Status.Close)) {
//Start your activity
Toast.makeText(mContext, " onClick : " + item.getName() + " \n" + item.getContactNum(), Toast.LENGTH_SHORT).show();
}
}
});*/
viewHolder.swipeLayout.getSurfaceView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, " onClick : " + item.getName() + " \n" + item.getContactNum(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.tvDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked on Map " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.tvShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on Share " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.tvEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked on Edit " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
viewHolder.tvDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mItemManger.removeShownLayouts(viewHolder.swipeLayout);
schedulesList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, schedulesList.size());
mItemManger.closeAllItems();
Toast.makeText(view.getContext(), "Deleted " + viewHolder.tvName.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
// mItemManger is member in RecyclerSwipeAdapter Class
mItemManger.bindView(viewHolder.itemView, position);
}
@Override
public int getItemCount() {
return schedulesList.size();
}
@Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
// ViewHolder Class
public class SimpleViewHolder extends RecyclerView.ViewHolder {
SwipeLayout swipeLayout;
TextView tvName;
TextView tvTime,tvLocation,tvLocation2;
TextView tvDelete;
TextView tvEdit;
TextView tvShare;
TextView tvDone;
TextView tvPrice;
public SimpleViewHolder(View itemView) {
super(itemView);
swipeLayout = itemView.findViewById(R.id.swipe);
tvName = itemView.findViewById(R.id.tvName);
tvTime = itemView.findViewById(R.id.tvTime);
tvLocation = itemView.findViewById(R.id.tvLocation);
tvLocation2 = itemView.findViewById(R.id.tvLocation2);
tvPrice = itemView.findViewById(R.id.tvPrice);
tvDelete = itemView.findViewById(R.id.tvDelete);
tvEdit = itemView.findViewById(R.id.tvEdit);
tvShare = itemView.findViewById(R.id.tvShare);
tvDone = itemView.findViewById(R.id.tvDone);
}
}
}
}
答案 0 :(得分:0)
我认为您应该在react-dom
中设置recyclerview
适配器。
在initScheduleView()
中,获取数据列表,将其传递到适配器并调用databaseSchedule.addValueEventListener(new ...)
。
对于性能问题,请使用
adapter.notifyDataSetChanged()
代替DiffUtil
。
修改强>
notifyDataSetChanged()
...希望它可以帮助你。