Azbesciak Azbesciak - 2 months ago 6
Java Question

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 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:

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

Answer

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. 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>