我正在尝试在listview中添加粘性标题。我在https://github.com/beworker/pinned-section-listview找到的代码的帮助下实现了这一点
下面是我在其中获取listview行的图片

它工作得很好,但我需要自定义上面的行。单行有两个单独的布局,分别是。)listheader.xml b.)这两个listrow.xml都是列表视图单行中的两个独立部分。
现在我想做的是列表行标题应该是透明的背景(listheader.xml),listrow.xml不应该有一个携带相机的人的背景图像。图像应设置为每个listview行的背景。它将如下所示:

因此,我的listview行将有一个背景图像和一个标题,不在图像上方,而是在图像上方,如您在上图中所看到的。有没有人能告诉我怎么做。下面是我使用的适配器代码。
public abstract class SectionAdapter extends BaseAdapter implements
OnItemClickListener {
private int mCount = -1;
Context context;
public SectionAdapter(Context c) {
this.context = c;
// TODO Auto-generated constructor stub
}
public abstract int numberOfSections();
public abstract int numberOfRows(int section);
public abstract View getRowView(int section, int row, View convertView,
ViewGroup parent);
public abstract Object getRowItem(int section, int row);
public boolean hasSectionHeaderView(int section) {
return false;
}
public View getSectionHeaderView(int section, View convertView,
ViewGroup parent) {
return null;
}
public Object getSectionHeaderItem(int section) {
return null;
}
public int getRowViewTypeCount() {
return 1;
}
public int getSectionHeaderViewTypeCount() {
return 0;
}
/**
* Must return a value between 0 and getRowViewTypeCount() (excluded)
*/
public int getRowItemViewType(int section, int row) {
return 0;
}
/**
* Must return a value between 0 and getSectionHeaderViewTypeCount()
* (excluded, if > 0)
*/
public int getSectionHeaderItemViewType(int section) {
return 0;
}
@Override
/**
* Dispatched to call onRowItemClick
*/
public final void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
onRowItemClick(parent, view, getSection(position),
getRowInSection(position), id);
}
public void onRowItemClick(AdapterView<?> parent, View view, int section,
int row, long id) {
}
@Override
/**
* Counts the amount of cells = headers + rows
*/
public final int getCount() {
if (mCount < 0) {
mCount = numberOfCellsBeforeSection(numberOfSections());
}
return mCount;
}
@Override
public boolean isEmpty() {
return getCount() == 0;
}
@Override
/**
* Dispatched to call getRowItem or getSectionHeaderItem
*/
public final Object getItem(int position) {
int section = getSection(position);
if (isSectionHeader(position)) {
if (hasSectionHeaderView(section)) {
return getSectionHeaderItem(section);
}
return null;
}
return getRowItem(section, getRowInSection(position));
}
@Override
public long getItemId(int position) {
return position;
}
@Override
/**
* Dispatched to call getRowView or getSectionHeaderView
*/
public final View getView(int position, View convertView, ViewGroup parent) {
/*
* RelativeLayout rl = new RelativeLayout(context); LayoutParams
* listParams = new LayoutParams(LayoutParams.MATCH_PARENT,
* LayoutParams.MATCH_PARENT); rl.setLayoutParams(listParams);
* convertView = (View)rl;
*/
int section = getSection(position);
if (isSectionHeader(position)) {
if (hasSectionHeaderView(section)) {
return getSectionHeaderView(section, convertView, parent);
}
return null;
}
return getRowView(section, getRowInSection(position), convertView,
parent);
// return convertView;
}
/**
* Returns the section number of the indicated cell
*/
protected int getSection(int position) {
int section = 0;
while (numberOfCellsBeforeSection(section) <= position) {
section++;
}
return section - 1;
}
/**
* Returns the row index of the indicated cell Should not be call with
* positions directing to section headers
*/
protected int getRowInSection(int position) {
int section = getSection(position);
int row = position - numberOfCellsBeforeSection(section);
if (hasSectionHeaderView(section)) {
return row - 1;
} else {
return row;
}
}
/**
* Returns true if the cell at this index is a section header
*/
protected boolean isSectionHeader(int position) {
int section = getSection(position);
return hasSectionHeaderView(section)
&& numberOfCellsBeforeSection(section) == position;
}
/**
* Returns the number of cells (= headers + rows) before the indicated
* section
*/
protected int numberOfCellsBeforeSection(int section) {
int count = 0;
for (int i = 0; i < Math.min(numberOfSections(), section); i++) {
if (hasSectionHeaderView(i)) {
count += 1;
}
count += numberOfRows(i);
}
return count;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mCount = numberOfCellsBeforeSection(numberOfSections());
}
@Override
public void notifyDataSetInvalidated() {
super.notifyDataSetInvalidated();
mCount = numberOfCellsBeforeSection(numberOfSections());
}
@Override
/**
* Dispatched to call getRowItemViewType or getSectionHeaderItemViewType
*/
public final int getItemViewType(int position) {
int section = getSection(position);
if (isSectionHeader(position)) {
return getRowViewTypeCount()
+ getSectionHeaderItemViewType(section);
} else {
return getRowItemViewType(section, getRowInSection(position));
}
}
@Override
/**
* Dispatched to call getRowViewTypeCount and getSectionHeaderViewTypeCount
*/
public final int getViewTypeCount() {
return getRowViewTypeCount() + getSectionHeaderViewTypeCount();
}
@Override
/**
* By default, disables section headers
*/
public boolean isEnabled(int position) {
return !isSectionHeader(position);
}
}发布于 2014-06-15 06:03:55
Problem
解决方案
listheader.xml放在listView之上,你应该在你的main activity XML中这样做:http://schemas.android.com/apk/res/android“xmlns:tools="http://schemas.android.com/tools”android:layout_width="match_parent“android:layout_height="match_parent”>
background设置为列表视图items,请执行以下操作:公共最终视图getView(int position,View convertView,ViewGroup parent) { /** *我没有时间研究你的代码关于这里发生了什么,*但我知道你应该在这里实现的可能的解决方案:) *我将在这里发布将背景*放到listView行的代码,如你的图像中所示*。因此,您在这里为与上图中相同的*行设置了条件* */ if( /*将您的条件放在此处,以便该行与图像中的条件相同*/ ){ LayoutInflater inflater = (LayoutInflater) null rowView = inflater.inflate(R.layout.listrow,null);//这是您的listrow.xml //我猜您的xml的根元素是相对布局RelativeLayout rLayout = (RelativeLayout) rowView .findViewById (R.id.rLayout);Resources res = contetx.getResources();//资源句柄Drawable drawable = res.getDrawable(R.drawable.newImage);//您可以根据行位置或任何条件为rLayout.setBackgroundDrawable(drawable);} }
发布于 2014-06-20 07:01:27
我认为你需要添加另一个布局文件,比如带有RelativeLayout的listrowitem.xml,你将把你的listheader.xml放在listrow.xml之上。在此之后,您可以使您的listheader具有@null背景。
https://stackoverflow.com/questions/23782773
复制相似问题