如何在浏览器中为回收视图中的每个项目打开单独的URL?

时间:2018-08-26 19:47:58

标签: java android android-recyclerview recycler-adapter

我正在将Gridlayout for Books应用程序中的Cardview与Recycleview一起使用,该应用程序将从Json获取。我正在寻找打开不同的网址,这将为每个项目下载单独的书籍。我该如何创建这个... ??

enter image description here

下面是我的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Model> mDataset;
    private ImageLoader mImageLoader;
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView nameText;
        public TextView phoneText;
        public NetworkImageView image;
        @SuppressLint("WrongViewCast")
        public ViewHolder(View v) {
            super(v);
            nameText = v.findViewById(R.id.name_text);
            phoneText = v.findViewById(R.id.phone_text);
            image = v.findViewById(R.id.imgAvatar);
            image.setDefaultImageResId(R.mipmap.ic_launcher);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(),"Position: "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
 
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(List<Model> myDataset, Context mCOntext) {
        Log.d("TEST",myDataset.get(0).getName());
        mDataset = myDataset;
        mImageLoader = MySingleton.getInstance(mCOntext).getImageLoader();
    }
 
    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
 
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
 
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        //holder.mTextView.setText(mDataset.get(position).getName());
        Log.d("TEST","Printing Names onBindView Holder"+mDataset.get(position).getName());
        holder.nameText.setText(mDataset.get(position).getName());
        holder.phoneText.setText(mDataset.get(position).getPhone());
        holder.image.setImageUrl(mDataset.get(position).getImage(),mImageLoader);
 
    }
 
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}

下面是我的网格布局的屏幕截图

谢谢。

1 个答案:

答案 0 :(得分:0)

每本书都有URL吗?如果是这样,您可以将其存储在Model中。然后,您可以将其存储在ViewHolder中并在OnClickListener中进行检索。

这是

Model.java

public class Model {

  // your model

  private String mUrl;

  String getUrl() {
    return mUrl;
  }
}

MyAdapter.java

public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView nameText;
    public TextView phoneText;
    public NetworkImageView image;
    public String url;
    @SuppressLint("WrongViewCast")
    public ViewHolder(View v) {
        super(v);
        nameText = v.findViewById(R.id.name_text);
        phoneText = v.findViewById(R.id.phone_text);
        image = v.findViewById(R.id.imgAvatar);
        image.setDefaultImageResId(R.mipmap.ic_launcher);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"Position: "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
                //  you can use url here
            }
        });
    }
}

// ...

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    Model model = mDataset.get(position);
    // - replace the contents of the view with that element
    //holder.mTextView.setText(model.getName());
    Log.d("TEST","Printing Names onBindView Holder"+mDataset.get(position).getName());
    holder.nameText.setText(model.getName());
    holder.phoneText.setText(model.getPhone());
    holder.image.setImageUrl(model.getImage(),mImageLoader);
    holder.url = model.getUrl();
}