【问题标题】:Different layout styles for each row in android custom listview [duplicate]android自定义列表视图中每一行的不同布局样式[重复]
【发布时间】:2013-07-04 10:13:44
【问题描述】:

我知道如何在 android 中创建自定义列表视图。但就像在 iPhone 应用程序开发中一样,我们可以为每一行设置多个不同的 uicellview 样式。如果是的话,在android中是否有可能我们如何实现这一点。我们将不胜感激您的帮助

【问题讨论】:

标签: android listview


【解决方案1】:

在 getView 方法中使用项目的位置。

伪:

public View getView(int position, View recycledView, ViewGroup group) {
   View view;
   if(position == 1){
        view = View.inflate(context, R.layout.view1, null);
   } else {
        view = View.inflate(context, R.layout.view2, null);
   }

  // populate the view

return view;
}

参考: http://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)

http://developer.android.com/reference/android/view/View.html#inflate(android.content.Context, int, android.view.ViewGroup)

【讨论】:

    【解决方案2】:

    您可以通过膨胀多个 xml 布局文件并根据条件返回它们来实现这一点。

    例如在适配器的 getview() 方法中

    你可以这样做

    for condition 1 
    
    inflate layout 1 and return view
    
    for condition 2 
    
    inflate layout 2 and return view.
    

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      相关资源
      最近更新 更多