我已经使用SuperSLiM成功实现了一个GridSLM视图,以使用Sticky标头以网格格式显示项目。我面临的主要问题是我无法设置标题以占据屏幕的整个宽度。 结果看起来像这样。
从屏幕截图中可以看到标题作为位于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);
}
MyAdapter
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的example application。
答案 0 :(得分: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"
/>