setViewBinder/setViewValue 和 getView/LayoutInflater 的用法有什么区别?
看起来有两种可能的方法可以更改 ListView
行中的某些内容:
使用
setViewBinder
/setViewValue
:myCursor.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
<预><代码> @Override public boolean setViewValue(View view, Cursor 光标, int columnIndex) { int viewId = view.getId(); 开关(视图ID){ 案例 R.id. 图标: // 在这里更改与图标相关的内容使用
getView/<代码>LayoutInflater:
公共视图 getView(int 位置,视图 ConvertView,ViewGroup 父级){
查看 itemView = null; 如果(convertView == null){ LayoutInflater 充气器 = (LayoutInflater)parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); itemView = inflater.inflate(R.layout.list_row, null); } 别的 { itemView = 转换视图; } ImageView imgViewChecked = (ImageView) itemView .findViewById(R.id.icon); // 在这里更改与图标相关的内容
这两种方法有什么区别?
Looks like there are two possible ways to change something in the ListView
rows:
using of
setViewBinder
/setViewValue
:myCursor.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { int viewId = view.getId(); switch(viewId) { case R.id.icon: // change something related to the icon here
using of
getView
/LayoutInflater
:public View getView(int position, View convertView, ViewGroup parent) {
View itemView = null; if (convertView == null) { LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); itemView = inflater.inflate(R.layout.list_row, null); } else { itemView = convertView; } ImageView imgViewChecked = (ImageView) itemView .findViewById(R.id.icon); // change something related to the icon here
What is the difference between these two approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用它们来完成相同的任务。
ViewBinder 系统是由 SimpleCursorAdapter 添加的,使您的事情变得更容易,因此您不必编写整个 getView 代码。事实上,SimpleCursorAdapter 只是通过调用 setViewValue 方法(以及标准样板错误检查和膨胀)来实现 getView
我已经附上了 Android 源代码在 SimpleCursorAdapter 中用于 getView 的实现:
You can use both of them to accomplish the same task.
The ViewBinder system is added by SimpleCursorAdapter to make things easier for you, so you don't have to write the entire getView code. In fact, SimpleCursorAdapter just implements getView by calling the setViewValue method (along with the standard boilerplate error checking and inflating)
I've attached the implementation that the Android source code uses for getView in SimpleCursorAdapter: