0

嗨,我正在尝试使用PinnedHeaderListView该类来获取带有标题的列表视图,就像contacts app在 Android 中一样。

标题显示正常,但是,滚动时它们不会固定到顶部,就像在联系人应用程序中一样。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);

}

}

谁能帮我解决这个问题?谢谢!

4

1 回答 1

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;
    }
于 2013-10-11T06:44:38.497 回答