首页 文章

Android ListView,每行有不同的布局

提问于
浏览
327

我试图确定一个包含每行不同布局的单个ListView的最佳方法 . 我知道如何创建自定义行自定义数组适配器以支持整个列表视图的自定义行,但是如何在ListView中实现许多不同的行样式?

6 回答

  • 62

    我知道如何创建自定义行自定义数组适配器以支持整个列表视图的自定义行 . 但是listview如何支持许多不同的行样式?

    你已经了解了基础知识 . 您只需要让自定义适配器根据提供的行/光标信息返回不同的布局/视图 .

    ListView 可以支持多行样式,因为它派生自AdapterView

    AdapterView是一个视图,其子项由适配器确定 .

    如果查看Adapter,您将看到考虑使用特定于行的视图的方法:

    abstract int getViewTypeCount()
    // Returns the number of types of Views that will be created ...
    
    abstract int getItemViewType(int position)
    // Get the type of View that will be created ...
    
    abstract View getView(int position, View convertView, ViewGroup parent)
    // Get a View that displays the data ...
    

    后两种方法 provide the position 所以你可以使用 determine the type of view 你应该使用 for that row .


    当然,您通常不直接使用AdapterView和Adapter,而是使用或派生自其子类之一 . Adapter的子类可以添加其他功能,以更改如何获取不同行的自定义布局 . Since the view used for a given row is driven by the adapter, the trick is to get the adapter to return the desired view for a given row. 如何执行此操作因具体适配器而异 .

    例如,要使用 ArrayAdapter

    • 覆盖 getView() 以膨胀,填充和返回给定位置的所需视图 . getView() 方法包括通过 convertView 参数的机会重用视图 .

    但要使用 CursorAdapter 的衍生物,

    • 覆盖 newView() 以膨胀,填充并返回当前光标状态的所需视图(即当前"row")[您还需要覆盖 bindView 以便窗口小部件可以重用视图]

    但是,要使用 SimpleCursorAdapter

    • 使用 setViewValue() 方法定义 SimpleCursorAdapter.ViewBinder 以对给定行(当前光标状态)和数据"column"进行充气,填充和返回所需视图 . 该方法可以仅定义"special"视图,并遵循SimpleCursorAdapter的"normal"绑定的标准行为 .

    查找最终使用的适配器类型的具体示例/教程 .

  • 409

    ListView适用于简单的用例,例如所有行项的相同静态视图 .
    由于您必须创建ViewHolders并大量使用 getItemViewType() ,并动态显示不同的行项目布局xml,您应该尝试使用Android API 22中提供的RecyclerView . 它为多种视图类型提供了更好的支持和结构 .

    看看这个tutorial如何使用RecyclerView来做你想要的 .

  • 4

    在您的自定义数组适配器中,您可以覆盖getView()方法,就像您可能熟悉的那样 . 然后,您所要做的就是使用switch语句或if语句返回某个自定义View,具体取决于传递给getView方法的position参数 . Android非常聪明,因为它只会为您的位置/行提供相应类型的convertView;你不需要检查它是否是正确的类型 . 您可以通过适当地覆盖getItemViewType()和getViewTypeCount()方法来帮助Android .

  • 14

    看看下面的代码 .

    首先,我们创建自定义布局 . 在这种情况下,有四种类型 .

    even.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff500000"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text"
            android:textColor="@android:color/white"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:textSize="24sp"
            android:layout_height="wrap_content" />
    
     </LinearLayout>
    

    odd.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff001f50"
        android:gravity="right"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:textSize="28sp"
            android:layout_height="wrap_content"  />
    
     </LinearLayout>
    

    white.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ffffffff"
        android:gravity="right"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:textSize="28sp"
            android:layout_height="wrap_content"   />
    
     </LinearLayout>
    

    black.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff000000"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/text"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:textSize="33sp"
            android:layout_height="wrap_content"   />
    
     </LinearLayout>
    

    然后,我们创建listview项 . 在我们的例子中,使用字符串和类型 .

    public class ListViewItem {
            private String text;
            private int type;
    
            public ListViewItem(String text, int type) {
                this.text = text;
                this.type = type;
            }
    
            public String getText() {
                return text;
            }
    
            public void setText(String text) {
                this.text = text;
            }
    
            public int getType() {
                return type;
            }
    
            public void setType(int type) {
                this.type = type;
            }
    
        }
    

    之后,我们创建了一个视图持有者 . 强烈建议使用Android操作系统,因为Android操作系统会保留布局参考,以便在项目消失后再次显示在屏幕上 . 如果您不使用此方法,则每次您的项目出现在屏幕上Android操作系统都会创建一个新项目并导致您的应用程序泄漏内存 .

    public class ViewHolder {
            TextView text;
    
            public ViewHolder(TextView text) {
                this.text = text;
            }
    
            public TextView getText() {
                return text;
            }
    
            public void setText(TextView text) {
                this.text = text;
            }
    
        }
    

    最后,我们创建自定义适配器,覆盖getViewTypeCount()和getItemViewType(int position) .

    public class CustomAdapter extends ArrayAdapter {
    
            public static final int TYPE_ODD = 0;
            public static final int TYPE_EVEN = 1;
            public static final int TYPE_WHITE = 2;
            public static final int TYPE_BLACK = 3;
    
            private ListViewItem[] objects;
    
            @Override
            public int getViewTypeCount() {
                return 4;
            }
    
            @Override
            public int getItemViewType(int position) {
                return objects[position].getType();
            }
    
            public CustomAdapter(Context context, int resource, ListViewItem[] objects) {
                super(context, resource, objects);
                this.objects = objects;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                ViewHolder viewHolder = null;
                ListViewItem listViewItem = objects[position];
                int listViewItemType = getItemViewType(position);
    
    
                if (convertView == null) {
    
                    if (listViewItemType == TYPE_EVEN) {
                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_even, null);
                    } else if (listViewItemType == TYPE_ODD) {
                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_odd, null);
                    } else if (listViewItemType == TYPE_WHITE) {
                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_white, null);
                    } else {
                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_black, null);
                    }
    
                    TextView textView = (TextView) convertView.findViewById(R.id.text);
                    viewHolder = new ViewHolder(textView);
    
                    convertView.setTag(viewHolder);
    
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
    
                viewHolder.getText().setText(listViewItem.getText());
    
                return convertView;
            }
    
        }
    

    我们的活动是这样的:

    private ListView listView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main); // here, you can create a single layout with a listview
    
            listView = (ListView) findViewById(R.id.listview);
    
            final ListViewItem[] items = new ListViewItem[40];
    
            for (int i = 0; i < items.length; i++) {
                if (i == 4) {
                    items[i] = new ListViewItem("White " + i, CustomAdapter.TYPE_WHITE);
                } else if (i == 9) {
                    items[i] = new ListViewItem("Black " + i, CustomAdapter.TYPE_BLACK);
                } else if (i % 2 == 0) {
                    items[i] = new ListViewItem("EVEN " + i, CustomAdapter.TYPE_EVEN);
                } else {
                    items[i] = new ListViewItem("ODD " + i, CustomAdapter.TYPE_ODD);
                }
            }
    
            CustomAdapter customAdapter = new CustomAdapter(this, R.id.text, items);
            listView.setAdapter(customAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                    Toast.makeText(getBaseContext(), items[i].getText(), Toast.LENGTH_SHORT).show();
                }
            });
    
        }
    }
    

    现在在mainactivity.xml中创建一个listview,就像这样

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.example.shivnandan.gygy.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_main" />
    
        <ListView
            android:layout_width="match_parent"
    
            android:layout_height="match_parent"
    
            android:id="@+id/listView"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
    
    
            android:layout_marginTop="100dp" />
    
    </android.support.design.widget.CoordinatorLayout>
    
  • 41

    如果我们需要在列表视图中显示不同类型的视图,那么在适配器中使用getViewTypeCount()和getItemViewType()而不是切换视图VIEW.GONE和VIEW.VISIBLE在getView()中可能是非常昂贵的任务影响列表滚动 .

    请检查这一个是否在Adapter中使用getViewTypeCount()和getItemViewType() .

    链接:the-use-of-getviewtypecount

  • 1

    既然您知道有多少种类型的布局 - 可以使用这些方法 .

    getViewTypeCount() - 此方法返回有关列表中有多少行类型的信息

    getItemViewType(int position) - 根据位置返回您应使用的布局类型的信息

    然后只有在布局为null时才会对布局进行膨胀,并使用 getItemViewType 确定类型 .

    请查看 this tutorial 以获取更多信息 .

    为了实现您在评论中描述的结构优化,我建议:

    • 在名为 ViewHolder 的对象中存储视图 . 它会提高速度,因为您不必每次都在 getView 方法中调用 findViewById() . 见List14 in API demos .

    • 创建一个符合所有属性组合的通用布局,如果当前位置没有,则隐藏一些元素 .

    我希望这会对你有所帮助 . 如果您可以提供一些XML存根,其中包含您的数据结构和信息,您希望如何将其映射到行中,我将能够为您提供更精确的建议 . 按像素 .

相关问题