Android 如何创建';模板&x27;是否在recyclerview中查看每个cardview?

Android 如何创建';模板&x27;是否在recyclerview中查看每个cardview?,android,android-fragments,Android,Android Fragments,我在回收视图中有一些卡片视图。当我点击其中一个,我希望它显示一个不同的布局 这是我点击每张卡片时应该拥有的“模板” <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android

我在回收视图中有一些卡片视图。当我点击其中一个,我希望它显示一个不同的布局

这是我点击每张卡片时应该拥有的“模板”

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/scrollview_sam"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/reading_bg"
    android:fillViewport="true"
    tools:context=".ScrollviewSam"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="188dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="56dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/sam" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="56dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/name"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="32dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/about"
            android:textColor="@color/white"
            android:textSize="35sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView" />

        <TextView
            android:id="@+id/textView19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="4dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/background"
            android:textColor="@color/white"
            android:textSize="35sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView5" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"

            android:layout_marginTop="5dp"
            android:layout_marginEnd="17dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:lineHeight="20dp"
            android:paddingLeft="17sp"
            android:paddingRight="17sp"
            android:text="@string/about_sam"
            android:textAlignment="textStart"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="17dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:lineHeight="20sp"
            android:paddingLeft="17sp"
            android:paddingRight="17sp"
            android:text="@string/background_sam1"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView19" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="17dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="17dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:lineHeight="20sp"
            android:paddingLeft="17sp"
            android:paddingRight="17sp"
            android:text="@string/background_sam2"
            android:textColor="@color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView6" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="56dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/name_sam"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="88dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/gender_male"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView10"
            app:layout_constraintTop_toTopOf="parent" />


        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="88dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/gender"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="116dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/hair_color"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView12"
            android:layout_width="56dp"
            android:layout_height="21dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="115dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/color_brown"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView11"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="148dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/eye_color"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="176dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/cast"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="101dp"
            android:layout_height="39dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="176dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:lineHeight="20dp"
            android:text="@string/cast_norman"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:layout_marginTop="148dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/color_blue"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="224dp"
            android:fontFamily="@font/sackers_gothic_heavy"
            android:text="@string/status_alive"
            android:textColor="@color/white"
            app:layout_constraintStart_toEndOf="@+id/textView3"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="224dp"
            android:fontFamily="@font/sackers_gothic_light"
            android:text="@string/status"
            android:textColor="@color/gray"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <ToggleButton
            android:id="@+id/like"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginStart="30dp"
            android:layout_marginTop="56dp"
            android:background="@drawable/toggle_selector"
            android:textOff=""
            android:textOn=""
            app:layout_constraintStart_toEndOf="@+id/textView8"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_baseline_favorite_border_24" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</ScrollView>

这是我的再生水

  import android.content.Context;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.example.myapplication.R;
    import com.example.myapplication.ScrollviewSam;
    
    import java.util.List;
    
    public class CharacterAdapter extends RecyclerView.Adapter<CharacterAdapter.ViewHolder> {
    
        List<String> name;
        List<Integer> images;
        LayoutInflater layoutInflater;
    
        public CharacterAdapter( Context context, List<String> name, List<Integer> images) {
            this.name = name;
            this.images = images;
            this.layoutInflater = LayoutInflater.from(context);
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = layoutInflater.inflate(R.layout.character_grid_layout,parent,false);
            return new ViewHolder(view);
        }
    
    
    
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.name.setText(name.get(position));
            holder.image.setImageResource(images.get(position));
        }
    
        @Override
        public int getItemCount() {
            return name.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder{
            TextView name;
            ImageView image;
    
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);
                image = itemView.findViewById(R.id.image);
    
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (getAdapterPosition() == 0 ){
                            Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
                            v.getContext().startActivity(intent);
                        }
                    }
                });
    
            }
        }
    
    
    }
导入android.content.Context;
导入android.content.Intent;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.example.myapplication.R;
导入com.example.myapplication.ScrollviewSam;
导入java.util.List;
公共类CharacterAdapter扩展了RecyclerView.Adapter{
名单名称;
列出图像;
LayoutInflater LayoutInflater;
公共字符适配器(上下文上下文、列表名称、列表图像){
this.name=名称;
这个。图像=图像;
this.layoutInflater=layoutInflater.from(上下文);
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
视图=布局更平坦。充气(R.layout.character\u grid\u layout,parent,false);
返回新的ViewHolder(视图);
}
public void onBindViewHolder(@NonNull ViewHolder,int位置){
holder.name.setText(name.get(position));
holder.image.setImageResource(images.get(position));
}
@凌驾
public int getItemCount(){
返回name.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
文本视图名称;
图像视图图像;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
name=itemView.findviewbyd(R.id.name);
image=itemView.findviewbyd(R.id.image);
itemView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(getAdapterPosition()==0){
Intent Intent=新的Intent(v.getContext(),ScrollviewSam.class);
v、 getContext().startActivity(意图);
}
}
});
}
}
}

如您所见,我为该项设置了clickListener。这正是我感到困惑的地方,因为我为视图创建了一个新的活动,但随着一次又一次地创建活动和视图,这将变得超级重复。

您就快到了。您不希望检查适配器位置并启动活动,而是希望将数据传递给ViewHolder,然后再传递给活动。 第一部分可以使用onBindViewHo\lder(),第二部分可以使用
[Intents][1]

// In your adapter's onBindViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.bindDataToYourViewHolder(name.get(position), images.get(position))
}    
上面的选项将设置ViewHolder中的名称和图像。您需要稍微调整您的取景器:

public ViewHolder(@NonNull View itemView) {
    super(itemView);
    name = itemView.findViewById(R.id.name);
    image = itemView.findViewById(R.id.image);
}

public void bindDataToYourViewHolder(String yourName, int yourImage){

   name.setText(yourName);
   image.setImageResource(yourImage);

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), ScrollviewSam.class);
            intent.putExtra("name", yourName)
            // You can use yourImage here as well
            v.getContext().startActivity(intent);
        }                    
    });

}
这样,您就可以将
name.getText()
传递给
ScrollviewSam
活动。 在ScrollviewSam.onCreate()中,您可以通过执行以下操作获取额外的内容:

// In ScrollviewSam Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    String name = getIntent().getStringExtra("name");
}

除了字符串,您还可以通过bundle传递布尔、整数等。如果对象实现了可包裹的界面,您甚至可以传递整个对象。但是,基本的原语应该可以让你开始使用。

这就是我想的,我只是在代码中更改字符串和图像,但我看不出onclick函数如何知道单击了哪张卡,以及如何设置所需的值?添加了BindViewHolder示例我终于明白了!因此,在Scrollview类中,我将只找到TextView,如果键匹配,我将用我希望从捆绑包中获得的字符串替换它!现在尝试一下,效果很好!谢谢你的澄清,manBingo!喜欢创造伟大的东西:肌肉:好的,但现在有另一个问题与喜欢按钮。它在视图中共享。如何绑定到当前视图?因为当我单击它时,更改将在其他视图中可见