我已经使用 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 的示例应用程序。