如何将图像从Recyclerview传递到下一个活动

时间:2016-05-16 15:03:42

标签: android android-recyclerview

我真的不知道该怎么做.......我已经尝试了一些教程但没有为我工作...我已经用textview完成了这个但是无法用imageview做到这一点......

这是我的recyclerview适配器

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ViewHolder> {
private static String Title = "title";
public ImageLoader mImageLoader;
//List of superHeroes
List<ListOfData> superHeroes;
private Activity context;

public RecyclerViewDataAdapter(List<ListOfData> superHeroes, Activity context) {
    super();
    //Getting all the superheroes
    this.superHeroes = superHeroes;
    this.context = context;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recyclerview_row, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListOfData superHero = superHeroes.get(position);


    mImageLoader = MyApplication.getInstance().getImageLoader();
    mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
    holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
    holder.textViewName1.setText(superHero.getName());
    //holder.textViewName2.setText(superHero.getName());
    //holder.textViewRank.setText(String.valueOf(superHero.getRank()));
    //holder.textViewRealName.setText(superHero.getRealName());
    //holder.textViewCreatedBy.setText(superHero.getCreatedBy());
    //holder.textViewFirstAppearance.setText(superHero.getFirstAppearance());

    //String powers = "";

    /* for(int i = 0; i<superHero.getPowers().size(); i++){
    powers+= superHero.getPowers().get(i);
}*/

    //holder.textViewPowers.setText(powers);


}

@Override
public int getItemCount() {
    return superHeroes.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
    public NetworkImageView imageView1;
    public TextView textViewName1;
    public TextView textViewName2;
    public TextView textViewRank;
    public TextView textViewRealName;
    public TextView textViewCreatedBy;
    public TextView textViewFirstAppearance;
    public TextView textViewPowers;

    public ViewHolder(View itemView) {
        super(itemView);
        imageView1 = (NetworkImageView) itemView.findViewById(R.id.thumbnail);
        textViewName1 = (TextView) itemView.findViewById(R.id.category_title_one);
    /* textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
    textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
    textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
    textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
    textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
    */

        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ListOfData nextActivitySuperHeroes = (ListOfData) v.getTag();

                Intent intent = new Intent(context, MasterDetail.class);
                intent.putExtra(Title, textViewName1.getText().toString());
                context.startActivity(intent);
            /*BehindTheLayoutFragment fragment = new BehindTheLayoutFragment();
            FragmentTransaction ft = ((AppCompatActivity)context).getFragmentManager().beginTransaction();
            ft.replace(R.id.behind_the_layout_fragment, fragment);
            ft.addToBackStack(null);
            ft.commit(); */
                //((AppCompatActivity)context).getFragmentManager().beginTransaction().replace(R.id.behind_the_layout_fragment, fragment).commit();

            }
        });
    }

}}

以及下一个活动代码

public class MasterDetail extends AppCompatActivity {
private ImageLoader mImageLoader ;
private String imgUrl = "http://api.androidhive.info/json/movies/2.jpg";
private static String Title = "title";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.behind_the_layout_fragment);

    Intent i=getIntent();
    String name = i.getStringExtra(Title);


    TextView textView2  = (TextView) findViewById(R.id.category_title_one);

    textView2.setText(name);

    mImageLoader = MyApplication.getInstance().getImageLoader();

    NetworkImageView imageView = (NetworkImageView)findViewById(R.id.thumbnail);
    imageView.setImageUrl(imgUrl, mImageLoader);
}}

1 个答案:

答案 0 :(得分:2)

在适配器的getBindViewHolder中,编辑一些代码,如下所示。

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final ListOfData superHero = superHeroes.get(position);

    mImageLoader = MyApplication.getInstance().getImageLoader();
    mImageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView1, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
    holder.imageView1.setImageUrl(superHero.getImageUrl(), mImageLoader);
    holder.textViewName1.setText(superHero.getName());

    holder.imageView1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, MasterDetail.class);
            intent.putExtra(Title, superHero.getName());
            intent.putExtra("URL", superHero.getImageUrl());
            context.startActivity(intent);
        }
    });
}    
这将是有帮助的。

页。秒。在MasterDetail中,imgUrl已经给出了。如果你添加

imgUrl = i.getStringExtra("URL");

它会起作用。