3 回答
TA貢獻1799條經(jīng)驗 獲得超6個贊
要創(chuàng)建一個新的,你只需要一個實例。您可以從以下任何實例中獲取它(您在方法內(nèi)部有它):IntentContextViewonItemClicked
Context context = view.getContext();
現(xiàn)在要創(chuàng)建并開始,您可以編寫如下代碼:Intent
Intent intent = new Intent(context, ActivityYouNeedToStart.class);
context.startActivity(intent);
因此,最終代碼是:
@Override
public void onItemClicked(View view, int position) {
Context context = view.getContext();
Intent intent = new Intent(context, ActivityYouNeedToStart.class);
context.startActivity(intent);
}
TA貢獻1155條經(jīng)驗 獲得超0個贊
您應(yīng)該將活動/片段提供給 的構(gòu)造函數(shù)。然后你可以使用這個contextadapter
Intent intent = new Intent(context,NewActivity.class); context.startActivity(intent);
TA貢獻1853條經(jīng)驗 獲得超18個贊
以下是在回收視圖中實現(xiàn)子項的單擊偵聽器的一種方法。
public class HotelAdapter extends RecyclerView.Adapter {
private List<Top10Hotel> hotels;
private Context context;
private SavedHotelViewModel viewModel;
public HotelAdapter(List<Top10Hotel> hotels, Context context, SavedHotelViewModel viewModel) {
this.hotels = hotels;
this.context = context;
this.viewModel = viewModel;
}
@NonNull
@Override
public MyHotelViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recommended_hotel, viewGroup, false);
return new MyHotelViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHotelViewHolder holder, int position) {
Top10Hotel hotel = hotels.get(position);
holder.hotelsTextView.setText(hotel.getHotel_name());
Glide.with(context)
.load(hotel.getHotel_image_url())
.error(R.drawable.ic_location_city)
// read original from cache (if present) otherwise download it and decode it
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(holder.hotelsImageView);
long rating = 0;
if(hotel.getHotel_rating() != null && hotel.getHotel_rating().length()>0){
try {
rating = Long.parseLong(hotel.getHotel_rating());
}catch (NumberFormatException e){
rating =1;
}
}
String rate= null;
if(rating>=4.5){
rate = String.valueOf(rating)+" Excellent";
}else if(rating>=4 && (rating < 4.5)){
rate = String.valueOf(rating)+" Very Good";
}else {
rate = String.valueOf(rating)+" Good";
}
holder.rating.setText(rate);
String range = "?"+hotel.getHotel_low_range()+" - "+"?"+hotel.getHotel_high_range();
holder.priceRange.setText(range);
holder.area.setText(hotel.getHotel_area());
holder.saveImage.setOnClickListener(view -> {
RoomBooking hotelSaved = new RoomBooking(hotel.getHotel_id(),
hotel.getHotel_name(),
hotel.getHotel_area(),
hotel.getHotel_low_range(),
hotel.getHotel_high_range(),
hotel.getHotel_rating(),
hotel.getHotel_image_url());
if(hotel.isSaved()){
holder.saveImage.setImageResource(R.drawable.ic_favorite_black);
hotel.setSaved(false);
viewModel.delete(hotelSaved);
Toast.makeText(context, "Removed", Toast.LENGTH_SHORT).show();
}else {
holder.saveImage.setImageResource(R.drawable.ic_saved_love);
hotel.setSaved(true);
viewModel.insert(hotelSaved);
Toast.makeText(context, "saved", Toast.LENGTH_SHORT).show();
}
notifyDataSetChanged();
});
holder.linearLayout.setOnClickListener(view -> navigateToHotelActivity(hotel.getHotel_id(),hotel.getHotel_name()));
holder.hotelsImageView.setOnClickListener(view -> navigateToHotelActivity(hotel.getHotel_id(),hotel.getHotel_name()));
}
private void navigateToHotelActivity(String hotelId, String hotelName) {
Intent intent = new Intent(context, HotelActivity.class);
intent.putExtra("hotelId",hotelId);
intent.putExtra("hotelName",hotelName);
context.startActivity(intent);
}
@Override
public int getItemCount() {
return hotels.size();
}
public class MyHotelViewHolder extends RecyclerView.ViewHolder {
TextView hotelsTextView,rating,priceRange,area;
ImageView hotelsImageView;
ImageView saveImage;
LinearLayout linearLayout;
public MyHotelViewHolder(@NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.linearLayoutH);
hotelsTextView = itemView.findViewById(R.id.hotels_name);
hotelsImageView = itemView.findViewById(R.id.hotels_image);
rating = itemView.findViewById(R.id.hotel_rating);
priceRange = itemView.findViewById(R.id.price_range);
area = itemView.findViewById(R.id.area_name);
saveImage = itemView.findViewById(R.id.save_icon);
}
}
}
添加回答
舉報
