所以我的问题是:当我向Recycler View添加许多项目时,下面的按钮必须添加更多项目,因为他被回收者从屏幕上扯下来而消失。 before add和after。没有按钮:/
这是xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AddActivityFormEvent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="14"
android:id="@+id/dateField"
android:singleLine="true"
android:hint="@string/data"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:drawableLeft="@drawable/ic_today"
android:onClick="showDatePickerDialog"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="time"
android:singleLine="true"
android:hint="@string/czas"
android:id="@+id/timeField"
android:layout_alignTop="@+id/dateField"
android:layout_toRightOf="@+id/dateField"
android:layout_toEndOf="@+id/dateField"
android:drawableLeft="@drawable/ic_time"
android:onClick="showTimePickerDialog"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/relativeLayout"
android:layout_below="@+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/activityRecyclerView"
android:elevation="15dp"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_gravity="bottom"
android:layout_below="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/testButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
顺便问一下,如何获得视图持有者的实际位置?我问,因为当我第一次删除项目时,它返回正确的索引,但每次下一次它都是一次,例如当我删除第一项(索引0)时它返回1 这是一个带有视图持有者的适配器:
public class ActivityFormEventAdapter extends RecyclerView.Adapter<ActivityFormEventAdapter.ActivityFormEventViewHolder>{
ArrayList<ActivityFormEvent> adapter_list = new ArrayList<>();
AddActivityFormEvent addActivityFormEvent;
private static Button button;
Context ctx;
public ActivityFormEventAdapter(ArrayList<ActivityFormEvent> adapter_list,Context ctx){
this.adapter_list=adapter_list;
this.ctx=ctx;
addActivityFormEvent= (AddActivityFormEvent) ctx;
button= (Button) addActivityFormEvent.findViewById(R.id.testButton);
}
public ActivityFormEventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_form_event_card,parent,false);
ActivityFormEventViewHolder activityFormEventViewHolder = new ActivityFormEventViewHolder(view,addActivityFormEvent);
return activityFormEventViewHolder;
}
@Override
public void onBindViewHolder(ActivityFormEventViewHolder holder, final int position) {
holder.activityName.setText(adapter_list.get(position).getActivityForm().getName());
holder.duration.setText(adapter_list.get(position).getTime());
holder.burnedKcalPerHour.setText(adapter_list.get(position).getActivityForm().getBurnedKcalPerHour());
holder.burnedKcal.setText(adapter_list.get(position).getBuriedKcal());
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter_list.remove(position);
notifyItemRemoved(position);
}
});
}
@Override
public int getItemCount() {
return adapter_list.size();
}
public static class ActivityFormEventViewHolder extends RecyclerView.ViewHolder{
AddActivityFormEvent addActivityFormEvent;
EditText activityName;
EditText duration;
EditText burnedKcalPerHour;
EditText burnedKcal;
ImageButton deleteButton;
public ActivityFormEventViewHolder(View itemView,AddActivityFormEvent addActivityFormEvent) {
super(itemView);
activityName = (EditText) itemView.findViewById(R.id.activityName);
duration = (EditText) itemView.findViewById(R.id.duration);
burnedKcalPerHour = (EditText) itemView.findViewById(R.id.burnedKcalPerHour);
burnedKcal = (EditText) itemView.findViewById(R.id.kcalExercise);
deleteButton = (ImageButton) itemView.findViewById(R.id.deleteButton);
this.addActivityFormEvent = addActivityFormEvent;
}
}
}
答案 0 :(得分:1)
好的尝试这个,这里底视图必须有最小高度=连接的Buttom的高度,并且它的高度必须以编程方式设置为所有视图的高度 - recyclerview内容高度。尝试更改底部视图的高度以查看预览中的差异。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"
android:layout_below="@+id/dateField">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/linearLayout"
android:layout_above="@+id/bottom_space_holder"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activityRecyclerView"
android:elevation="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addActivityEvent"
android:text="Dodaj aktywność"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout"
android:layout_alignParentStart="true" />
<View
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="@+id/bottom_space_holder"
android:text="Dodaj aktywność"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
答案 1 :(得分:0)
这是因为您使用Button
将RecyclerView
置于layout_below
下方,RecyclerView
的高度设置为wrap_content
。这意味着,当RecyclerView
的内容超出屏幕高度后,Button
将从屏幕上掉落,因为它位于RecyclerView
下方。
要解决此问题,您应该将Button
与父母的底部对齐(使用layout_alignParentBottom
)并在按钮上方布置RecyclerView
(使用layout_above
和{{ 1}}),这样layout_alignParentTop
将始终放置在屏幕底部并显示。