Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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
Java 将文本视图显示为列表视图的最后一项_Java_Android - Fatal编程技术网

Java 将文本视图显示为列表视图的最后一项

Java 将文本视图显示为列表视图的最后一项,java,android,Java,Android,我想将文本视图显示为列表视图的最后一项。这是我的xml代码- <LinearLayout 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_

我想将文本视图显示为列表视图的最后一项。这是我的xml代码-

<LinearLayout
    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:orientation="vertical"
    tools:context="com.activity.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="8dp">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingBottom="60dp" />

        </android.support.v4.widget.SwipeRefreshLayout>

    </FrameLayout>
</LinearLayout>

列表视图将显示一个接一个的卡片视图。但是,我想要一个文本视图作为列表中的最后一项。 例如,如果我有一个卡片视图,我需要在其下方显示textview。 如果我有两个卡片视图,我需要在第二个卡片视图下面显示textview


如何实现这一点?

这可以通过向列表视图添加页脚来解决

这很简单,只需使用所需的Textview创建一个布局。将其作为页脚添加到列表视图中,如下所示

listView.addFooterView(footerView);

请在此查找有关添加页脚的更多参考

您可以将ListView包装为LinearLayout,并在其下方添加TextView。然后用NestedScrollView包装它,并将fillViewPort设置为true,以便在TextView超出屏幕时仍然能够滚动

<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="match_parent"
    android:orientation="vertical"
    tools:context="com.activity.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="8dp">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <ListView
                        android:id="@android:id/list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:clipToPadding="false"
                        android:paddingBottom="60dp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Footer text view" />

                </LinearLayout>

            </android.support.v4.widget.NestedScrollView>

        </android.support.v4.widget.SwipeRefreshLayout>

    </FrameLayout>

</LinearLayout>

像这样膨胀你的视野

     LayoutInflater layoutInflater = (LayoutInflater) 
     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     View root = layoutInflater.inflate(R.layout.yourLayout, null);
     // for footerclick
     //add code her root.setOnClickListner(...)
     listView.addFooterView(root, null, true);
请点击试试这个

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
          if(position < adapter.getCount()){ 
            Log.d("test","working");
          }
     }
 });

显示您的活动或片段代码请listview.addfooter为此,您需要将footer视图添加到listview。如果我这样做,我会得到异常-java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams当我这样做时,我得到异常-java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams要工作谢谢:)但当我单击列表视图中的第二项时,它并没有发生单击事件。我需要第二次点击它来完成一些活动。谢谢,有些问题可以解决:)但问题是当我点击列表视图的第二项或第三项或第四项时,它无法识别。需要点击两次。我得到java.lang.IndexOutOfBoundsException:Index:2,大小:2,用于adapter.getItem()我更改代码签出代码addFotterview(root,bull,false);还是同样的问题:(当我这样做时,我得到异常-java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams。)
mListView.removeFooterView(root);