简体   繁体   中英

How can I place a Button below a list, but limit the list's height, if it is too long

I want to have a RecyclerView and an ImageButton below that.

If there are only a few items in the list, the button should just stick to the end of the list. Like in this picture

If the list's content is too long, the button should stick to the end of the site and the scrollable list should be limited to the top of the button. Like in this picture

I tried out many layouts and configurations but I could only achieve:

  1. That the RecyclerView fills the screen and the button stays always at the bottom, even when there are no items in the list.

OR

  1. That the button always sticks to the bottom of the list. If the List is too long, it kicks out the button out of the screen and even further list items are outside of the screen and the list is not scrollable.

How is it possible to get the layout how described in the first part? Is it even possible with xml-only?

Here some code for trying out:

TestActivity.java:
public class TestActivity extends AppCompatActivity {
    private final int NUMBER_OF_ITEMS = 2; // change me!

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        TestAdapter adapter = new TestAdapter();
        recyclerView.setAdapter(adapter);
        ArrayList<Object> list = new ArrayList<>();
        for(int i = 0; i < NUMBER_OF_ITEMS; i++) list.add(null);
        adapter.submitList(list);
    }

    public class TestAdapter extends ListAdapter<Object, TestViewHolder> {
        TestAdapter() {
            super(new DiffUtil.ItemCallback<Object>() {
                @Override
                public boolean areItemsTheSame(Object oldItem, Object newItem) {
                    return oldItem.equals(newItem);
                }
                @Override
                public boolean areContentsTheSame(Object oldItem, Object newItem) {
                    return oldItem.equals(newItem);
                }
            });
        }
        @NonNull
        @Override
        public TestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new TestViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_card, parent, false));
        }
        @Override
        public void onBindViewHolder(@NonNull TestViewHolder holder, int position) {
        }
    }
    class TestViewHolder extends RecyclerView.ViewHolder {
        TestViewHolder(View itemView) {
            super(itemView);
        }
    }
}

and here is the card-layout

activity_card.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:foreground="@color/cardview_dark_background"
    android:layout_margin="16dp">
</android.support.v7.widget.CardView>

and here is the activity-layout (only behaving well, with a full list. Button instead of ImageButton for simplification)

activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<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=".ui.TestActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


<ImageButton
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM