[英]pushed off by the recycler view button and notifyItemRemoved
So my problem is: when I add many items to the Recycler View, the button below which has to add more items, dissapear because he is pusshed off from the screen by the recycler. 所以我的问题是:当我在“回收站视图”中添加许多项目时,下面的按钮必须添加更多项目,这消失了,因为回收站将他从屏幕上弹出了。 before add and after .
在添加之前和之后 。 There is no button :/
没有按钮:/
here is the xml code: 这是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>
By the way, how to get actual position of the view holder? 顺便问一下,如何获取视图持有者的实际位置? I ask because when I delete item first time, it return correct index, but each next time it is one more, for example when I delete first item(index 0) it returns 1 here is an adapter with view holder:
我问是因为第一次删除项目时,它返回正确的索引,但是每次下一次它都返回一个,例如,当我删除第一个项目(索引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;
}
}
}
Okay try this, here bottom view must have minimum height = height of Buttom that attached and it's height must be programmatically set to height of all view - recyclerview content height. 好的,尝试一下,这里的底部视图必须具有最小高度=附加的Buttom高度,并且必须通过编程将其高度设置为所有视图的高度-recyclerview内容高度。 Try to change height of bottom view to see the difference in preview.
尝试更改底视图的高度以查看预览中的差异。
<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>
This happens because you place the Button
below the RecyclerView
using layout_below
and the RecyclerView
's height is set to wrap_content
. 发生这种情况是因为您使用
layout_below
将Button
放置在RecyclerView
下方,并且RecyclerView
的高度设置为wrap_content
。 This means that once the RecyclerView
's content exceeds the screens height, the Button
will fall off the screen, because it is placed below the RecyclerView
. 这意味着一旦
RecyclerView
的内容超过屏幕高度,该Button
就会从屏幕上掉下来,因为它位于RecyclerView
下方。
To fix this issue, you should align your Button
to the parent's bottom (using layout_alignParentBottom
) and layout your RecyclerView
above the button (using layout_above
and layout_alignParentTop
), this way the Button
will always be placed and visible at the bottom of your screen. 要解决此问题,您应该将
Button
与父对象的底部对齐(使用layout_alignParentBottom
),然后将RecyclerView
布置在按钮上方(使用layout_above
和layout_alignParentTop
),这样Button
将始终放置在屏幕底部并在屏幕底部可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.