When using listview/recycleview, we often encounter a demand: a layout is suspended under a ListView, and when the items of ListView exceed the bottom of the screen, they are fixed at the bottom.
At first, I thought of using ListView's footer to realize this function. Later, I found a simple layout to realize this function.
Look at the picture:
Layout code:
It is mainly listview and the bottom layout are linearly distributed, and the reserved space at the bottom does not occupy the display space, so that the listview will have position display when it exceeds the screen
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" > <Button android:id="@+id/bt_sub" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="reduce"/> <Button android:id="@+id/bt_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="plus"/> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:visibility="invisible"> </View> </RelativeLayout>
Test code:
package com.example.wush.listviewfooterapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { //data source private List<String> stringList = new ArrayList<>(); private ListView listView; private Button bt_sub; private Button bt_add; private int index = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { listView = findViewById(R.id.lv); bt_sub = findViewById(R.id.bt_sub); bt_add = findViewById(R.id.bt_add); final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stringList); listView.setAdapter(adapter); //Delete an Item bt_sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (stringList.size() > 0) { stringList.remove(stringList.size()-1); index--; adapter.notifyDataSetChanged(); } } }); //Add an Item bt_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { index++; stringList.add("Incoming:" + index + "Number"); adapter.notifyDataSetChanged(); } }); } }
Thanks.