我有聊天应用程序,我有片段,并在其中显示我的朋友,目前我想在我的朋友片段中添加用户名旁边的在线和离线功能,我写了代码,它没有问题,但我需要在用户的全名旁边添加图标在线或离线,在布局中我添加了图标,但我无法在代码中控制这些图标,我希望如果用户在线..在线图标设置visibilty可见和离线图标设置visibilty隐形我希望如果用户离线..在线图标设置隐形和离线图标设置可见..这是我的问题!
注意:图标是ImageView ...
我在fisrt类和布局xml中的问题上面写了评论..
我必须对第一堂课(FriendsFragment)进行分类:
public class FriendsFragment extends Fragment {
public static int setOnlineSta;
RecyclerView results_list;
DatabaseReference mUDB,mUDBSec;
FirebaseUser mCurrentUser;
FriendsAdapter friendsAdapter;
ArrayList<String> fullNameList,userStatusList, userIdList, profileImageList;
View mMainView;
public FriendsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_friends, container, false);
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = mCurrentUser.getUid();
mUDB =
FirebaseDatabase.getInstance()
.getReference().child("Friends").child(current_ui
d);
mUDB.keepSynced(true);
mUDBSec = FirebaseDatabase.getInstance().getReference();
mUDBSec.keepSynced(true);
results_list = (RecyclerView) mMainView.findViewById(R.id.users_list);
results_list.setHasFixedSize(true);
results_list.setLayoutManager(new LinearLayoutManager(getActivity()));
results_list.addItemDecoration(new DividerItemDecoration(getActivity(),
LinearLayoutManager.VERTICAL));
fullNameList = new ArrayList<>();
userStatusList = new ArrayList<>();
userIdList = new ArrayList<>();
profileImageList = new ArrayList<>();
setAdapter();
return mMainView;
}
private void setAdapter() {
mUDB.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final String uid = snapshot.getKey();
DatabaseReference userRef = mUDBSec.child("Users").child(uid);
userRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uidSec = uid;
userIdList.add(uidSec);
String full_name = dataSnapshot.child("name").getValue(String.class);
String user_status = dataSnapshot.child("status").getValue(String.class);
String profile_pic = dataSnapshot.child("image").getValue(String.class);
fullNameList.add(full_name);
userStatusList.add(user_status);
profileImageList.add(profile_pic);
if (dataSnapshot.hasChild("online"))
{
// My Problem Here
Boolean userOnline_Check = (Boolean)
dataSnapshot.child("online").getValue();
if (userOnline_Check == true)
{
}
if (userOnline_Check == false)
{
}
}
friendsAdapter = new FriendsAdapter(getActivity(), fullNameList,
userStatusList, userIdList, profileImageList);
results_list.setAdapter(friendsAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}}
二等(FriendsAdapter):
public class FriendsAdapter extends
RecyclerView.Adapter<FriendsAdapter.FriendViewHolder> {
Context context;
ArrayList<String> fullNameList,userStatusList, userIdList,profileImageList;
class FriendViewHolder extends RecyclerView.ViewHolder
{
ImageView profileImage;
ImageView active_emo, not_active_emo;
TextView full_name, user_status,user_id;
public FriendViewHolder(View itemView) {
super(itemView);
full_name = (TextView) itemView.findViewById(R.id.user_single_name);
user_status = (TextView) itemView.findViewById(R.id.username_friend);
user_id = (TextView) itemView.findViewById(R.id.user_single_id);
profileImage = (ImageView) itemView.findViewById(R.id.user_single_image);
active_emo = (ImageView) itemView.findViewById(R.id.active_state);
not_active_emo = (ImageView) itemView.findViewById(R.id.not_active_state);
}
}
public FriendsAdapter(Context context, ArrayList<String> fullNameList,
ArrayList<String> userStatusList, ArrayList<String> userIdList,
ArrayList<String> profileImageList) {
this.context = context;
this.fullNameList = fullNameList;
this.userStatusList = userStatusList;
this.userIdList = userIdList;
this.profileImageList = profileImageList;
}
@Override
public FriendsAdapter.FriendViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view =
LayoutInflater.from(context).inflate(R.layout.users_single_layout, parent,
false);
return new FriendsAdapter.FriendViewHolder(view);
}
@Override
public void onBindViewHolder(final FriendsAdapter.FriendViewHolder holder,
final int position) {
holder.full_name.setText(fullNameList.get(position));
holder.user_status.setText(userStatusList.get(position));
holder.user_id.setText(userIdList.get(position));
Glide.with(context).load(profileImageList.get(position)).into(holder.profile
Image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence options[] = new CharSequence[]{"Open Profile", "Send message"};
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if(i == 0){
Intent profileIntent = new Intent(context, UserProfile.class);
profileIntent.putExtra("USER_ID",String.valueOf(userIdList.get(position)));
context.startActivity(profileIntent);
}
if(i == 1){
Intent chatIntent = new Intent(context, Search.class);
chatIntent.putExtra("USER_ID",String.valueOf(userIdList.get(position)));
context.startActivity(chatIntent);
}
}
});
builder.show();
}
});
}
@Override
public int getItemCount() {
return fullNameList.size();
}}
我的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/user_single_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:padding="2dp"
android:src="@drawable/profile_default" />
<LinearLayout
android:layout_width="233dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/user_single_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full name"
android:textColor="@color/bluePrim"
android:textSize="20sp" />
<TextView
android:id="@+id/username_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name" />
<TextView
android:id="@+id/user_single_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Id"
android:visibility="gone" />
</LinearLayout>
<-- the icons below !-->
<ImageView
android:id="@+id/active_state"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/active"
android:visibility="invisible" />
<ImageView
android:id="@+id/not_active_state"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/notactive"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
谢谢..