您好我正在尝试使用PinnedHeaderListView
类来获取带标题的列表视图,就像Android中的contacts app
一样。
标题显示正常,但是,在滚动时它们不会固定到顶部,就像在联系人应用程序中一样。相反,它们只是像普通listview
项一样滚动过去。
我该如何解决这个问题?
这是我的片段和适配器类的片段:
public class StaffSectionFragment extends ListFragment implements
ActionBar.TabListener {
private ListFragment mFragment;
private StaffListAdapter adapter;
private ArrayList<Staff> staffList;
public StaffSectionFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from fragment_courses.xml
getActivity().setContentView(R.layout.fragment_staff);
adapter = new StaffListAdapter();
LayoutInflater inflator = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflator.inflate(R.layout.fragment_staff, null);
PinnedHeaderListView listview =(PinnedHeaderListView) layout.findViewById(R.id.pinnedListView);
setListAdapter(adapter);
listview.setAdapter(adapter);
}
private void setupListView()
{
}
public class StaffListAdapter extends SectionedBaseAdapter {
@Override
public Object getItem(int section, int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int section, int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSectionCount() {
//*************DO THIS*****************
// TODO Auto-generated method stub
return 2;
}
@Override
public int getCountForSection(int section) {
//*************DO THIS*****************
// TODO Auto-generated method stub
return 20;
}
@Override
public View getItemView(int section, int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout layout = null;
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflator.inflate(R.layout.list_item, null);
}
else
{
layout = (LinearLayout)convertView;
}
//set item
((TextView) layout.findViewById(R.id.listtextItem)).setText("Section " + section + " item " + position);
return layout;
}
@Override
public View getSectionHeaderView(int section, View convertView,
ViewGroup parent) {
LinearLayout layout = null;
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflator.inflate(R.layout.header_item, null);
}
else
{
layout = (LinearLayout)convertView;
}
//set item
((TextView) layout.findViewById(R.id.headertextItem)).setText("Header for section " + section);
return layout;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something with the data
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
mFragment = new StaffSectionFragment();
// Attach fragment_courses.xml layout
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction ft) {
// TODO Auto-generated method stub
// Remove fragment_courses.xml layout
ft.remove(mFragment);
}
}
任何人都可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:0)
您的适配器应该如下所示,有多少部分?如果你有2个或更多让我知道,这个实现是针对一个部分的,因为你应该覆盖其他方法。
@Override
public Object getItem (int section, int position)
{
if(staffList != null ){
if(section == 0){
if(position < staffList.size()){
return staffList.get(position);
} else {
return // do your second section header
}
}
}
return null;
}
@Override
public long getItemId(int section, int position) {
return position;
}
@Override
public int getSectionCount() {
return 2;
}
@Override
public int getCountForSection(int section) {
if (staffList != null) {
if(section == 0) {
return staffList.size();
} else {
return // your second section
}
}
return 0;
}
@Override
public int getItemViewType(int section, int position) {
if (section == 0 && staffList != null && position == staffList.size() + 1) {
return 1;
} else if (//do your second section ) {
return 2;
}
return 0;
}
@Override
public int getSectionHeaderViewType(int section) {
return section;
}
@Override
public int getSectionHeaderViewTypeCount() {
return 2;
}