[英]How to open new intent from recylerview onclick
I am still beginner on Android dev, I'm setting up some new view after my recylerview item clicked, but my app are mixed between kotlin and android because i get some source code from internet, but my project since the beginning were compiled by kotlin. 我仍然是Android开发者的初学者,我在我的recylerview项目点击后设置了一些新的视图,但我的应用程序在kotlin和android之间混合,因为我从互联网获得了一些源代码,但我的项目从一开始就是由kotlin编译的。
// Set New View Adapter
// Based on Java
holder.itemImageView.setOnClickListener(new CustomOnItemClickListener(position, new CustomOnItemClickListener.OnItemClickCallback() {
@Override
public void onItemClicked(View view, int position) {
if(holder.itemNameTextView.getText().equals("Pemerintahan (OPD)")){
//open new intent
else if(holder.itemNameTextView.getText().equals("Pelayanan Publik")){
//open new intent
}
the sample code i referenced are used kotlin show new activity started like this 我引用的示例代码使用kotlin show new activity就像这样开始
// Referenced Code
// Based on Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
creativeViewPagerView.setCreativeViewPagerAdapter(NatureCreativePagerAdapter(this))
}
}
notes : createViewPagerView = are attribute value on xml activity main NatureCreativePagerAdapter = are the adapter i will use 注意:createViewPagerView =是xml activity main上的属性值 NatureCreativePagerAdapter =是我将使用的适配器
setCreativeViewPagerAdapter = a function inside a class with code below setCreativeViewPagerAdapter =类中的一个函数,代码如下
// setCreativeViewPagerAdapter\
// Based on Kotlin
fun setCreativeViewPagerAdapter(creativePagerAdapter: CreativePagerAdapter) {
post({
this.creativePagerAdapter = creativePagerAdapter
// Setup adapter for palette manager
paletteCacheManager.setCreativeViewAdapter(creativePagerAdapter)
paletteCacheManager.cachePalettesAroundPositionAsync(0, {
refreshBackgroundColor(0, 0f)
})
// Setup image adapter
creativeImageAdapter.creativePagerAdapter = creativePagerAdapter
creativeHeaderRecycler.layoutManager = LinearLayoutManager(context,
LinearLayoutManager.HORIZONTAL, false)
creativeHeaderRecycler.adapter = creativeImageAdapter
// Setup content adapter
creativeContentAdapter.creativePagerAdapter = creativePagerAdapter
creativeContentViewPager.adapter = creativeContentAdapter
creativeHeaderRecycler.post({ refreshImagesPosition(0f, 0) })
})
}
My question is how to get the referenced sample code to work on my //Set New View adapter Many Thanks. 我的问题是如何让引用的示例代码在我的//设置新视图适配器上工作非常感谢。
You should give the context
of your activity/fragment to constructor of the adapter
. 您应该将您的activity / fragment的
context
提供给adapter
构造函数。 Then you can use this 然后你可以用它
Intent intent = new Intent(context,NewActivity.class);
context.startActivity(intent);
To create a new Intent
you need just an instance of Context
. 要创建新的
Intent
您只需要一个Context
实例。 You can get it from any instance of View
(you have it inside onItemClicked
method): 你可以从任何
View
实例中获取它(你在onItemClicked
方法中有它):
Context context = view.getContext();
Now to create and start Intent
you can write code like this: 现在要创建并启动
Intent
您可以编写如下代码:
Intent intent = new Intent(context, ActivityYouNeedToStart.class);
context.startActivity(intent);
So, final code is: 所以,最终的代码是:
@Override
public void onItemClicked(View view, int position) {
Context context = view.getContext();
Intent intent = new Intent(context, ActivityYouNeedToStart.class);
context.startActivity(intent);
}
Here is the one way to achieve click listener for child item in the recyclerview. 以下是在recyclerview中实现子项的单击侦听器的一种方法。
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.