Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

java - RecyclerView: different ViewType by different button

I have a recycler view to show an activity timeline and three floating buttons that click to add activity. How can I code three buttons to a different view? How can I pass the condition to the Adapter?

Now I can show only one viewType. thx a lot.

Adapter.java

public class TripAdapter extends RecyclerView.Adapter<TripAdapter.TripHolder> {
    private List<Trip> trips = new ArrayList<>();
    private OnItemClickListener listener;

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

        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.trip_item, parent, false);

        return new TripHolder(itemView);

    }

    @Override
    public void onBindViewHolder(@NonNull TripHolder holder, int position) {
        Trip currentTrip = trips.get(position);
        holder.textViewLodgingTitle.setText(currentTrip.getLodgingTitle());
        holder.textTextViewLodgingCheckInDateTime.setText(currentTrip.getLodgingCheckInDateTime());
        holder.textTextViewLodgingCheckOutDateTime.setText(currentTrip.getLodgingCheckOutDateTime());
        holder.textViewLodgingAddress.setText(currentTrip.getLodgingAddress());

    }


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

    public void setTrips(List<Trip> trips) {
        this.trips = trips;
        notifyDataSetChanged();
    }

    public Trip getTripAt(int position) {
        return trips.get(position);
    }


     class TripHolder extends RecyclerView.ViewHolder {
        //lodging
        private TextView textViewLodgingTitle;
        private TextView textTextViewLodgingCheckInDateTime;
        private TextView textTextViewLodgingCheckOutDateTime;
        private TextView textViewLodgingAddress;
        private TextView textViewLodgingPhone;
        private TextView textViewLodgingWebsite;
        private TextView textViewLodgingEmail;

        public TripHolder(@NonNull View itemView) {
            super(itemView);
            context  = itemView.getContext();
            textViewLodgingTitle = itemView.findViewById(R.id.text_view_title);
            textTextViewLodgingCheckInDateTime = itemView.findViewById(R.id.text_view_start_date_time);
            textTextViewLodgingCheckOutDateTime = itemView.findViewById(R.id.text_view_end_date_time);
            textViewLodgingAddress = itemView.findViewById(R.id.text_view_description);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(trips.get(position));
                    }

                }
            });
        }
    }
 
    public interface OnItemClickListener {
        void onItemClick(Trip trip);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }


}

MainActivity.java

//adapter
RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        final TripAdapter adapter = new TripAdapter();
      
        recyclerView.setAdapter(adapter );
        tripViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(TripViewModel.class);
        tripViewModel.getAllTrips().observe(this, new Observer<List<Trip>>() {
            @Override
            public void onChanged(@Nullable List<Trip> trips) {
                adapter.setTrips(trips);
            }
        });




//this on onActivityResult()
else if (requestCode == ADD_LODGING && resultCode == Activity.RESULT_OK) {
            //get data from lodging activity
            String lodgingTitle = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_TITLE);
            String lodgingCheckInDateTime = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_CHECK_IN_DATE_TIME);
            String lodgingCheckOutDateTime = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_CHECK_OUT_DATE_TIME);
            String lodgingDescription = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_DESCRIPTION);
            String lodgingAddress = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_ADDRESS);
            String lodgingPhone = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_PHONE);
            String lodgingWebsite = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_WEBSITE);
            String lodgingEmail = data.getStringExtra(LodgingEditActivity.EXTRA_LODGING_EMAIL);
            String lodgingImagePath = "test";

            Trip lodging = new Trip(lodgingTitle, lodgingCheckInDateTime, lodgingCheckOutDateTime,lodgingDescription, lodgingAddress, lodgingPhone, lodgingWebsite, lodgingEmail,lodgingImagePath);
            tripViewModel.insert(lodging);

            Toast.makeText(this, "lodging save", Toast.LENGTH_SHORT).show();

        }

My application I adapt from codinginflow channel. https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-1-introduction

question from:https://stackoverflow.com/questions/65652311/recyclerview-different-viewtype-by-different-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...