Android SuperSLiM 设置 header 以保持整个宽度
Android SuperSLiM set header to keep the whole width
我已经使用 SuperSLiM 成功实现了 GridSLM 视图,以使用 Sticky header 以网格格式显示项目。我面临的主要问题是我无法将 header 设置为占据整个屏幕宽度。
结果看起来像这样。
从屏幕截图中,您可以看到 header 的反应与位于位置 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 的 example application。
该示例不是很好,因为它是作为用于库开发的可视化测试器编写的,仅作为示例提供,而 none 否则可用。
无论如何,您可以设置可配置的 header 边距和 header 显示模式,这应该会让事情更容易理解。
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 布局中为您的项目和 headers.
配置您喜欢的东西
您的 header 布局将如下所示:
<?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"
/>
我已经使用 SuperSLiM 成功实现了 GridSLM 视图,以使用 Sticky header 以网格格式显示项目。我面临的主要问题是我无法将 header 设置为占据整个屏幕宽度。 结果看起来像这样。
从屏幕截图中,您可以看到 header 的反应与位于位置 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 的 example application。
该示例不是很好,因为它是作为用于库开发的可视化测试器编写的,仅作为示例提供,而 none 否则可用。
无论如何,您可以设置可配置的 header 边距和 header 显示模式,这应该会让事情更容易理解。
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 布局中为您的项目和 headers.
配置您喜欢的东西您的 header 布局将如下所示:
<?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"
/>