首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表视图中的项的不同布局

列表视图中的项的不同布局
EN

Stack Overflow用户
提问于 2011-10-20 22:18:51
回答 3查看 810关注 0票数 1

我有一些扩展的游标适配器,在其中我用列表中项的上下文和资源布局调用super,如下所示。

在我的适配器中调用super:

代码语言:javascript
复制
super(activity, viewResource, c, false);

创建我的适配器:

代码语言:javascript
复制
new MyCursorAdapter(this, null, R.layout.my_list_item, null);

我想要实现的东西就像我用油漆做的愚蠢的模型。换句话说,我想要有不同种类的项目布局,例如,我希望所有偶数项目都有layout1,所有奇数项目都有layout2。到目前为止,在本例中我只能给出一种布局,即R.layout.my_list_item。是否可以动态更改布局?是否可以将适配器构造为具有不同布局的项?我的目标是动态选择项目的布局。我不想只有一个布局的所有项目,我想有为例二…

谢谢

EN

回答 3

Stack Overflow用户

发布于 2011-10-20 22:25:12

是的,你将不得不做两件事。首先,覆盖适配器中的getItemViewType()方法,这样就可以确保bindView()只获取适合列表中特定位置的视图,如下所示:

代码语言:javascript
复制
public int getItemViewType(int position){
  if(correspondsToViewType1(position)){
    return VIEW_TYPE_1;
  }
  else(correspondsToViewType2(position)){
    return VIEW_TYPE_2;
  }
  //and so on and so forth.
}

一旦你这样做了,只需在你的bindView()中做一个简单的测试,看看它应该接收到什么类型的视图,并相应地进行如下设置:

代码语言:javascript
复制
public void bindView(View view, Context context, Cursor cursor){
  if(correspondsToViewType1(cursor)){
    //Now we know view is of a particular type and we can do the 
    //setup for it
  }
  else if(correspondsToViewType2(cursor){
    //Now we know view is of a different type and we can do the 
    //setup for it
  }
}

请注意,您必须为correpondsToViewType使用不同的方法,一个接受游标,另一个接受int (表示位置)。它们的实现将根据您想要做的事情而有所不同。

请注意,这样做将允许您重用可能回收的视图。如果你不这样做,你的应用程序将会受到巨大的性能的影响。滚动将是超级起伏的。

票数 2
EN

Stack Overflow用户

发布于 2011-10-20 22:28:54

我猜您是从自定义适配器的名称开始扩展SimpleCursorAdapter的。您将希望覆盖适配器中的函数getView,并根据列表中的对象膨胀不同的布局并返回该视图。

例如:

代码语言:javascript
复制
     @Override
     public View getView (int position, View convertView, ViewGroup parent)
     {
            Object myObject = myList.get(position);

            if(convertView == null)
            {
                  if( something to determine layout )
                        convertView = inflater.inflate(Layout File);
                  else
                        convertView = inflater.inflate(Some Other Layout File);
            }

            //Set up the view here, such as setting textview text and such

            return convertView;
     }

这只是一个示例,并且是一些sudo代码,因此需要根据您的特定情况进行一些调整。

票数 0
EN

Stack Overflow用户

发布于 2011-10-20 22:29:01

只需覆盖newView方法:

代码语言:javascript
复制
public class MyCursorAdapter extends CursorAdapter {

private final LayoutInflater inflater;
    private ContentType type;

public MyCursorAdapter (Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if( cursor.getString(cursor.getColumnIndex("type")).equals("type1") ) {
                // get elements for type1
    } else {
                // get elements for type1
            }

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    if( cursor.getString(cursor.getColumnIndex("type")).equals("type1") ) {
        final View view = inflater.inflate(R.layout.item_type1, parent, false);
    } else {
        final View view = inflater.inflate(R.layout.item_type2, parent, false);
    }
    return view;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7837332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档