我的问题是,而不是重新发明轶事轮; 我想对 Spinners 和 ListView 使用相同的布局,其中列表将基于相同的数据。
我还希望在整个应用程序中具有相似的自定义外观,例如具有交替行颜色和根据当前核心 Activity () 编码的颜色的列表(微调器和 ListView )。
例如。我的应用程序有商店(核心 Activity ),它在 ListView
(ShopName、ShopCity 和 ShopOrder)中显示为列表,用于此的布局是 R.layout.shoplist
,根据:
我的应用程序也有过道(另一个核心 Activity ,所以颜色不同)。过道列表仅限于其中一个商店,因此合并了一个微调器,列出可用的商店以选择相应的过道。过道列表目前看起来像,没有微调器:-
我知道我可以简单地通过使用 ShopList 适配器为微调器适配器中的微调器指定 ShopLIst Listview 的项目布局(其中 sclcsr 是包含所有商店的 cusror,并且 selectshoplist 是微调器):-
slcsr = dbshopmethods.getShops("", shopsorderby);
selectshoplistadapter = new AdapterShopList(this,
slcsr,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER,
getIntent(),
true,
false
);
selectshoplist.setAdapter(selectshoplistadapter);
注意!与标准适配器相比,有 3 个额外的参数,Intent
被传递(一个 int
被提取用于确定使用哪个颜色范围,第一个 boolean
是一个标志,指示来自 Spinner
的调用,而不是 ListView
,第二个用于指示是否显示额外数据)
然而,结果是:-
那是 Spinner 的下拉列表中缺少定制。
因此现在的问题是我需要做什么来设置下拉菜单的背景颜色?
请注意,我有一个类 (ActionColorCoding) 和方法来确定/应用适配器的 getView
方法中的颜色,按照:-
int evenrow = ActionColorCoding.setHeadingColor(ctxt,
callerintent,
ActionColorCoding.getColorsPerGroup() - 1
) & ActionColorCoding.transparency_evenrow;
int oddrow = evenrow & ActionColorCoding.transparency_oddrow;
if (position % 2 == 0) {
view.setBackgroundColor(evenrow);
} else {
view.setBackgroundColor(oddrow);
}
因此,以上内容将成为要合并到适配器中的代码的基础。 Spinnner 的选择/选定项的自定义不是问题,因为这是根据 Spinner 在 Activity 布局中的声明。
请注意,这个问题的目的是作为一种指南,可以帮助其他人使用似乎没有答案的技术
最佳答案
Spinner 实际上有两个布局与之相关联,第二个布局用于 DropDownView 并且将调用方法 getDropDownView
,如果覆盖 (而 ListViews 调用 getView
方法)。
需要注意的一件事是,如果 getDropDownView
被调用,则 bindView
不会被调用,因此您必须调用它。
通过将以下内容添加到 ListView
适配器,您的适配器将满足 ListView
和 Spinner
的要求:-
@Override
public View getDropDownView(int position, View convertview, ViewGroup parent) {
super.getDropDownView(position, convertview, parent);
View view = convertview;
if (fromspinner) {
int cpos = this.cursor.getPosition();
view = View.inflate(ctxt,R.layout.shoplist,null);
int evenrow = ActionColorCoding.setHeadingColor(ctxt,callerintent, ActionColorCoding.getColorsPerGroup() - 1) & ActionColorCoding.transparency_evenrow;
int oddrow = evenrow & ActionColorCoding.transparency_oddrow;
if (position % 2 == 0) {
view.setBackgroundColor(evenrow);
} else {
view.setBackgroundColor(oddrow);
}
this.cursor.moveToPosition(position);
}
bindView(view, ctxt, this.cursor);
return view;
}
注意!我不认为 **if (fromspinner)**
构造是必需的,但已作为预防措施包含在内。
附加说明,callng super.getDropDownView(position, convertview, parent);
似乎不是必需的,因此可能是最好的说明。
注意! ctxt
、calleritent
、fromspinner
和 cursor
按照 :-
AdapterShopList(Context context,Cursor csr, int flags, Intent intent, boolean fromspinner,boolean showdetails) {
super(context, csr, 0);
ctxt = context;
callerintent = intent;
this.fromspinner = fromspinner;
this.cursor = csr;
setShopOffsets(csr);
}
你得到的结果是:-
关于android - 我可以有效地为微调器和 ListView 使用一种布局吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41080470/