[英]Recyclerview on item click set image to another imageview in activity
I'm working on Recyclerview, and I want to set an image to center imageview from Recyclerview's selected item 我正在Recyclerview上工作,我想设置一个图像以将Recyclerview的选定项目居中
Error : You must pass in a non null View
错误:您必须传递非null的视图
here I'm trying to set an image to imageview located in activity_set_back.xml
from Onclick
method of Recyclerview's 在这里,我试图通过Recyclerview的
Onclick
方法将图像设置为位于activity_set_back.xml
imageview
I have: moviesList(ArrayList)
that hold all URL and I'm getting position on click only thing I need to do is set that image to img_back(Imageview) Shown in this code 我有:拥有所有URL的
moviesList(ArrayList)
,仅获得单击的位置,我只需要将该图像设置为img_back(Imageview),如代码所示。
MoviesAdapter.class 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
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
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>
Here your imageview
- img_edit_this
is inside your activity and not inside the itemview. 在这里您
imageview
- img_edit_this
是你的活动中,而不是里面ItemView控件。 Inorder to access the imageview in your activity from adapter, you first need to pass a listner from your activity to adapter during adapter initilalisation. 为了从适配器访问活动中的imageview,首先需要在适配器初始化过程中将活动中的列表器传递给适配器。 Then use this listener in your adapter to change the imageview in activity.
然后在适配器中使用此侦听器来更改活动的imageview。
First create an interface like this 首先创建一个这样的界面
public interface RecyclerImageClick {
void onCenterImageChange(String imagePath);
}
Make your activity implement this, for egs 使您的活动实现这一点,例如
public class MembersList extends AppCompatActivity implements RecyclerImageClick
Now implement the method onCenterImageChange(String imagePath)
in activity 现在在活动中实现
onCenterImageChange(String imagePath)
方法
Now when initialising adapter, pass this listner into it along with arraylist like this 现在,当初始化适配器时,将此列表器和arraylist一起传递给它
data= new MoviesAdapter(ArrayList<Image> moviesList,this);
Now change your adapter constructor like this 现在像这样更改您的适配器构造函数
private RecyclerImageClick listener;
public MoviesAdapter(ArrayList<Image> moviesList,RecyclerImageClick listener) {
this.moviesList = moviesList;
this.listener=listener;
}
Now inside the adapter's imageview click listener call listener.onCenterImageChange(imagePath)
现在在适配器的imageview中,单击listener调用
listener.onCenterImageChange(imagePath)
Now inside your activity, declare your imageview
and set it to the image path passed from adapter
现在在您的活动中,声明您的
imageview
并将其设置为从adapter
传递的图像路径
Don't use onclick
method of RecyclerView
to detect child click instead use ClickListener
on child view in bind
method as in 不要使用
RecyclerView
onclick
方法检测子点击,而应ClickListener
在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);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.