我正在使用Recyclerview,我想将图像设置为Recyclerview所选项目的中心imageview
错误:您必须传入非空视图
此处我尝试将图片设置为位于activity_set_back.xml
的{{1}}的图像视图,来自Recyclerview的Onclick
方法
我有:moviesList(ArrayList)
包含所有网址
而且我在点击时获得了位置
我唯一需要做的就是将该图像设置为img_back(Imageview)此代码中显示
MoviesAdapter.class
这是我的适配器
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
String string_url;
String clicked_url;
private ArrayList<Image> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView img_backimage;
public ImageView img_back;
public MyViewHolder(View itemView) {
super(itemView);
img_backimage = (ImageView) itemView.findViewById(R.id.img_backimage);
img_back =(ImageView) itemView.findViewById(R.id.img_edit_this);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getLayoutPosition(); // gets item position
clicked_url= moviesList.get(position).getPath();
Toast.makeText(view.getContext(),position+"",Toast.LENGTH_LONG).show();
Glide.with(view.getContext()).load(clicked_url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.crossFade()
.into(img_back);
}
}
public MoviesAdapter(ArrayList<Image> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View rootView = inflater.inflate(R.layout.image_list_row, parent, false);
return new MyViewHolder(rootView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
string_url= moviesList.get(position).getPath();
Glide.with(holder.img_backimage.getContext()).load(string_url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.crossFade()
.into(holder.img_backimage);
}
@Override
public int getItemCount() {
return moviesList.size();
}
}
image_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_single_item"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:paddingBottom="8dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="6dp">
<ImageView
android:id="@+id/img_backimage"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:scaleType="fitCenter"
android:src="@drawable/background"/>
</RelativeLayout>
activity_set_back.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.weblogicindia.paint.SetBackActivity">
<RelativeLayout
android:id="@+id/rl_Paint_Area_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/grid_view_container">
<RelativeLayout
android:id="@+id/rl_Paint_Area2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
>
<ImageView
android:id="@+id/img_edit_this"
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/background"/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/grid_view_container"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:background="@color/colorAccent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:4)
此处imageview
- img_edit_this
位于您的活动中,而不在项目视图中。为了从适配器访问活动中的imageview,首先需要在适配器初始化期间将活动的列表器传递给适配器。然后在适配器中使用此侦听器来更改活动中的imageview。
首先创建一个这样的界面
public interface RecyclerImageClick {
void onCenterImageChange(String imagePath);
}
让你的活动实现这一点,对于egs
public class MembersList extends AppCompatActivity implements RecyclerImageClick
现在在活动
中实施方法onCenterImageChange(String imagePath)
现在初始化适配器时,将此列表器与arraylist一起传递给它
data= new MoviesAdapter(ArrayList<Image> moviesList,this);
现在更改你的适配器构造函数
private RecyclerImageClick listener;
public MoviesAdapter(ArrayList<Image> moviesList,RecyclerImageClick listener) {
this.moviesList = moviesList;
this.listener=listener;
}
现在在适配器的imageview内点击监听器调用listener.onCenterImageChange(imagePath)
现在,在您的活动中,声明您的imageview
并将其设置为从adapter
传递的图像路径
答案 1 :(得分:0)
不要使用onclick
RecyclerView
方法来检测子点击,而是使用ClickListener
方法在bind
方法中使用@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
string_url= moviesList.get(position).getPath();
Glide.with(holder.img_backimage.getContext()).load(string_url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.crossFade()
.into(holder.img_backimage);
holder.itemView.setonClickListener(new View.OnClickListener(){
Glide.with(view.getContext()).load(string_url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.fitCenter()
.crossFade()
.into(img_back);
});
}
,如
zoomLevel