如何使适配器中的所有项目可点击?

huangapple go评论62阅读模式
英文:

how to make all items in an adapter clickable?

问题

我根据您提供的内容进行翻译如下:

我按照教程制作了一个应用程序。教程的创建者制作了一个包含可以去的地方照片的“最近”行。

代码被设计成每次单击照片时都显示相同的“详细信息活动”。

问题是:我已经为最近行中的每张照片制作了4个不同的活动,并且我想将每张照片链接到其相应的活动。

一直显示的DetailsActivity
最近行

public class RecentsAdapter extends RecyclerView.Adapter<RecentsAdapter.RecentsViewHolder> {
    Context context;
    List<RecentsData> recentsDataList;
    // RecentsAdapter的代码:

    public RecentsAdapter(Context context, List<RecentsData> recentsDataList) {
        this.context = context;
        this.recentsDataList = recentsDataList;
    }

    @NonNull
    @Override
    public RecentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recents_row_item, parent ,false);
        return new RecentsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecentsViewHolder holder, int position) {
        holder.countryName.setText(recentsDataList.get(position).getCountryName());
        holder.placeName.setText(recentsDataList.get(position).getPlaceName());
        holder.price.setText(recentsDataList.get(position).getPrice());
        holder.placeImage.setImageResource(recentsDataList.get(position).getImageUrl());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(context, DetailsActivity.class);
                context.startActivity(i);
            }
        });
    }

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

    public static final class RecentsViewHolder extends RecyclerView.ViewHolder {
        ImageView placeImage;
        TextView placeName,countryName,price;
        public RecentsViewHolder(@NonNull View itemView) {
            super(itemView);

            placeImage = itemView.findViewById(R.id.place_image);
            placeName = itemView.findViewById(R.id.place_name);
            countryName = itemView.findViewById(R.id.country_name);
            price = itemView.findViewById(R.id.price);
        }
    }
}

希望这能帮助到您。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

I made an app following a tutorial. The creator of the tutorial made a "Recents" row which contains photos of the places you can go to.
The code was made to show the same "details activity" every time you click on a photo.

The problem: I already made 4 different Activities to each photo in the recents row and I want to link each photo to its Activity.

the DetailsActivity that keeps on showing
Recents-row

public class RecentsAdapter extends RecyclerView.Adapter<RecentsAdapter.RecentsViewHolder> {
    Context context;
    List<RecentsData> recentsDataList;
    //the RecentsAdapter code:

    public RecentsAdapter(Context context, List<RecentsData> recentsDataList) {
        this.context = context;
        this.recentsDataList = recentsDataList;
    }

    @NonNull
    @Override
    public RecentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recents_row_item, parent ,false);
        return new RecentsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecentsViewHolder holder, int position) {
        holder.countryName.setText(recentsDataList.get(position).getCountryName());
        holder.placeName.setText(recentsDataList.get(position).getPlaceName());
        holder.price.setText(recentsDataList.get(position).getPrice());
        holder.placeImage.setImageResource(recentsDataList.get(position).getImageUrl());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(context, DetailsActivity.class);
                context.startActivity(i);
            }
        });
    }

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

    public static final class RecentsViewHolder extends RecyclerView.ViewHolder {
        ImageView placeImage;
        TextView placeName,countryName,price;
        public RecentsViewHolder(@NonNull View itemView) {
            super(itemView);

            placeImage = itemView.findViewById(R.id.place_image);
            placeName = itemView.findViewById(R.id.place_name);
            countryName = itemView.findViewById(R.id.country_name);
            price = itemView.findViewById(R.id.price);
        }
    }
}

答案1

得分: 1

Pass extra arguments in your click listener:

@Override
public void onBindViewHolder(@NonNull RecentsViewHolder holder, int position) {
    DataItem item = recentsDataList.get(position); // 简洁的代码

    holder.countryName.setText(item.getCountryName());
    holder.placeName.setText(item.getPlaceName());
    holder.price.setText(item.getPrice());
    holder.placeImage.setImageResource(item.getImageUrl());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, DetailsActivity.class);
            
            // 在这里添加更多的参数
            i.putExtra("place_name", item.getPlaceName());

            context.startActivity(i);
        }
    });
}

And in your DetailsActivity.onCreate():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // 获取地点名称并相应加载布局
    String placeName = getIntent().getStringExtra("place_name");
}
英文:

Pass extra arguments in your click listener:

@Override
public void onBindViewHolder(@NonNull RecentsViewHolder holder, int position) {
    DataItem item = recentsDataList.get(position); //Cleaner code

    holder.countryName.setText(item.getCountryName());
    holder.placeName.setText(item.getPlaceName());
    holder.price.setText(item.getPrice());
    holder.placeImage.setImageResource(item.getImageUrl());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, DetailsActivity.class);
            
            //You can add more arguments here
            i.putExtra("place_name", item.getPlaceName());

            context.startActivity(i);
        }
    });
}

And in your DetailsActivity.onCreate():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    //Get the place name and load the layout accordingly
    String placeName = getIntent().getStringExtra("place_name");
}

答案2

得分: 0

如果您想使用意图传递消息,可以使用 intent.putExtra()

如果您想传递一个捆绑包,可以使用 intent.putExtras()

英文:

If you want to use an intent to convey a message. You can use intent.putExtra()

If you want to pass a bundle, you can use intent.putExtras()

huangapple
  • 本文由 发表于 2023年5月30日 01:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定