简体   繁体   English

当我单击 recyclerView 中的项目时打开特定活动

[英]Open an specific activity when I click an item from an recyclerView

I need some help with two of my recycler views(one named "recentRecycler", and the other "topPlacesRecycler").My question is, how do I make to be redirected on a specific Activity when I click a specific item from the recycler.我的两个回收站视图需要一些帮助(一个名为“recentRecycler”,另一个名为“topPlacesRecycler”)。我的问题是,当我单击回收站中的特定项目时,如何在特定活动上重定向。 For example:例如:

1- when I click the first item from the "recentRecycler" to be redirected to "Parlament.class" 1-当我单击“recentRecycler”中的第一项以重定向到“Parlament.class”时

2- when I click the first item from the "topPlacesRecycler" to be redirected to "Ramada.class" 2-当我单击“topPlacesRecycler”中的第一项以重定向到“Ramada.class”时

etc.等等

My Main Activity which is named "BUCint" (The code from the bottom is from a drawerlayout that I use for my project)我的名为“BUCint”的主要活动(底部的代码来自我用于项目的抽屉布局)

public class BUCint extends AppCompatActivity {

    DrawerLayout drawerLayout;
    RecyclerView recentRecycler, topPlacesRecycler;
    RecentsAdapter recentsAdapter;
    TopPlacesAdapter topPlacesAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buc_int);

        drawerLayout = findViewById(R.id.drawer_layout);

        List<RecentsData> recentsDataList = new ArrayList<>();
        recentsDataList.add(new RecentsData("Palatul Parlamentului","Cladire administrativă","40 lei",R.drawable.palatulparlamentului));
        recentsDataList.add(new RecentsData("Arcul de Triumf","Monument istoric","Gratis",R.drawable.arctriumf));
        recentsDataList.add(new RecentsData("Carturesti Carusel","Librarie","Gratis",R.drawable.carturesti));
        recentsDataList.add(new RecentsData("Parcul Herăstrău","Parc","Gratis",R.drawable.parculherastrau));
        recentsDataList.add(new RecentsData("Parcul Cișmigiu","Parc","Gratis",R.drawable.parculcismigiu));
        recentsDataList.add(new RecentsData("Muzeul Antipa","Muzeu","20 lei",R.drawable.muzeulantipa));

        setRecentRecycler(recentsDataList);

        List<TopPlacesData> topPlacesDataList = new ArrayList<>();
        topPlacesDataList.add(new TopPlacesData("Ramada Parc","Sectorul 1","227 lei",R.drawable.ramadaparc));
        topPlacesDataList.add(new TopPlacesData("Berthelot","Sectorul 1","207 lei",R.drawable.bethelot));
        topPlacesDataList.add(new TopPlacesData("Union Plaza","Centru","215 lei",R.drawable.unionplaza));
        topPlacesDataList.add(new TopPlacesData("Rin Grande","Sectorul 4","223 lei",R.drawable.ringrande));
        topPlacesDataList.add(new TopPlacesData("Hilton Garden","Centru","240 lei",R.drawable.hiltongarden));

        setTopPlacesRecycler(topPlacesDataList);


    }

    private  void setRecentRecycler(List<RecentsData> recentsDataList){

        recentRecycler = findViewById(R.id.recent_recycler);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
        recentRecycler.setLayoutManager(layoutManager);
        recentsAdapter = new RecentsAdapter(this, recentsDataList);
        recentRecycler.setAdapter(recentsAdapter);

    }

    private  void setTopPlacesRecycler(List<TopPlacesData> topPlacesDataList){

        topPlacesRecycler = findViewById(R.id.top_places_recycler);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        topPlacesRecycler.setLayoutManager(layoutManager);
        topPlacesAdapter = new TopPlacesAdapter(this, topPlacesDataList);
        topPlacesRecycler.setAdapter(topPlacesAdapter);

    }



    public void ClickMenu(View view){
        BUCM.openDrawer(drawerLayout);
    }

    public void ClickLogo(View view){
        BUCM.closeDrawer(drawerLayout);
    }

    public void ClickHome(View view){
        BUCM.redirectActivity(this, Acasa.class);
    }

    public void ClickLinii(View view){
        BUCM.redirectActivity(this,BUClinii.class);
    }

    public void ClickPreturi(View view){
        BUCM.redirectActivity(this, BUCpreturi.class);
    }

    public void Clickint(View view){
        recreate();
    }

    public void ClickSetari(View view) {
        BUCM.redirectActivity(this, Setari.class);
    }

    public void ClickInformatii(View view){
        BUCM.redirectActivity(this, Informatii.class);
    }

    public void ClickLogout(View view){
        BUCM.logout(this);
    }


    @Override
    protected void onPause(){
        super.onPause();
        BUCM.closeDrawer(drawerLayout);
    }
}

The Adapter for recentRecycler, named RecentsAdapter最近Recycler 的适配器,名为RecentsAdapter

public class RecentsAdapter extends RecyclerView.Adapter<RecentsAdapter.RecentsViewHolder> {

    Context context;
    List<RecentsData> recentsDataList;

    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());
    }

    @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);

        }
    }
}

The DataModel for recentRecycler, named RecentsData最近Recycler 的DataModel,名为RecentsData

package com.example.spinner.model;

public class RecentsData {

    String placeName;
    String countryName;
    String price;
    Integer imageUrl;

    public Integer getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(Integer imageUrl) {
        this.imageUrl = imageUrl;
    }

    public RecentsData(String placeName, String countryName, String price, Integer imageUrl) {
        this.placeName = placeName;
        this.countryName = countryName;
        this.price = price;
        this.imageUrl = imageUrl;
    }

    public String getPlaceName() {
        return placeName;
    }

    public void setPlaceName(String placeName) {
        this.placeName = placeName;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

The Adapter for topPlacesRecycler, named TopPlacesAdapter topPlacesRecycler 的适配器,名为 TopPlacesAdapter

public class TopPlacesAdapter extends RecyclerView.Adapter<TopPlacesAdapter.TopPlacesViewHolder> {

    Context context;
    List<TopPlacesData> topPlacesDataList;

    public TopPlacesAdapter(Context context, List<TopPlacesData> topPlacesDataList) {
        this.context = context;
        this.topPlacesDataList = topPlacesDataList;
    }

    @NonNull
    @Override
    public TopPlacesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.top_places_row_item, parent, false);

        return new TopPlacesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TopPlacesViewHolder holder, int position) {

        holder.countryName.setText(topPlacesDataList.get(position).getCountryName());
        holder.placeName.setText(topPlacesDataList.get(position).getPlaceName());
        holder.price.setText(topPlacesDataList.get(position).getPrice());
        holder.placeImage.setImageResource(topPlacesDataList.get(position).getImageUrl());
    }

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

    public static final class TopPlacesViewHolder extends RecyclerView.ViewHolder{

        ImageView placeImage;
        TextView placeName, countryName, price;

        public TopPlacesViewHolder(@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);

        }
    }
}

The DataModel for topPlacesRecycler, named TopPlacesData topPlacesRecycler 的 DataModel,名为 TopPlacesData

public class TopPlacesData {

String placeName;
String countryName;
String price;
Integer imageUrl;

public Integer getImageUrl() {
    return imageUrl;
}

public void setImageUrl(Integer imageUrl) {
    this.imageUrl = imageUrl;
}

public TopPlacesData(String placeName, String countryName, String price, Integer imageUrl) {
    this.placeName = placeName;
    this.countryName = countryName;
    this.price = price;
    this.imageUrl = imageUrl;
}

public String getPlaceName() {
    return placeName;
}

public void setPlaceName(String placeName) {
    this.placeName = placeName;
}

public String getCountryName() {
    return countryName;
}

public void setCountryName(String countryName) {
    this.countryName = countryName;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}
}

I'm pretty new with Android Studio so every feedback would be gladly accepted.我对 Android Studio 还是很陌生,所以我很乐意接受每个反馈。

Thanks in advance!提前致谢!

 public static final class RecentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        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);

            itemView.setOnClickListener(this);
            //you can do same code for another recyclerview.

        }

 @Override
        public void onClick(View view) {
        
               Intent intent = new Intent(context, Parlament.class);
                view.getContext().startActivity(intent);
            }
    }

The best solution that does not overload the adapter would be to use interface.不使适配器过载的最佳解决方案是使用接口。

Declare an interface in your adapter like this...像这样在适配器中声明一个接口......

public class RecentsAdapter extends RecyclerView.Adapter<RecentsAdapter.RecentsViewHolder> {

    Context context;
    List<RecentsData> recentsDataList;
    private RecentsAdapter.OnRecentItemclickListener listener;

public void setOnRecentItemclickListener(RecentsAdapter.OnRecentItemclickListener listener){
this.listener = listener;
}

    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, listener);
    }

    @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());
    }

    @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, RecentsAdapter.OnRecentItemclickListener listener) {
            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);
            itemView.setOnclickListener( -> {
             if(listner == null)return;
             int pos = getAdapterposition()
              if(pos == RecyclerView.NO_POSITION)return;
               listner.onRecentItemclickListener(pos);
            });

        }
    }
    public interface OnRecentItemclickListener {
     void onRecentItemClickListener(int position);
   }

}

Add then in you activity然后在你的活动中添加

     private  void setRecentRecycler(List<RecentsData> recentsDataList){
    
            recentRecycler = findViewById(R.id.recent_recycler);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
            recentRecycler.setLayoutManager(layoutManager);
            recentsAdapter = new RecentsAdapter(this, recentsDataList);
            recentRecycler.setAdapter(recentsAdapter);
            recentsAdapter.setOnRecentItemclickListener(this::onOpenParlamentClass)
    
        }
  private void onOpenParlamentClass(int position){
   //perform your intent here. you can  also get the clicked item using the position of the data list and pass it to the receiving activity
  }

Happy coding.快乐编码。 If you are new to android you can also consider Kotlin.如果您是 android 的新手,您也可以考虑 Kotlin。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击 RecyclerView 项目时,我希望 TextView 在同一活动中显示项目的标题 - When I click on RecyclerView Item I want the TextView in same activity to display Item's title 如何根据RecyclerView中的单击项在新活动中打开特定片段 - How to open specific Fragment in new Activity depending on clicked item in RecyclerView 在Firebase RecyclerView Cardview中单击项目时如何打开详细信息活动 - How to open a details activity when an item is clicked in Firebase RecyclerView Cardview Android,从特定的TabBar项目打开活动 - Android, Open activity from Specific TabBar item 单击查看寻呼机项目时如何打开新活动? - How can I open a new activity when I click on a view pager item? 当我点击一个recyclerview项目时,如何从一个片段移动到一个新的活动? - how to move from a fragment to a new activity when i clicked an item of recyclerview? 当我单击按钮打开此活动时,它关闭 - When I click the button to open this activity it closes 当我单击RecyclerView项时,我想获取ID - I want to get the id when I click a RecyclerView item RecyclerView在更改项目时单击 - RecyclerView click when item is changed 如何在单独的活动中从RecyclerView打开图像? - How can I open an image from a RecyclerView in a separate activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM