Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 避免Listview中ImageButton的重复_Android_Android Layout_Android Listview_Android Linearlayout - Fatal编程技术网

Android 避免Listview中ImageButton的重复

Android 避免Listview中ImageButton的重复,android,android-layout,android-listview,android-linearlayout,Android,Android Layout,Android Listview,Android Linearlayout,我是安卓系统的新手&正在开发我的第一个个人日记应用程序。 现在我在Listview中遇到了一个问题,我在一个布局中有一个Imagebutton,我想表现得像一个标题,在另一个布局中有一些TextView,我表示为Listview,它们与数据库绑定。现在的问题是,我从数据库中获得的行数和生成的按钮数一样多,而实际上我并不想要这些。我正在使用列表活动从文本视图生成列表视图 <LinearLayout xmlns:android="http://schemas.android.com/apk/r

我是安卓系统的新手&正在开发我的第一个个人日记应用程序。 现在我在Listview中遇到了一个问题,我在一个布局中有一个Imagebutton,我想表现得像一个标题,在另一个布局中有一些TextView,我表示为Listview,它们与数据库绑定。现在的问题是,我从数据库中获得的行数和生成的按钮数一样多,而实际上我并不想要这些。我正在使用列表活动从文本视图生成列表视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="2dp"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/detail_title_bar_2"
        android:orientation="horizontal"
        android:layout_marginBottom="5dp" >

        <ImageButton
            android:id="@+id/btnadd"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="bottom"
            android:src="@drawable/add" />
    </LinearLayout>

 <LinearLayout
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:background="@drawable/detail_title_bar_2"
            android:orientation="vertical"
            android:paddingRight="10dp" >

            <TextView
                android:id="@+id/txtday"
                style="@style/Title_day"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginBottom="-10dp"
                android:includeFontPadding="false"
                android:verticalSpacing="0px" />

            <TextView
                android:id="@+id/txtmonth"
                style="@style/Title_Month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginBottom="-10dp"
                android:includeFontPadding="false"
                android:verticalSpacing="0px" />

            <TextView
                android:id="@+id/txtyear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right" />

            <TextView
                android:id="@+id/txttime"
                style="@style/Title_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right" />
        </LinearLayout>
</LinearLayout>

我理解了您的问题,您的列表视图中有从数据库加载的条目,您希望在列表顶部有一个按钮,例如添加新项目

然后可以使用addHeaderView()函数。将包含图像按钮的布局定义为xml布局文件,例如list_header_view.xml,并将其添加到listview:

View headerView = (View)inflater.inflate(
            R.layout.list_header_view, null);

listview.addHeaderView(headerView);
list_header_view.xml可以包含:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/detail_title_bar_2"
    android:orientation="horizontal"
    android:layout_marginBottom="5dp" >

    <ImageButton
        android:id="@+id/btnadd"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:src="@drawable/add" />
</LinearLayout>

不要害怕将其作为单独的组件使用。如果您正确使用
ListView
,它不会低效

这里有一些如何使用谷歌IO上的ListView的教程,作者是Romain Guy。它持续了50多分钟,但很值得。

关键部分是重用
ListView
的已创建部分,而不是创建新部分


然后在
getView()
方法中有一些关于查找
视图的有用信息
findViewById()
方法类似于在树中搜索—通过
活动
布局,然后是子项,直到找到您要查找的
视图。这对设备来说可能很困难。因此他们建议使用一个名为
ViewHolder
的静态类,以找到布局的某些部分只保留一次的位置。不是在每次调用
getView()

Wawoo..时都这样。。!!我没想到会这么容易。。谢谢你救了我一个晚上:)