问题
I'm working on Recyclerview, and I want to set an image to center imageview from Recyclerview's selected item
Error : You must pass in a non null View
here I'm trying to set an image to imageview located in activity_set_back.xml
from Onclick
method of Recyclerview's
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
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>
回答1:
Here your imageview
- img_edit_this
is inside your activity and not inside the 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. Then use this listener in your adapter to change the imageview in activity.
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
Now when initialising adapter, pass this listner into it along with arraylist like this
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)
Now inside your activity, declare your imageview
and set it to the image path passed from adapter
回答2:
Don't use onclick
method of RecyclerView
to detect child click instead use ClickListener
on child view in bind
method as in
@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);
});
}
来源:https://stackoverflow.com/questions/49397689/recyclerview-on-item-click-set-image-to-another-imageview-in-activity