我想在其下放置一个RecyclerView和一个ImageButton。
如果列表中只有几个项目,则按钮应仅停留在列表的末尾。 Like in this picture
如果列表的内容太长,则该按钮应粘贴在站点的末尾,并且可滚动列表应限制在该按钮的顶部。 Like in this picture
我尝试了许多布局和配置,但只能实现:
OR
如何获得第一部分中描述的布局?仅使用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>
答案 0 :(得分:0)
<?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>