如何从Android中单击的RecyclerView项目获取图像

时间:2018-08-14 05:16:34

标签: android android-recyclerview

这是RecyclerView Holder类  从单击的RecyclerView项目中获取图像。

class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
   ImageView gAvatar_photo;
   View layoutView;

    public RecyclerViewHolders(View layoutView) {
        super(layoutView);
        this.layoutView=layoutView;
        layoutView.setOnClickListener(this);
        gAvatar_photo=(ImageView) layoutView.findViewById(R.id.gAvatar_photo);
        gAvatar_photo.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
       if(v.getId()==R.id.gAvatar_photo) {
           Integer img = BoyAvatarAdapter.url[getAdapterPosition()];
           SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
           SharedPreferences.Editor editor = sharedPreferences.edit();
           editor.putInt("imgRes", img);
           editor.apply();
           Intent intent = new Intent(v.getContext(), ProfileActivity.class);
           ProfileActivity pa = new ProfileActivity();
           pa.avatar(img);
           v.getContext().startActivity(intent);
           Toast.makeText(v.getContext(), "" + img, Toast.LENGTH_SHORT).show();
       }

    }
}

3 个答案:

答案 0 :(得分:0)

您可以将网址传递到ProfileActivity,并在ProfileActivity中获取更多内容。

Intent intent = new Intent(v.getContext(), ProfileActivity.class);
intent.putExtra("image", BoyAvatarAdapter.url[getAdapterPosition()];); 
v.getContext().startActivity(intent);

并在ProfileActivity onCreate()中获取传递的资源ID

int image = getIntent().getIntExtra("image", 0); // or use getStringExtra() if you have url instead of resource id.
if(image!=0){
 //  setImageHere.
}

答案 1 :(得分:0)

这是您的适配器

let sortedResults = (userArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "name", ascending: true)]) as! [[String:AnyObject]]

这里是捕获点击事件的活动部分

s_articles_categories_ro

希望它会对您有所帮助。干杯

答案 2 :(得分:0)

尝试: 我有适配器从ImageVIew获取图像并将ImageView发送到其他片段,我需要进行其他活动,只是不要使用getArgument(); 或者您可以搜索有关将数据传递到意图的信息

在您的所有者onclick()中添加此代码

        Drawable myDrawable = mImageView.getDrawable();
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) myDrawable);
        mBitmap = bitmapDrawable.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageBytes = stream.toByteArray();
        mBundle.putByteArray("ResID", imageBytes);


        Fragment fragment = new Edit_Profile_Fragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.addToBackStack(null);
        fragment.setArguments(mBundle);
        fragmentTransaction.commit();

并用于其他课堂活动/片段

    byte[] byteArray = getArguments().getByteArray("ResID");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    mImage_profile.setImageBitmap(bmp);   

它也会正常工作