简体   繁体   English

如何将按钮放在列表下方,但是如果列表太长则限制列表的高度

[英]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. 我想在其下有一个RecyclerView和一个ImageButton。

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. 即使列表中没有任何项目,RecyclerView仍会填满屏幕,并且按钮始终位于底部。

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? 仅使用xml甚至可能吗?

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) 这是活动布局(行为举止不错,有完整列表。为简化起见,按钮而不是ImageButton)

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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