StickyListHeaders和MergeAdapter

时间:2013-01-14 01:49:07

标签: android android-layout android-listview

我有一个我正在尝试设置的布局,它由三个不同的布局合并为一个。我有两个RelativeLayouts作为前两个项目,列表适配器作为第三个。使用MergeAdapter的原因是因为我想要整个页面滚动,但我希望整个布局中的第二个总项目(第二个RelativeLayout)坚持到顶部。所以我正在尝试使用StickyListHeaders Library来完成它。

它似乎并没有被广泛使用,即使它的想法很棒。无论如何,我必须在列表适配器中覆盖两种方法,在本例中是MergeAdapter。我一直得到一个NullPointerException,但它并没有告诉我我的代码在哪里发生。因此,如果有人有使用该库的经验,更具体地说使用MergeAdapter的库,你能告诉我我做错了什么吗?这是我的MergeAdapter的代码。

private class ProfileMergeAdapter extends MergeAdapter implements StickyListHeadersAdapter{

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {

        LayoutInflater nameInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = (RelativeLayout) nameInflater.inflate(R.layout.name_line, null);
        profilePhotoBottom = (ImageView) convertView.findViewById(R.id.profile_pic_bottom);
        TextView userName = (TextView) convertView.findViewById(R.id.userName);
        userName.setText(Utility.userName);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        return 1;
    }

}

这是logcat:

01-13 20:39:21.237: W/dalvikvm(4917): threadid=1: thread exiting with uncaught exception (group=0x40de5930)
01-13 20:39:21.245: E/AndroidRuntime(4917): FATAL EXCEPTION: main
01-13 20:39:21.245: E/AndroidRuntime(4917): java.lang.NullPointerException
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:523)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.View.measure(View.java:15513)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at com.emilsjolander.components.stickylistheaders.StickyListHeadersListView.measureHeader(StickyListHeadersListView.java:242)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at com.emilsjolander.components.stickylistheaders.StickyListHeadersListView.scrollChanged(StickyListHeadersListView.java:284)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at com.emilsjolander.components.stickylistheaders.StickyListHeadersListView.onScroll(StickyListHeadersListView.java:269)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.ListView.layoutChildren(ListView.java:1753)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.View.layout(View.java:14003)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.View.layout(View.java:14003)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.View.layout(View.java:14003)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.View.layout(View.java:14003)
01-13 20:39:21.245: E/AndroidRuntime(4917):     at android.view.ViewGroup.layout(ViewGroup.java:4375)

1 个答案:

答案 0 :(得分:0)

感谢Mark的回应。我想通了,有点儿。我想我的xml中的layout_width和layout_height对于其中一个列表项有些麻烦,改变它们并不起作用。所以我在代码中使用LayoutParams更改了项目的高度和宽度。