gpt4 book ai didi

java - 使用“Glide”时遇到问题

转载 作者:行者123 更新时间:2023-12-01 16:22:43 29 4
gpt4 key购买 nike

这是我的User.Adapter.java的代码:

package com.example.wanderspacetraveljournalapp.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.wanderspacetraveljournalapp.Fragment.ProfileFragment;
import com.example.wanderspacetraveljournalapp.Model.User;
import com.example.wanderspacetraveljournalapp.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

private Context mContext;
private List<User> mUsers;

private FirebaseUser firebaseUser;

public UserAdapter(Context mContext, List<User> mUsers) {
this.mContext = mContext;
this.mUsers = mUsers;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.user_item, viewGroup, false);
return new UserAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {

firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

final User user = mUsers.get(i);

viewHolder.btn_follow.setVisibility(View.VISIBLE);

viewHolder.username.setText(user.getUsername());
viewHolder.fullname.setText(user.getFullname());
Glide.with(mContext).load(user.getImageurl()).into(viewHolder.image_profile);
isFollowing(user.getId(), viewHolder.btn_follow);

if (user.getId().equals(firebaseUser.getUid())) {
viewHolder.btn_follow.setVisibility(View.GONE);
}

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREFS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", user.getId());
editor.apply();

((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
}
});

viewHolder.btn_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolder.btn_follow.getText().toString().equals("follow")){
FirebaseDatabase.getInstance().getReference().child("follow").child(firebaseUser.getUid())
.child("following").child(user.getId()).setValue(true);
FirebaseDatabase.getInstance().getReference().child("follow").child(user.getId())
.child("followers").child(firebaseUser.getUid()).setValue(true);
} else {
FirebaseDatabase.getInstance().getReference().child("follow").child(firebaseUser.getUid())
.child("following").child(user.getId()).removeValue();
FirebaseDatabase.getInstance().getReference().child("follow").child(user.getId())
.child("followers").child(firebaseUser.getUid()).removeValue();
}
}
});

}

@Override
public int getItemCount() {
return mUsers.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

public TextView username;
public TextView fullname;
public CircleImageView image_profile;
public Button btn_follow;

public ViewHolder(@NonNull View itemView) {
super(itemView);

username = itemView.findViewById(R.id.username);
fullname = itemView.findViewById(R.id.fullname);
image_profile = itemView.findViewById(R.id.image_profile);
btn_follow = itemView.findViewById(R.id.btn_follow);
}
}

private void isFollowing(final String userid, final Button button) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
.child("Follow").child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(userid).exists()){
button.setText("following");
} else {
button.setText("follow");
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

}


现在我面临的问题是,当我单击用户个人资料的“关注”按钮时,我没有在“关注”中打开它。在Firebase数据库中,它正在正确显示,我正在关注该特定用户。但在应用程序中,它无法正常发生。我知道这是因为“滑行”,但找不到完美的答案。所以在这里,我分享了我的代码,并等待可能对我有用的完美答案。我正在使用最新的Android Studio 4.0。这是我的 build.gradle

    implementation ("com.github.bumptech.glide:glide:4.9.0") {
exclude group: "com.android.support"
}
annotationProcessor 'androidx.annotation:annotation:1.1.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation ("com.github.bumptech.glide:glide:4.9.0@aar") {
transitive = true
}

最佳答案

对于应用程序端的更改值,需要通知您的适配器

在最后一个notifyDataSetChanged(); btn_follow中添加onClickListener

关于java - 使用“Glide”时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62228635/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com