我正在尝试使用RecyclerView适配器为卡片视图布局充气,该适配器将按得分降序显示所有玩家的列表。
我希望当用户打开排行榜部分时,它应该在卡片中显示所有用户的得分,但应突出显示他的卡片。我怎样才能做到这一点?我可以将holder.textview与当前用户名进行比较吗?
排行板适配器:
public class LeaderboardAdapter extends RecyclerView.Adapter<LeaderboardAdapter.LeaderboardCustomViewHolder>{
Context mContext;
ArrayList< String> mArrayList =
new ArrayList<>();
ArrayList< String> mArrayList1 =
new ArrayList<>();
ArrayList<GradientDrawable> mGradientDrawables;
String name;
public LeaderboardAdapter(Context context, ArrayList <String> arrayList,ArrayList<String> arrayList1){
mContext=context;
mArrayList=arrayList;
mArrayList1=arrayList1;
mGradientDrawables= new ArrayList<>();
fillGradientList(mContext);
FirebaseAuth mFirebaseAuth;
FirebaseUser mFirebaseUser;
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
final String email = mFirebaseUser.getEmail();
final DatabaseReference ref;
FirebaseDatabase mFirebaseDatabase;
mFirebaseDatabase = FirebaseDatabase.getInstance();
ref = mFirebaseDatabase.getReference();
ref.child("quizz").child("Users").child(email.replace(".", "")).child("name").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
name = dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public LeaderboardCustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.players_list_view,parent,false);
return new LeaderboardCustomViewHolder(view);
}
@Override
public void onBindViewHolder(final LeaderboardCustomViewHolder holder, final int position) {
final String playerName=mArrayList.get(position);
final String playerScore=mArrayList1.get(position);
holder.playerTextView.setText(playerName);
holder.playerScoreView.setText(playerScore);
sort();
if(holder.playerTextView.getText().equals(name))
{
Log.d(TAG,"Current checkName:"+holder.playerTextView.getText().toString());
int color=mContext.getResources().getColor(R.color.wrongAnswerBackground);
holder.playerCardView.setCardBackgroundColor();
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
}, 10);
}
}
@Override
public int getItemCount() {
return mArrayList.size();
}
public class LeaderboardCustomViewHolder extends RecyclerView.ViewHolder {
private TextView playerTextView;
private TextView playerScoreView;
CardView playerCardView;
RelativeLayout playerRelativeLayout;
public LeaderboardCustomViewHolder(View itemView) {
super(itemView);
playerTextView=(TextView)itemView.findViewById(R.id.playerTextView);
playerScoreView=(TextView)itemView.findViewById(R.id.playerScoreView);
playerRelativeLayout=(RelativeLayout)itemView.findViewById(R.id.playerRelativeLayout);
playerCardView=(CardView)itemView.findViewById(R.id.playersCardView);
}
}
}
Card View XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/playersCardView"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_64x"
android:layout_marginBottom="@dimen/dimen_4x"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/dimen_16x"
android:layout_marginTop="@dimen/dimen_4x"
android:clickable="true"
app:cardCornerRadius="@dimen/dimen_8x">
<RelativeLayout
android:id="@+id/playerRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/fab_margin">
<TextView
android:id="@+id/playerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="@dimen/dimen_4x"
android:text="TextView"
android:textAlignment="textStart"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:textSize="@dimen/dimen_16x" />
<TextView
android:id="@+id/playerScoreView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="TextView"
android:textSize="@dimen/activity_horizontal_margin" />
</RelativeLayout>
</android.support.v7.widget.CardView>