簡體   English   中英

當我單擊 recyclerView 中的項目時打開特定活動

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

我的兩個回收站視圖需要一些幫助(一個名為“recentRecycler”,另一個名為“topPlacesRecycler”)。我的問題是,當我單擊回收站中的特定項目時,如何在特定活動上重定向。 例如:

1-當我單擊“recentRecycler”中的第一項以重定向到“Parlament.class”時

2-當我單擊“topPlacesRecycler”中的第一項以重定向到“Ramada.class”時

等等

我的名為“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);
    }
}

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

        }
    }
}

最近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;
    }
}

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

        }
    }
}

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

我對 Android Studio 還是很陌生,所以我很樂意接受每個反饋。

提前致謝!

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

不使適配器過載的最佳解決方案是使用接口。

像這樣在適配器中聲明一個接口......

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

}

然后在你的活動中添加

     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
  }

快樂編碼。 如果您是 android 的新手,您也可以考慮 Kotlin。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM