简体   繁体   English

在项目上单击Recyclerview将图像设置为活动中的另一个imageview

[英]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方法检测子点击,而应ClickListenerbind方法中那样在子视图上使用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.

相关问题 RecyclerView 单击并替换另一个活动中的图像 - RecyclerView click and replace image from another activity 如何解析JSON并置于控件中。 ListView或RecyclerView并单击该项目可以在android中启动另一个活动? - how parse JSON and set into in control. ListView or RecyclerView and click on that item can start another activity in android? 如何在recyclerview中的不同项目位置为imageView设置图像 - How to set image for imageView in different item position in a recyclerview 如何在 Recyclerview 项目单击方法中将图像获取到新活动 - How to Get image to new Activity in Recyclerview item click method 如何在适配器的imageview中设置图像并在其他活动中获取uri? - How to set image in imageview in adapter and getting uri in another activity? 在另一个imageview和textview中设置列表视图项目的图像和文本 - Set image and text of list view item in another imageview and textview RecyclerView 项目单击并打开另一个 recyclerview - RecyclerView item click and open another recyclerview 在RecyclerView中为每一项设置ImageView的大小和Glide,图像大小正在变化 - Set ImageView`s size with Glide for each item in RecyclerView, the image size is changing 在recyclerview项的clickevent上更改Imageview在recyclerview之外的图像 - Change Imageview's image outside recyclerview on a clickevent of recyclerview item 带有单击项上的导航栏的recyclerview打开活动 - recyclerview open activity with navbar on click item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM