【问题标题】:Listview: Headers and custom layout rowsListview:标题和自定义布局行
【发布时间】:2015-12-26 04:42:10
【问题描述】:

在 Android 中,我有一个列表视图,它使用自定义适配器来更改列表视图每一行的外观。但是,我需要找到一种方法来使用和适配器,以便能够为特定的正方形进行多种不同的布局。我也希望能够使用我从未使用过的标题。我希望它在这里看起来像这样的布局:

我怎样才能最好地做到这一点?谢谢!

【问题讨论】:

    标签: android android-layout android-listview android-custom-view android-xml


    【解决方案1】:

    我认为你不需要在这里使用 listview。但是如果你想使用具有不同行布局的列表视图,请在你的适配器中这样做:

    private static final int ROW_TYPE1 = 0;
    private static final int ROW_TYPE2 = 1;
    private static final int ROW_TYPE3 = 2;
    int type;
    

    @Override
    public int getItemViewType(int position) {
    if (position == 0)
        type = ROW_TYPE1;
    else if (position == 1)
        type = ROW_TYPE2;
    else
        type = ROW_TYPE3;
    return type;
    }
    
    @Override
     public int getViewTypeCount() {
        return 3; 
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);
    if (convertView  == null)
    switch (type) {
        case ROW_TYPE1:
            // inflate layout1
            break;
        case ROW_TYPE2:
            // inflate layout2
            break;
        case ROW_TYPE3:
            // inflate layout3
            break;
        default:
            break;
        }
    }
    

    【讨论】:

      【解决方案2】:

      ListView 用于布局,其中包含每行相同布局的对象列表。在你的情况下,我认为你应该设计一个你想要的布局,不要使用 ListView。

      【讨论】:

      • 谢谢!我听取了您的建议,并使用垂直 ScrollView 制作了一个自定义视图
      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多