我想在点击
上的缩略图照片时进行移动活动但是当我在
部分创建intent函数时.
它发生了错误,但如果使用Toast它可以..
如何处理?这是我在PhotoAdapter.java中的代码
holder.thumbnail.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View view) {
答案 0 :(得分:1)
您需要在适配器中创建一个接口,并让您的活动(您从哪里移动)实现它。界面将有一个方法(监听器),每当您单击缩略图时使用活动的实例(上下文)并从适配器中的接口调用该方法。您的活动将由操作触发,您将在活动中使用意图。
见下文:
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();
}
}
关于您的活动:
public class MyActivity implements MyAdapterListener(){
...
@override
public void onThumbnailClick(){
//Do your intent here
Intent intent = ...
}
}