我正在实施来自this的粘贴标头,它显示所有带标题的数据,但标题不粘。
我尝试使用LinearLayoutManager而不是库的LayoutManager,但它仍然无法正常工作。
public class MyListAdapterTest extends SectioningAdapter
{
Locale locale = Locale.getDefault();
static final boolean USE_DEBUG_APPEARANCE = false;
private class Section
{
String alpha;
ArrayList<Users> usersArrayList = new ArrayList<>();
}
public class ItemViewHolder extends SectioningAdapter.ItemViewHolder
{
RelativeLayout relativeLayout;
TextView name, product, description;
ImageView mainimg, moreimg;
public ItemViewHolder(View itemView)
{
super(itemView);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.card_layout_relative_layout);
name = (TextView) itemView.findViewById(R.id.card_layout_name_txt);
product = (TextView) itemView.findViewById(R.id.card_layout_product_txt);
description = (TextView) itemView.findViewById(R.id.card_layout_description_txt);
mainimg = (ImageView) itemView.findViewById(R.id.card_layout_image);
moreimg = (ImageView) itemView.findViewById(R.id.card_layout_moreimage);
}
}
public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder
{
TextView titleTextView;
public HeaderViewHolder(View itemView)
{
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.card_header_txt);
}
}
List<Users> usersList;
ArrayList<Section> sections = new ArrayList<>();
public MyListAdapterTest()
{
}
public List<Users> getUsersList()
{
return usersList;
}
public void setUsersList(List<Users> usersList)
{
this.usersList = usersList;
sections.clear();
// sort usersList into buckets by the first letter of last name
char alpha = 0;
Section currentSection = null;
for (Users users : usersList)
{
if (users.getContact().charAt(0) != alpha)
{
if (currentSection != null)
{
sections.add(currentSection);
}
currentSection = new Section();
alpha = users.getContact().charAt(0);
currentSection.alpha = String.valueOf(alpha);
}
if (currentSection != null)
{
currentSection.usersArrayList.add(users);
}
}
sections.add(currentSection);
notifyAllSectionsDataSetChanged();
}
@Override
public int getNumberOfSections()
{
return sections.size();
}
@Override
public int getNumberOfItemsInSection(int sectionIndex)
{
return sections.get(sectionIndex).usersArrayList.size();
}
@Override
public boolean doesSectionHaveHeader(int sectionIndex)
{
return true;
}
@Override
public boolean doesSectionHaveFooter(int sectionIndex)
{
return false;
}
@Override
public ItemViewHolder onCreateItemViewHolder(ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.card_layout, parent, false);
return new ItemViewHolder(v);
}
@Override
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.card_header, parent, false);
return new HeaderViewHolder(v);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex)
{
Section s = sections.get(sectionIndex);
ItemViewHolder ivh = (ItemViewHolder) viewHolder;
Users person = s.usersArrayList.get(itemIndex);
ivh.name.setText(person.getContact());
ivh.product.setText(person.getContactPhone());
ivh.description.setText(person.getSR_No() + "");
}
@SuppressLint("SetTextI18n")
@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex)
{
Section s = sections.get(sectionIndex);
HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
if (USE_DEBUG_APPEARANCE)
{
hvh.itemView.setBackgroundColor(0x55ffffff);
hvh.titleTextView.setText(pad(sectionIndex * 2) + s.alpha);
} else
{
hvh.titleTextView.setText(s.alpha);
}
}
private String pad(int spaces)
{
StringBuilder b = new StringBuilder();
for (int i = 0; i < spaces; i++)
{
b.append(' ');
}
return b.toString();
}
}
我做错了什么?
答案 0 :(得分:0)
我弄清楚问题是什么,我没有在我的适配器中的onBindItemViewHolder和onBindHeaderViewHolder中实现super,所以它应该是
@SuppressLint("SetTextI18n")
@Override
public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, int itemIndex)
{
super.onBindItemViewHolder(viewHolder, sectionIndex, itemIndex);
Section s = sections.get(sectionIndex);
ItemViewHolder ivh = (ItemViewHolder) viewHolder;
Users person = s.usersArrayList.get(itemIndex);
ivh.name.setText(person.getContact());
ivh.product.setText(person.getContactPhone());
ivh.description.setText(person.getSR_No() + "");
}
@SuppressLint("SetTextI18n")
@Override
public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex)
{
super.onBindHeaderViewHolder(viewHolder, sectionIndex);
Section s = sections.get(sectionIndex);
HeaderViewHolder hvh = (HeaderViewHolder) viewHolder;
if (USE_DEBUG_APPEARANCE)
{
hvh.itemView.setBackgroundColor(0x55ffffff);
hvh.titleTextView.setText(pad(sectionIndex * 2) + s.alpha);
} else
{
hvh.titleTextView.setText(s.alpha);
}
}