Recyclerview on item 单击将图像设置为 activity 中的另一个图像视图
Recyclerview on item click set image to another imageview in activity
我正在研究 Recyclerview,我想将图像设置为从 Recyclerview 的所选项目中居中的图像视图
Error : You must pass in a non null View
这里我试图通过 Recyclerview 的 Onclick
方法将图像设置为位于 activity_set_back.xml
的图像视图
我有:moviesList(ArrayList)
可以容纳所有 URL
我在点击时获得位置
我唯一需要做的就是将该图像设置为 img_back(Imageview) Shown in this code
MoviesAdapter.class
this is my adapter
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>
不要使用 RecyclerView
的 onclick
方法来检测子点击,而是在 bind
方法的子视图上使用 ClickListener
,如
@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);
});
}
此处您的 imageview
- img_edit_this
在您的 activity 中,而不是在 itemview 中。为了从适配器访问 activity 中的图像视图,您首先需要在适配器初始化期间将监听器从 activity 传递给适配器。然后在您的适配器中使用此侦听器来更改 activity.
中的图像视图
首先创建一个这样的界面
public interface RecyclerImageClick {
void onCenterImageChange(String imagePath);
}
让你的 activity 实施这个,例如
public class MembersList extends AppCompatActivity implements RecyclerImageClick
现在在activity
中实现方法onCenterImageChange(String imagePath)
现在在初始化适配器时,像这样将此监听器与数组列表一起传递给它
data= new MoviesAdapter(ArrayList<Image> moviesList,this);
现在像这样更改适配器构造函数
private RecyclerImageClick listener;
public MoviesAdapter(ArrayList<Image> moviesList,RecyclerImageClick listener) {
this.moviesList = moviesList;
this.listener=listener;
}
现在在适配器的图像视图中单击侦听器调用 listener.onCenterImageChange(imagePath)
现在在您的 activity 中,声明您的 imageview
并将其设置为从 adapter
传递的图像路径
我正在研究 Recyclerview,我想将图像设置为从 Recyclerview 的所选项目中居中的图像视图
Error : You must pass in a non null View
这里我试图通过 Recyclerview 的 Onclick
方法将图像设置为位于 activity_set_back.xml
的图像视图
我有:moviesList(ArrayList)
可以容纳所有 URL
我在点击时获得位置
我唯一需要做的就是将该图像设置为 img_back(Imageview) Shown in this code
MoviesAdapter.class
this is my adapter
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>
不要使用 RecyclerView
的 onclick
方法来检测子点击,而是在 bind
方法的子视图上使用 ClickListener
,如
@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);
});
}
此处您的 imageview
- img_edit_this
在您的 activity 中,而不是在 itemview 中。为了从适配器访问 activity 中的图像视图,您首先需要在适配器初始化期间将监听器从 activity 传递给适配器。然后在您的适配器中使用此侦听器来更改 activity.
首先创建一个这样的界面
public interface RecyclerImageClick {
void onCenterImageChange(String imagePath);
}
让你的 activity 实施这个,例如
public class MembersList extends AppCompatActivity implements RecyclerImageClick
现在在activity
中实现方法onCenterImageChange(String imagePath)
现在在初始化适配器时,像这样将此监听器与数组列表一起传递给它
data= new MoviesAdapter(ArrayList<Image> moviesList,this);
现在像这样更改适配器构造函数
private RecyclerImageClick listener;
public MoviesAdapter(ArrayList<Image> moviesList,RecyclerImageClick listener) {
this.moviesList = moviesList;
this.listener=listener;
}
现在在适配器的图像视图中单击侦听器调用 listener.onCenterImageChange(imagePath)
现在在您的 activity 中,声明您的 imageview
并将其设置为从 adapter