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

发布于 2024-12-11 09:46:42 字数 467 浏览 0 评论 0原文

我有一些扩展的光标适配器,我用列表中的项目的上下文和资源布局来调用 super ,类似这样。

在我的适配器中调用 super:

super(activity, viewResource, c, false);

创建我的适配器:

new MyCursorAdapter(this, null, R.layout.my_list_item, null);

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

谢谢

I have some extended cursor adapter, in witch I call super with the context and the resource layout for the item in the list, something like this.

call to super in my adapter:

super(activity, viewResource, c, false);

creation of my adapter:

new MyCursorAdapter(this, null, R.layout.my_list_item, null);

What I want to achieve is something like my stupid mock up made in paint.
Put into words I want to have different kinds of layout for the items, for example I want all even items to have layout1 and all odd to have the layout2. So far I am able to give only one layout in this case R.layout.my_list_item. Is it possible to dynamically change the layout ?
Is it possible to construct the adapter to have items with different layout ? My goal is to dynamically chose the layout of the item. I do not want to have just one layout for all of the items I want to have foe example two...

Thanks

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤君无依 2024-12-18 09:46:42

是的,但你必须做两件事。首先,重写适配器中的 getItemViewType() 方法,以便确保您的 bindView() 仅获取适合列表中特定位置的视图,如下所示:

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() 中进行一个简单的测试,检查它应该收到什么类型的视图,并相应地进行设置,如下所示:

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(用于位置)。这些的实现将根据您想要执行的操作而有所不同。

请注意,以这种方式执行操作将允许您重用可能回收的视图。如果您不这样做,您的应用将会遭受巨大性能损失。滚动会非常不稳定。

Yes, you're going to have to do two things though. First, override the getItemViewType() method in your adapter so that you can be sure your bindView() only get's views that appropriate for a particular position in the list, like so:

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.
}

Once you do that, just have a simple test in your bindView() that checks to see what type of view it should have recieved and setup things accordingly like so:

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
  }
}

Note that you're going to have to have to different methods for correpondsToViewType, one that takes a cursor and one that takes an int (for a position). The implementation for these will vary depending on what you want to do.

Note that doing things this way will allow you to reuse potentially recycled views. If you don't do this, your app is going to take a huge performance hit. Scrolling will be super choppy.

佼人 2024-12-18 09:46:42

我猜测您从自定义适配器的名称中扩展了 SimpleCursorAdapter 。您将需要重写函数 getView 在适配器中,并根据列表中的对象膨胀不同的布局并返回该视图。

EX:

     @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 代码,因此需要根据您的具体情况进行一些调整。

I'm guessing your extending SimpleCursorAdapter from the name of your custom adapter. You will want to override the function getView in your adapter and depending on the object in the list inflate a different layout and return that view.

EX:

     @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;
     }

This is just an example and is somewhat sudo code so it will need some adjustments for your specific situation.

衣神在巴黎 2024-12-18 09:46:42

只需重写 newView 方法:

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;
}

Just override the newView Method:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文