我正在尝试使RecyclerView中的每个图像都可点击以打开照片,以及它的标题和描述,但所有这些信息都来自Flickr,我添加了代码,我认为应该是合适,但它似乎没有工作,因为没有任何图像点击,任何关于如何解决这个问题的指导非常感谢。
详细信息类中的一个简单的findByViewId()方法是否足够或者是否比我似乎缺少的更多?就像我说的,任何帮助都表示赞赏。
谢谢!
ImageListAdapter类
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
/**
* Created by Adam on 22/12/2017.
*/
public class ImageListAdapter
extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
private NetworkImageView imageThumbnail;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView imageTitle;
public NetworkImageView mainImage;
public ViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
}
private View.OnClickListener itemClicked = new View.OnClickListener() {
int position = ViewHolder.this.getLayoutPosition();
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
imageThumbnail.setOnClickListener(itemClicked);
itemView.getContext().startActivity(intent);
}
};
}
//Data Source
public ArrayList<ImageInfo> imageList = new ArrayList<ImageInfo>();
// context
private Context context;
public ImageListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return imageList.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create the view for the cell in the list
View v = LayoutInflater.from(context).inflate(R.layout.cell_image_card, parent, false);
ImageListAdapter.ViewHolder vh = new ImageListAdapter.ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ImageInfo imageInfo = imageList.get(position);
holder.imageTitle.setText(imageInfo.title);
holder.mainImage.setImageUrl(imageInfo.url_m,
NetworkMgr.getInstance(context).imageLoader);
// holder.mainImage.setImageResource(imageInfo.imageResource);
}
}
DetailsActivity Class
public class DetailsActivity extends AppCompatActivity {
public TextView imageTitle;
public NetworkImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
photoPosition = intent.getIntExtra("PHOTO_POSITION", 0);
titleView.setText(photo.title);
imageView = NetworkMgr.getInstance(this).imageList.get(photoPosition);
}
}
答案 0 :(得分:1)
您只是忘记在mainImage视图上设置onClick侦听器
第1步:在mainImage上设置点击监听器
public ViewHolder(View itemView) {
super(itemView);
final Context mContext = itemView.getContext();
final int position = getAdapterPosition();
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (NetworkImageView)
itemView.findViewById(R.id.mainImage);
mainImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.putExtra("PHOTO_POSITION", position);
mContext.startActivity(intent);
}
});
}
第2步:在详细活动中获得这样的位置
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
int position = intent.getIntExtra("PHOTO_POSITION", 0);
ImageInfo imageInfo = NetworkMgr.getInstance(this).imageList.get(position);
//use image info to get the image and title details.
}