如何检索recyclerview位置

lymnna71  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(305)

我正在试着弄清楚我房间里物品的位置 RecyclerView ,主要是位于[0]、[1]和[2]位置的。原因是目前我正在创建一个领导委员会,将突出前三名球员的最高得分。

我试过打电话 getAdapterPosition() 但我想我用错了。

if(holder.getAdapterPosition() == 0)
{
    Glide.with(holder.firstPlace.getContext())
            .load(model.getUrl())
            .into(holder.firstPlace);
}

如果有人能给我指出正确的方向,我将不胜感激。谢谢您。
编辑1:
我的用户适配器

public class UserAdapter extends FirestoreRecyclerAdapter<UserModel, UserAdapter.UserViewHolder> {

public UserAdapter(@NonNull FirestoreRecyclerOptions<UserModel> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull UserAdapter.UserViewHolder holder, int position, @NonNull UserModel model) {

    holder.username.setText(model.getFullName());
    holder.email.setText(model.getEmail());
    holder.score.setText(model.getScore()+"");
    holder.rank.setText(String.valueOf(position + 1));

    Glide.with(holder.userImage.getContext())
            .load(model.getUrl())
            .into(holder.userImage);

    //error here
//        if(holder.getAdapterPosition() == 0)
//        {
//            Glide.with(holder.firstPlace.getContext())
//                    .load(model.getUrl())
//                    .into(holder.firstPlace);
//        }

}

@NonNull
@Override
public UserAdapter.UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_leaderboard_single, parent, false);
    return new UserViewHolder(view);
}

public class UserViewHolder extends RecyclerView.ViewHolder {

    CircleImageView userImage;
    TextView username;
    TextView email;
    TextView score;
    TextView rank;

    CircleImageView firstPlace;
    CircleImageView secondPlace;
    CircleImageView thirdPlace;

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

        userImage = itemView.findViewById(R.id.list_image);
        username = itemView.findViewById(R.id.list_username);
        email = itemView.findViewById(R.id.list_email);
        score = itemView.findViewById(R.id.list_score);
        rank = itemView.findViewById(R.id.leaderboard_position);

        firstPlace = itemView.findViewById(R.id.place1stProfile);
        secondPlace = itemView.findViewById(R.id.place2ndProfile);
        thirdPlace = itemView.findViewById(R.id.place3rdProfile);
    }
}

列表\u排行榜\u single.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:background="#EFCDB4">

<RelativeLayout
    android:layout_width="32dp"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:background="#FFE5C6"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/leaderboard_position"
        android:textStyle="bold"
        android:textColor="#000"
        android:text="##"
        android:layout_centerInParent="true"
        android:textSize="18sp"
        />
</RelativeLayout>

<de.hdodenhof.circleimageview.CircleImageView
    android:paddingTop="5dp"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:id="@+id/list_image"
    android:src="@mipmap/ic_launcher_round"
    android:scaleType="centerCrop"
    android:layout_centerVertical="true"
    android:layout_marginLeft="15dp"
    android:layout_toRightOf="@id/leaderboard_position"
    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/list_username"
        android:layout_width="173dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"

        android:layout_marginTop="26dp"
        android:fontFamily="@font/poppinsmedium"
        android:layout_toRightOf="@id/list_image"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Username"
        android:textColor="#000"
        android:textSize="18sp" />

<TextView
    android:id="@+id/list_email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email"
    android:layout_toRightOf="@+id/list_username"
    android:layout_centerVertical="true"
    android:layout_marginLeft="0dp"
    android:textColor="#000"
    android:visibility="gone"
    />
</LinearLayout>

<LinearLayout
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_marginTop="0dp"
    android:id="@+id/list_score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Score"
        android:textSize="25sp"
    android:textColor="#000"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PTS"
        android:textStyle="bold"
        android:fontFamily="@font/poppinsbold"
        android:textSize="15sp"
        android:textColor="#000"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        app:srcCompat="@drawable/ic_blood_drop"
        android:visibility="gone"
        />

</LinearLayout>

fragment\u leadboard.xml文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 
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="match_parent"
tools:context=".Leaderboard"
android:background="#790604">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="15dp"
        >

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="L E A D E R B O A R D"
        android:fontFamily="@font/poppinsbold"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:textStyle="bold" />

</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_marginBottom="16dp"
        android:minHeight="150dp">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        >

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/place2ndProfile"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="16dp"
            android:scaleType="centerCrop"
            android:layout_centerHorizontal="true"
            android:src="@mipmap/ic_launcher_round"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"
            android:layout_below="@id/place2ndProfile"
            android:fontFamily="@font/poppinsbold"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            android:textColor="#ffffff"
            android:text="2ND"/>

    </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/place1stProfile"
                android:layout_width="110dp"
                android:layout_height="110dp"
                android:layout_marginTop="16dp"
                android:scaleType="centerCrop"
                android:layout_centerHorizontal="true"
                android:src="@mipmap/ic_launcher_round"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:fontFamily="@font/poppinsbold"
                android:layout_below="@id/place1stProfile"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="8dp"
                android:text="1ST"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1">
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/place3rdProfile"
                android:src="@mipmap/ic_launcher_round"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginTop="16dp"
                android:scaleType="centerCrop"
                android:layout_centerHorizontal="true"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:textStyle="bold"
                android:textColor="#FFFFFF"
                android:fontFamily="@font/poppinsbold"
                android:layout_below="@id/place3rdProfile"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="8dp"
                android:text="3RD"/>
        </RelativeLayout>

    </LinearLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/leaderboard_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

</androidx.core.widget.NestedScrollView>
jm2pwxwz

jm2pwxwz1#

你应该打电话 holder.getAdapterPosition 只有在 ViewHolder ; 相反,您可以简单地使用参数 position 传入 onBindViewHolder() .

@Override
protected void onBindViewHolder(@NonNull UserAdapter.UserViewHolder holder, int position, @NonNull UserModel model) {
    // .....
    if(position == 0)
    {
        Glide.with(holder.firstPlace.getContext())
                .load(model.getUrl())
                .into(holder.firstPlace);
    }

相关问题