2

У меня есть 2 макета в приложении, которое закрывается панелью инструментов. В каждом макете я пробовал использовать android:layout_below="@id/app_bar_layout или android:layout_below="@id/toolbar, не повезло. Как я могу это исправить?Панель инструментов Android покрывает фрагмент

Вид ресайклера вызывается прикосновением к элементу меню и раздувается с помощью rv adpater и классов классов просмотра. Пока у меня нет других проблем с приложением, кроме панели инструментов, охватывающей виды.

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
    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:fitsSystemWindows="true" 
    tools:context="com.example.aaron.walkingtourtest09feb.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/app_bar_layout" 
    android:theme="@android:style/Theme.Material"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:elevation="4dp" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     android:background="#607D8B"/> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main"/> 

</android.support.design.widget.CoordinatorLayout> 

content_main.xml:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar" 
    tools:context="com.example.test09feb.MainActivity" 
    tools:showIn="@layout/app_bar_main" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<fragment 
    android:id="@+id/listFragment" 
    android:tag="unique_tag" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.example.test09feb.MainActivity$ExampleFragment" > 
</fragment> 

Recyclerview_activity.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/cardview_light_background" 
android:animateLayoutChanges="true" 
android:padding="6dp"> 

<android.support.v7.widget.RecyclerView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/rv"> 
</android.support.v7.widget.RecyclerView> 

cards.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/tools" 
      xmlns:card_view="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 


<android.support.v7.widget.CardView 
    android:id="@+id/cv" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:elevation="4dp" 
    card_view:cardUseCompatPadding="true"> 

    <RelativeLayout 
     android:id="@+id/card_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:animateLayoutChanges="true" 
     android:background="#EEEEEE" 
     android:orientation="vertical" 
     android:padding="0dp"> 

     <ImageView 
      android:id="@+id/subject_photo" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:scaleType="fitCenter"/> 

     <TextView 
      android:id="@+id/subject_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/subject_photo" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:paddingTop="16dp" 
      android:textColor="@android:color/black" 
      android:textSize="16sp"/> 

     <TextView 
      android:id="@+id/subject_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/subject_name" 
      android:paddingBottom="16dp" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:textColor="#777777" 
      /> 

     <Button 
      android:id="@+id/card_button_left" 
      style="@style/Widget.AppCompat.Button.Borderless" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/subject_text" 
      android:layout_toLeftOf="@+id/card_button_right" 
      android:text="@string/watch" 
      /> 

     <Button 
      android:id="@id/card_button_right" 
      style="@style/Widget.AppCompat.Button.Borderless" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@id/subject_text" 
      android:text="@string/read"/> 

     <TextView 
      android:id="@+id/expanded_subject_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/card_button_right" 
      android:paddingBottom="16dp" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" 
      android:textColor="#777777" 
      /> 
    </RelativeLayout> 

</android.support.v7.widget.CardView> 

RVAdapter:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.SubjectViewHolder> { 

List<Subject> subjects; 
static SubjectViewHolder svh; 
View v = null; 
Context cxt; 

private static MyListener listener; 

public interface MyListener { 
    void onClick(View itemView, int viewPosition); 

} 


RVAdapter(List<Subject> subjects, Context cxt) { 
    this.subjects = subjects; 
    this.cxt = cxt; 
} 

public void setOnClickListener(MyListener listener) { 
    this.listener = listener; 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    super.onAttachedToRecyclerView(recyclerView); 
} 

@Override 
public SubjectViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards, viewGroup, false); 
    svh = new SubjectViewHolder(v); 
    return svh; 
} 

String[] localLinks = { 
     "http://192.168.11.111:8000/pic1.png", 
     "http://192.168.11.111:8000/pic2.jpg", 
     "http://192.168.11.111:8000/pic3.png", 
     "http://192.168.11.111:8000/pic4.jpg", 
     "http://192.168.11.111:8000/pic5.png", 
     "http://192.168.11.111:8000/pic6.jpg", 
     "http://192.168.11.111:8000/pic7.png", 
     "http://192.168.11.111:8000/pic8.jpg", 
     "http://192.168.11.111:8000/pic9.png", 
     "http://192.168.11.111:8000/pic10.jpg", 
     "http://192.168.11.111:8000/pic11.png", 
     "http://192.168.11.111:8000/pic12.png", 
     "http://192.168.11.111:8000/pic13.png", 
     "http://192.168.11.111:8000/pic14.png", 
}; 


@Override 
public void onBindViewHolder(SubjectViewHolder subjectViewHolder, int i) { 
    subjectViewHolder.subjectName.setText(subjects.get(i).subjectName); 
    subjectViewHolder.subjectText.setText(subjects.get(i).subjectText); 
    Picasso.with(cxt).load(localLinks[i]).into(subjectViewHolder.subjectPhoto); 
    subjectViewHolder.expandedSubjectText.setText(subjects.get(i).expandedSubjectText); 
    subjectViewHolder.expandedSubjectText.setVisibility(View.GONE); 


} 

@Override 
public long getItemId(int position) { 
    return position; 
} 


@Override 
public int getItemCount() { 
    return subjects.size(); 
} 

public static class SubjectViewHolder extends RecyclerView.ViewHolder { 

    CardView cv; 
    TextView subjectName; 
    TextView subjectText; 
    TextView expandedSubjectText; 
    ImageView subjectPhoto; 
    Button leftButton; 
    Button rightButton; 


    SubjectViewHolder(final View itemView) { 

     super(itemView); 

     cv = (CardView) itemView.findViewById(R.id.cv); 
     subjectName = (TextView) itemView.findViewById(R.id.subject_name); 
     subjectText = (TextView) itemView.findViewById(R.id.subject_text); 
     expandedSubjectText = (TextView) itemView.findViewById(R.id.expanded_subject_text); 
     subjectPhoto = (ImageView) itemView.findViewById(R.id.subject_photo); 
     leftButton = (Button) itemView.findViewById(R.id.card_button_left); 
     rightButton = (Button) itemView.findViewById(R.id.card_button_right); 

     rightButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (expandedSubjectText.getVisibility() == View.GONE) { 
        expandedSubjectText.setVisibility(View.VISIBLE); 
       } else { 
        expandedSubjectText.setVisibility(View.GONE); 
       } 
       // Triggers click upwards to the adapter on click 
       if (listener != null) 
        v.findViewById(R.id.card_container); 
       listener.onClick(rightButton, svh.getAdapterPosition()); 
      } 
     }); 

     leftButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Triggers click upwards to the adapter on click 
       if (listener != null) 
        listener.onClick(leftButton, svh.getAdapterPosition()); 
      } 
     }); 
    } 
}} 

результат добавления appbar_scrolling_view_behavior как это было предложено. Пустой бар покрывает текст, который только что оживил себя.

enter image description here

ответ

7

Вы должны добавить следующий атрибут родительского вида в content_main.xml

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

Как это:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.test09feb.MainActivity" 
    tools:showIn="@layout/app_bar_main" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

</FrameLayout> 
+0

Спасибо. Я забыл упомянуть, что я пробовал этот метод, но он испортил макет карт в моем recyclerview. Каждая карта имеет кнопку «читать», которая делает видимым текстовое изображение. Добавление 'appbar_scrolling_view_behavior' приводит к тому, что видимое текстовое представление закрывается пробелом в нижней части экрана. Я добавлю еще код в свой первоначальный ответ. – user465001

+0

Вам не нужно добавлять 'app: layout_behavior' в макет вашей карты. Только родительский вид в файле 'content_main.xml' – Sharj

+0

Упс - я поставил его в качестве эксперимента. Тем не менее, я добавил строку поведения, в которой вы предлагали, и, по-прежнему, не уменьшает представление. Вид остается затененным панелью инструментов. – user465001

5

Ваша высота панелей инструментов определяется: android:layout_height="?attr/actionBarSize" так Я добавил верхний край с этими значениями в список: android:layout_marginTop="?attr/actionBarSize"

Работал для меня в приложении, над которым я работаю.

Смежные вопросы