我仍然是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
}
我引用的示例代码使用kotlin演示了这样的新活动
// 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))
}
}
注释: createViewPagerView =是xml活动主体上的属性值 NatureCreativePagerAdapter =我将使用的适配器
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) })
})
}
我的问题是如何使引用的示例代码在我的// Set New View适配器上工作 非常感谢。
答案 0 :(得分:1)
您应将活动/片段的context
交给adapter
的构造函数。然后您可以使用
Intent intent = new Intent(context,NewActivity.class);
context.startActivity(intent);
答案 1 :(得分:1)
要创建一个新的Intent
,您只需一个Context
的实例。您可以从View
的任何实例中获取它(您可以在onItemClicked
方法中找到它):
Context context = view.getContext();
现在要创建并启动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);
}
答案 2 :(得分:1)
这是在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);
}
}
}