Как закрепить элемент внизу экрана
Пишу приложение придерживаясь Single-Activity Architecture, управление фрагментами осуществляю с помощью навигационного графика. На данный момент столкнулась с проблемой фиксации view в нижней части экрана, так как фрагменты вставляются в NestedScrollView.
Что я имею:
Что мне нужно: всё тоже самое, но EditText ("Добавить комментарий") с кнопкой должны быть фиксированы и всегда видны.
XML для вставки фрагментов выглядит следующим образом:
<androidx.core.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/scroll_content"
android:fillViewport="true">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/navHost"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </androidx.core.widget.NestedScrollView>
Разметка для содержимого:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutViewComment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Layout для Изображения -->
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Views для описания изображения -->
</androidx.constraintlayout.widget.ConstraintLayout>
<!--Часть с RecyclerView и EditText-->
<include layout="@layout/comments_view"
android:id="@+id/comment_include"
android:visibility="gone"/>
XML для списка комментариев и ввода своего комментария:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Заголовок Title -->
<TextView
style="@style/title"
android:id="@+id/title_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/comment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!--Layout for comments-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_comment"
app:layout_constraintBottom_toTopOf="@+id/line_comment"
/>
<!-- view line-->
<View
android:id="@+id/line_comment"
android:layout_width="match_parent"
android:layout_height="1dp"
style="@style/line_blue"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/layout_toleave_comment"/>
<!-- leave a comment-->
<LinearLayout
android:id="@+id/layout_toleave_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="?attr/actionBarSize"
android:orientation="horizontal"
>
<EditText
android:id="@+id/editTextComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:hint="@string/add_comment"
android:layout_weight="1"/>
<ImageView
style="@style/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_send"
android:id="@+id/sendButton"/>
</LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
Что я пыталась сделать:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/comments"
android:layout_width="match_parent"
android:layout_height="0dp"/>
Но в таком случае комментарии "запираются" между картинкой и EditText:
Было много и других попыток, но ничего существенного не получалось, понимаю, что основная проблема именно в том, что фрагмент находится внутри прокрутки. Буду рада любой помощи.
Ответы (1 шт):
Решение оказалось довольно простым.
Так как в фиксации нуждалась только нижняя часть фрагмента, а изображение нуждалось в прокрутке как и RecylerView, было решено добавить верхнюю часть в качестве Header к RecyclerView. Для реализации понадобилось воспользоваться getItemViewType и немного подправить адаптер:
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
ArrayList<Model> mModels;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public Adapter(Context context, ArrayList<Model> models) {
mContext = context;
mModels = models;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
if(viewType ==TYPE_ITEM)
{
// Here Inflating your recyclerview item layout
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View post = inflater.inflate(R.layout.sample, parent, false);
Adapter.CViewHolder cViewHolder = new Adapter.CViewHolder(post);
return cViewHolder;
} else if(viewType ==TYPE_HEADER)
{
// Here Inflating your header view
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false);
return new HeaderViewHolder(itemView);
}
else return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder){
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
// You have to set your header items
}
else if (holder instanceof CViewHolder){
final CViewHolder itemHolder = (CViewHolder) holder;
// Following code give a row of header and decrease the one position from listview items
Model model = mModels.get(position-1);
// You have to set your listview items
}
}
@Override
public int getItemCount() {
return mModels.size()+1;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
public class CViewHolder extends RecyclerView.ViewHolder {
TextView one, two, three;
public CViewHolder(@NonNull View itemView) {
super(itemView);
one = itemView.findViewById(R.id.one);
two = itemView.findViewById(R.id.two);
three = itemView.findViewById(R.id.three);
}
}
public class HeaderViewHolder extends RecyclerView.ViewHolder {
ImageView photo;
public HeaderViewHolder(@NonNull View itemView) {
super(itemView);
photo = itemView.findViewById(R.id.content_photo);
}
}
В разметке самого RecyclerView не забыть установить android:layout_height="0dp"
Надеюсь это сможет помочь кому-нибудь.)


