Code
public class PieOptions extends Service {
WindowManager wm;
RelativeLayout ll;
LayoutInflater li;
String UID;
String mFriend;
private String userName;
private FirebaseAuth mAuth;
private FirebaseUser currentUser;
private DatabaseReference mDatabaseReference;
private DatabaseReference mUsersDatabase;
ArrayList<String> array = new ArrayList<String>();
private RecyclerView mUsersList;
private FirebaseRecyclerAdapter<AllUsersHelper, PieOptions.UsersViewHolder> firebaseRecyclerAdapter;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final View myview;
li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
myview = li.inflate(R.layout.service_pie, null);
ImageButton mClose = (ImageButton) myview.findViewById(R.id.close);
mUsersList = (RecyclerView) myview.findViewById(R.id.usersList);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
UID = mAuth.getCurrentUser().getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Friends").child(UID);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("UserData");
mUsersList.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<AllUsersHelper> options =
new FirebaseRecyclerOptions.Builder<AllUsersHelper>()
.setQuery(mDatabaseReference, AllUsersHelper.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsersHelper, UsersViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final UsersViewHolder holder, int position, @NonNull AllUsersHelper model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
holder.setImage(model.getImage());
final String userId = getRef(position).getKey();
mUsersDatabase.orderByChild("Name");
mUsersDatabase.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String mName = dataSnapshot.child("Name").getValue().toString();
String mStatus = dataSnapshot.child("Status").getValue().toString();
String mDisplayImage = dataSnapshot.child("Image").getValue().toString();
if (dataSnapshot.hasChild("Online")) {
String userOnline = dataSnapshot.child("Online").getValue().toString();
holder.setUserOnline(userOnline);
}
holder.setName(mName);
holder.setImage(mDisplayImage);
holder.setStatus(mStatus);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("UserData").child(userId);
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.child("Name").getValue().toString();
Intent intent = new Intent(PieOptions.this, FloatingWindow.class);
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", userName);
startActivity(intent);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
@Override
public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.all_users_layout, parent, false);
return new UsersViewHolder(view);
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_INPUT_METHOD |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
/////////////////////////Another params
params = new WindowManager.LayoutParams(
750,1250,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER;
wm.addView(myview, params);
params.x = 0;
params.y = 0;
final WindowManager.LayoutParams finalParameters = params;
mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(myview);
stopSelf();
}
});
}
public static class UsersViewHolder extends RecyclerView.ViewHolder {
public View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView mDisplayName = (TextView) mView.findViewById(R.id.display_name);
mDisplayName.setText(name);
}
public void setStatus(String status) {
TextView mDisplayStatus = (TextView) mView.findViewById(R.id.display_status);
mDisplayStatus.setText(status);
}
public void setImage(String image) {
CircularImageView mDisplayImage = (CircularImageView) mView.findViewById(R.id.circleImageView);
Picasso.get().load(image).into(mDisplayImage);
}
public void setUserOnline(String userOnline) {
ImageView userOnlineView = (ImageView) mView.findViewById(R.id.online);
if(userOnline.equals("Online")){
userOnlineView.setVisibility(View.VISIBLE);
} else {
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
}
}
尝试在服务中显示回收器视图。该服务没有问题。它膨胀了布局。代码很好,因为我有一个具有相同代码的活动,因此代码工作正常。我只是不确定这是否是在服务中显示回收器视图数据的正确方法。有人可以帮我吗?
问题是服务显示一个空白窗口。像显示除回收器视图之外的其他所有内容一样。
您使用新版本的FirebaseUI,要开始侦听数据(因此数据显示在回收器视图中(,您需要调用startListening()
方法。
mUsersList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
查看此处了解更多信息:
https://github.com/firebase/FirebaseUI-Android/tree/master/database#startstop-listening