1

我已经使用 SuperSLiM 成功实现了一个 GridSLM 视图,以显示带有粘性标题的网格格式的项目。我面临的主要问题是我无法将标题设置为占据屏幕的整个宽度。结果看起来像这样。

在此处输入图像描述

从屏幕截图中,您可以看到标题作为位于位置 0 的普通 GridSLM 项目做出反应,但没有占据整个宽度。

onViewCreated()对于主要片段

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (savedInstanceState != null) {
        mHeaderDisplay = savedInstanceState
                .getInt(KEY_HEADER_POSITIONING,
                        getResources().getInteger(R.integer.default_header_display));
        mAreMarginsFixed = savedInstanceState
                .getBoolean(KEY_MARGINS_FIXED,
                        getResources().getBoolean(R.bool.default_margins_fixed));
    } else {
        mHeaderDisplay = getResources().getInteger(R.integer.default_header_display);
        mAreMarginsFixed = getResources().getBoolean(R.bool.default_margins_fixed);
    }

    mViews = new ViewHolder(view);
    mViews.initViews(new LayoutManager(getActivity()));
    serviceListDatums = serviceList.getServicesList();
    mAdapter = new MyAdapter(getActivity(), mHeaderDisplay, serviceListDatums);
    mAdapter.setMarginsFixed(mAreMarginsFixed);
    mAdapter.setHeaderDisplay(mHeaderDisplay);
    mViews.setAdapter(mAdapter);
}

我的适配器

 public MyAdapter(Context context, int headerMode, List<ServicesList> items) {
        mContext = context;
        mHeaderDisplay = headerMode;
        mItems = new ArrayList<>();
        Items = items;

        //Insert headers into list of items.
        int sectionManager = -1;
        int sectionFirstPosition = 0;

        for (int i = 0; i < Items.size(); i++) {
            ServicesList m = Items.get(i);
            if (i == 0) {
                String header = "New Releases";
                mItems.add(new LineItem(header, "", true, sectionManager, sectionFirstPosition));
            }
            mItems.add(new LineItem(m.getServiceName(), m.getIcon(), false, sectionManager, sectionFirstPosition));
        }
    }

    @Override
    public CountryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_HEADER) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.header_item, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.text_line_item, parent, false);
        }
        return new CountryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CountryViewHolder holder, int position) {
        final LineItem item = mItems.get(position);
        final View itemView = holder.itemView;

        holder.bindItem(item.text);
        if (!item.isHeader) {
            holder.bindImage(mContext, item.path);
        }

        final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams());
        // Overrides xml attrs, could use different layouts too.
        if (item.isHeader) {
            lp.headerDisplay = mHeaderDisplay;
            if (lp.isHeaderInline() || (mMarginsFixed && !lp.isHeaderOverlay())) {
                lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            } else {
                lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            }

            lp.headerEndMarginIsAuto = !mMarginsFixed;
            lp.headerStartMarginIsAuto = !mMarginsFixed;
        }
        lp.setSlm(item.sectionManager == LINEAR ? LinearSLM.ID : GridSLM.ID);

        lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width));
        lp.setFirstPosition(item.sectionFirstPosition);
        itemView.setLayoutParams(lp);
    }

    public void setHeaderDisplay(int headerDisplay) {
        mHeaderDisplay = headerDisplay;
        notifyHeaderChanges();
    }

    public void setMarginsFixed(boolean marginsFixed) {
        mMarginsFixed = marginsFixed;
        notifyHeaderChanges();
    }

任何形式的帮助将不胜感激。

我正在使用 SuperSLiM 的示例应用程序

4

1 回答 1

5

该示例不是很好,因为它是作为库开发的可视化测试器编写的,并且仅作为示例提供,而没有其他可用的示例。

无论如何,您可以抛出可配置的页眉边距和页眉显示模式,它应该让事情更容易理解。

public MyAdapter(Context context, List<ServicesList> items) {
    mContext = context;
    mItems = new ArrayList<>();
    Items = items;

    //Insert headers into list of items.
    int sectionManager = -1;
    int sectionFirstPosition = 0;

    for (int i = 0; i < Items.size(); i++) {
        ServicesList m = Items.get(i);
        if (i == 0) {
            String header = "New Releases";
            mItems.add(new LineItem(header, "", true, sectionManager, sectionFirstPosition));
        }
        mItems.add(new LineItem(m.getServiceName(), m.getIcon(), false, sectionManager, sectionFirstPosition));
    }
}

@Override
public CountryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == VIEW_TYPE_HEADER) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.header_item, parent, false);
    } else {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.text_line_item, parent, false);
    }
    return new CountryViewHolder(view);
}

@Override
public void onBindViewHolder(CountryViewHolder holder, int position) {
    final LineItem item = mItems.get(position);
    final View itemView = holder.itemView;

    holder.bindItem(item.text);

    final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams());
    if (!item.isHeader) {
        holder.bindImage(mContext, item.path);
    }
    lp.setSlm(GridSLM.ID);
lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width));
    lp.setFirstPosition(item.sectionFirstPosition);
    itemView.setLayoutParams(lp);
}

如您所见,我已经删除了适配器中布局参数的大部分额外配置。相反,您应该在 xml 布局中为您的项目和标题配置您喜欢的东西。

您的标题布局将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/text"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:slm_headerDisplay="inline|sticky"
    app:slm_isHeader="true"
    />
于 2015-07-24T13:08:53.973 回答