我正在创建RecyclerView
并从JSON
获取数据,我的应用会检查项目区域是否与前一个区域不同,然后它会显示带有区域名称的标题,否则它将继续创造物品,我希望很清楚,
我无法理解如何使用getItemViewType()
功能查看我的Adapter
public class AdapterItems extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int TYPE_HEADER = 0;
final int TYPE_ITEM = 1;
private ArrayList<Salon> mListItems = new ArrayList<>();
private ImageLoader mImageLoader;
TextView textViewItemSection;
public AdapterItems(Context context) {
mImageLoader = AppController.getInstance().getImageLoader();
}
public void setmListItems(ArrayList<Salon> mListItems) {
this.mListItems = mListItems;
//update the adapter to reflect the new set of mListItems
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
return TYPE_ITEM;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.custom_row_item, parent, false);
return new ItemHolder(itemView);
} else {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.custom_row_header, parent, false);
return new HeaderHolder(itemView);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//textViewItemSection.setVisibility(View.GONE);
/* salon_id = (TextView)itemView.findViewById(R.id.salon_id);
salon_name = (TextView)itemView.findViewById(R.id.salon_name);
salon_address = (TextView)itemView.findViewById(R.id.salon_address);
salon_logo = (NetworkImageView)itemView.findViewById(R.id.salon_logo);
salon_gender = (ImageView)itemView.findViewById(R.id.salon_gender);*/
if (holder instanceof HeaderHolder) {
} else {
ItemHolder itemHolder = (ItemHolder) holder ;
Salon currentItem = mListItems.get(position);
//if (position == 0) {
/* textViewItemSection.setText("");
textViewItemSection.setVisibility(View.VISIBLE);
textViewItemSection.setText(currentItem.getRegion());*/
itemHolder.salon_id.setText(currentItem.getSalon_id());
itemHolder.salon_name.setText(currentItem.getA_name());
itemHolder.salon_address.setText(currentItem.getA_address());
itemHolder.salon_region.setText(currentItem.getRegion());
String urlLogo = currentItem.getLogo();
String urlGender = currentItem.getGender();
loadImages(urlLogo, itemHolder);
}
// }
}
private void loadImages(String urlThumbnail, final ItemHolder holder) {
mImageLoader.get(urlThumbnail, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.salon_logo.setImageBitmap(response.getBitmap());
//holder.salon_gender.setImageBitmap(response.getBitmap());
}
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
@Override
public int getItemCount() {
return mListItems.size();
}
private class HeaderHolder extends RecyclerView.ViewHolder {
public HeaderHolder(View itemView) {
super(itemView);
textViewItemSection = (TextView) itemView.findViewById(R.id.textViewItemSection);
}
}
private class ItemHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView salon_logo;
public ImageView salon_gender;
public TextView salon_id, salon_name, salon_address, salon_region;
public ItemHolder(View itemView) {
super(itemView);
salon_id = (TextView) itemView.findViewById(R.id.salon_id);
salon_name = (TextView) itemView.findViewById(R.id.salon_name);
salon_address = (TextView) itemView.findViewById(R.id.salon_address);
salon_region = (TextView) itemView.findViewById(R.id.salon_region);
textViewItemSection = (TextView) itemView.findViewById(R.id.textViewItemSection);
salon_logo = (ImageView) itemView.findViewById(R.id.salon_logo);
salon_gender = (ImageView) itemView.findViewById(R.id.salon_gender);
}
}
}
这是我获取数据的地方
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
hidePDialog();
try {
JSONArray regionArray = response.getJSONArray("SALON");
for (int a = 0; a < regionArray.length(); a++) {
JSONObject region = regionArray.getJSONObject(a);
JSONArray jsonArray1 = region.getJSONArray("saloon_list");
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject salonDetails = jsonArray1.getJSONObject(i);
Salon salon = new Salon();
//fill salon information
salon.setSalon_id(salonDetails.getString("salon_id"));
salon.setRegion(region.getString("a_name").trim());
salon.setLogo(salonDetails.getString("logo"));
salon.setA_name(salonDetails.getString("a_name"));
salon.setA_address(salonDetails.getString("a_address"));
salonList.add(salon);
}
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "JSONExceptoion", Toast.LENGTH_SHORT).show();
hidePDialog();
e.printStackTrace();
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
//customListAdapter.notifyDataSetChanged();
salonRecycleView.setAdapter(adapterItems);
adapterItems.setmListItems(salonList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "onErrorResponse", Toast.LENGTH_SHORT).show();
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
答案 0 :(得分:0)
在getItemViewType中,您必须比较前一个位置的项目是否来自与当前位置不同的区域:
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
if (mListItems.get(position).getRegion().equalsIgnoreCase(
mListItems.get(position-1).getRegion()) {
return TYPE_HEADER;
}
else {
return TYPE_ITEM;
}
}
问题是你的对象是这样的:
Region | Name
----------------------
| Region 1 | Salon A |
----------------------
| Region 1 | Salon B |
----------------------
| Region 2 | Salon C |
----------------------
| Region 3 | Salon D |
----------------------
| Region 3 | Salon E |
----------------------
您的代码最终会在Recyclerview中显示如下项目:
------------
| Region 1 |
------------
| Salon B |
------------
| Region 2 |
------------
| Region 3 |
------------
| Salon E |
------------
解决此问题的一个简单方法是为适配器创建“Section”对象:
class Section {
boolean isHeader;
String headerText;
Salon salon;
}
更改适配器以获得章节列表:
private ArrayList<Section> mListItems = new ArrayList<>();
但是应该以这种方式创建列表:
isHeader | headerText | Salon
-----------------------------------
| true | Region 1 | null |
-----------------------------------
| false | | Salon A |
-----------------------------------
| false | | Salon B |
-----------------------------------
| true | Region 2 | null |
-----------------------------------
| false | | Salon C |
-----------------------------------
| true | Region 3 | null |
-----------------------------------
| false | | Salon D |
-----------------------------------
| false | | Salon E |
-----------------------------------
将getItemViewType()更改为:
@Override
public int getItemViewType(int position) {
if (mListItems.get(position).isHeader) {
return TYPE_HEADER;
}
else {
return TYPE_ITEM;
}
}
然后相应地更改你的onBindViewHolder()。
如果您不想自己编码,可以使用库SectionedRecyclerViewAdapter。
您可以看到它如何处理getItemViewType()here。