gpt4 book ai didi

java - 如何在 recyclerview 适配器中创建 Intent Activity

转载 作者:行者123 更新时间:2023-12-02 11:50:02 31 4
gpt4 key购买 nike

我想在点击缩略图时进行移动 Activity

但是当我在该部分中创建 Intent 函数时

 holder.thumbnail.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {

发生了错误,但如果使用 Toast 就可以..

如何处理?这是我在 PhotoAdapter.java 中的代码

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.List;


public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.MyViewHolder> {

private Context mContext;
public List<Photo> PhotoList;

public PhotoAdapter(Context mContext, List<Photo> PhotoList) {
this.mContext = mContext;
this.PhotoList = PhotoList;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, rating;
public ImageView thumbnail, overflow;

public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
rating= (TextView) view.findViewById(R.id.rating);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
overflow = (ImageView) view.findViewById(R.id.overflow);
}


}

@Override
public PhotoAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.photo_card, parent, false);
return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final PhotoAdapter.MyViewHolder holder, int position) {
final Photo movie = PhotoList.get(position);
holder.name.setText(movie.getName());
holder.rating.setText("Tanggal Lahir : "+String.valueOf( movie.getRating()));


Glide.with(mContext).load(movie.getThumbnail()).into(holder.thumbnail);

holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.overflow);
}
});

holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(movie.getName()=="Aditya Nanda Utama"){
Toast.makeText(mContext,"Anda memilih Saya",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(mContext,"Anda memilih "+movie.getName(),Toast.LENGTH_LONG).show();
}

}
});
}

@Override
public int getItemCount() {

return PhotoList.size();
}


private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_movie, popup.getMenu());

popup.show();
}

}

最佳答案

您需要在适配器中创建一个接口(interface),并让您的 Activity (您从哪里移动)实现它。该接口(interface)将有一个方法(监听器),每当您单击缩略图时,都会使用 Activity 的实例(上下文)并从适配器中的接口(interface)调用该方法。您的 Activity 将由操作触发,并且在您的 Activity 中您将使用 Intent 。

见下文:

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.List;


public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.MyViewHolder> {

private Context mContext;
public List<Photo> PhotoList;
public MyAdapterListener myAdapterListener;

public PhotoAdapter(Context mContext, List<Photo> PhotoList, MyAdapterListener myAdapterListener) {
this.mContext = mContext;
this.PhotoList = PhotoList;
this.myAdapterListener = myAdapterListener;
}

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, rating;
public ImageView thumbnail, overflow;

public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
rating= (TextView) view.findViewById(R.id.rating);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
overflow = (ImageView) view.findViewById(R.id.overflow);
}


}

@Override
public PhotoAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.photo_card, parent, false);
return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final PhotoAdapter.MyViewHolder holder, int position) {
final Photo movie = PhotoList.get(position);
holder.name.setText(movie.getName());
holder.rating.setText("Tanggal Lahir : "+String.valueOf( movie.getRating()));


Glide.with(mContext).load(movie.getThumbnail()).into(holder.thumbnail);

holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.overflow);
}
});

holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//notify listener:
myAdapterListener.onThumbnailClick();
if(movie.getName()=="Aditya Nanda Utama"){
Toast.makeText(mContext,"Anda memilih Saya",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(mContext,"Anda memilih "+movie.getName(),Toast.LENGTH_LONG).show();
}

}
});
}

@Override
public int getItemCount() {

return PhotoList.size();
}


private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_movie, popup.getMenu());

popup.show();
}

interface MyAdapterListener(){
void onThumbnailClick();
}

}

关于您的 Activity :

public class MyActivity implements MyAdapterListener(){
...
@override
public void onThumbnailClick(){
//Do your intent here

Intent intent = ...

}

}

关于java - 如何在 recyclerview 适配器中创建 Intent Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957602/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com