向RecyclerView适配器添加多个对象

时间:2018-07-06 21:19:55

标签: java android android-recyclerview

在片段FilteredResultsFragment.java中,我有一个回收站视图和一个 ImageView ,其中显示了从 Firebase Realtime数据库检索的图像。 这段代码来自我的FilteredResultaFragment.java

public class FilteredResultsFragment extends android.support.v4.app.Fragment {
ImageAdapter imageAdapter;
List<String> listOfImages = new ArrayList<>();
List<String> listOfNames = new ArrayList<>();
private RecyclerView recyclerView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View viewFilteredResults = inflater.inflate(R.layout.fragment_filtered_results, container, false);
    recyclerView = viewFilteredResults.findViewById(R.id.recyclerViewFiltered);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    imageAdapter = new ImageAdapter(listOfImages);
    imageAdapter = new ImageAdapter(listOfNames);
    recyclerView.setAdapter(imageAdapter);
    return viewFilteredResults;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            listOfImages.addAll(getArguments().getStringArrayList("listOfImages"));
            imageAdapter.notifyDataSetChanged();
        }
    }
}

如您所见,我已经为我的回收站视图创建了一个适配器。它的名称为ImageAdapter.java,在这里我可以在 ImageView 中显示图像。 一切正常,如您所见:

enter image description here

问题是,我也想显示一个带有值的TextView。我该如何实现? 这是我的ImageAdapter-java

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
List<String> listaPaisesFiltrados;
List<String> listaNomesFiltrados;

ImageAdapter(List<String> listaPaisesFiltrados) {
    this.listaPaisesFiltrados = listaPaisesFiltrados;

}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_view, parent, false);


    return new ImageViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
    String imageUrl = listaPaisesFiltrados.get(holder.getAdapterPosition());
    Log.d("ADAPTER", "imageUrl : " + imageUrl + " position : " + holder.getAdapterPosition());

    Glide.with(holder.imageView.getContext()).load(imageUrl).centerCrop().into(holder.imageView);
}

@Override
public int getItemCount() {
    Log.d("ADAPTER", "SIZE : " + listaPaisesFiltrados.size());
    return listaPaisesFiltrados.size();
}


public class ImageViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;

        public ImageViewHolder(View itemView) {

            super(itemView);
            imageView = itemView.findViewById(R.id.imgPaisFiltrado);

        }
    }

}

顺便说一句,我想要在 TextView 中显示的值已经被检索,然后我通过ImageAdapter通过{{1 }}

1 个答案:

答案 0 :(得分:2)

要做的就是创建一个具有imageview和textview的新布局。

例如,如果您制作并命名为city_thumbnail_view 然后,您将在onCreateViewHolder中进行操作。

View itemView = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.city_thumnail_view, parent, false);

然后在ViewHolder中,只需添加具有其ID名称的textview

public class ImageViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;
    TextView textView;

    public ImageViewHolder(View itemView) {

        super(itemView);
        imageView = itemView.findViewById(R.id.imgPaisFiltrado);
        textView = itemView.findViewById(R.id.cityName);
    }
}

然后在onBindViewHolder中创建一个包含cityName和imageUrl的对象,而不是仅包含一个字符串列表。

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
    myCityNameObject city = listaPaisesFiltrados.get(holder.getAdapterPosition());

    textView.setText(city.cityName);    
    Glide.with(holder.imageView.getContext()).load(imageUrl).centerCrop().into(holder.imageView);
}

以下是该对象的外观示例。

public class City
{
    // Instance Variables
    String cityName;
    String cityImageUrl;

    // Constructor Declaration of Class
    public City(String cityName, String cityImageUrl)
    {
        this.cityname = cityName;
        this.cityImageUrl = cityImageUrl;
    }

    // Get City
    public String getCityName()
    {
        return cityName;
    }

    // Set City
    public String setCityName(String name)
    {
        cityName = name;
    }

    // Get City Image Url
    public int getCityImageUrl()
    {
        return cityImageUrl;
    }

    // Set City Image Url
    public String setCityImageUrl(String url)
    {
    cityImageUrl = url;
    }
}

更改后,您的适配器应该是这样

public class ImageAdapter extends RecyclerView.Adapter { 
    List<City> listaPaisesFiltrados; 

    ImageAdapter(List<City> listaPaisesFiltrados) {
    this.listaPaisesFiltrados = listaPaisesFiltrados;

    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int             viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.city_thumnail_view, parent, false);


        return new ImageViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
    City city = listaPaisesFiltrados.get(position);

    holder.cityNameTextView.setText(city.getCityName());    
    Glide.with(holder.imageView.getContext()).load(imageUrl).centerCrop().into(holder.imageView);
    }

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


    public class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView cityNameTextView;

            public ImageViewHolder(View itemView) {

            super(itemView);
            imageView = itemView.findViewById(R.id.imgPaisFiltrado);
            cityNameTextView = itemView.findViewById(R.id.cityNameText);
        }
    }
}