繁体   English   中英

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

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

我想在其下有一个RecyclerView和一个ImageButton。

如果列表中只有几个项目,则按钮应仅停留在列表的末尾。 就像这张照片

如果列表的内容太长,则按钮应粘贴在站点的末尾,而可滚动列表应限制在按钮的顶部。 就像这张照片

我尝试了许多布局和配置,但只能实现:

  1. 即使列表中没有任何项目,RecyclerView仍会填满屏幕,并且按钮始终位于底部。

要么

  1. 该按钮始终停留在列表的底部。 如果列表太长,则会将按钮从屏幕上踢出,甚至其他列表项都在屏幕之外,并且列表不可滚动。

如何获得第一部分中描述的布局? 仅使用xml甚至可能吗?

这里有一些代码可以尝试:

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);
        }
    }
}

这是卡布局

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>

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