2016-06-28 2 views
3

Я с помощью CoordinatorLayout с AppBarLayout внутри и элемента, чтобы попытаться, чтобы иметь возможность прокрутки при RecyclerView в настоящее время прокручивается. До этого все работает так, как это принято, проблемы возникают, когда я пытаюсь программно прокручиватьToPosition последних позиций RecyclerView. recyclerViews делает прокрутку, но позиция, которую я говорю для прокрутки, не видна (на самом деле, если я прокручиваю ее вручную, она видна, когда другой элемент внутри AppBarLayout полностью прокрутил экран). Это мой код:RecyclerLayout внутри видимости CoordinatorLayout scrollToPosition

<android.support.design.widget.CoordinatorLayout 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"> 

      <android.support.design.widget.AppBarLayout 
       android:id="@+id/app_bar_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:elevation="0dp"> 

       <com.github.mikephil.charting.charts.BarChart 
        android:id="@+id/chart" 
        android:layout_width="match_parent" 
        android:layout_height="280dp" 
        android:layout_below="@+id/toolbar_conso" 
        android:background="@color/background_dark_grey" 
        app:layout_scrollFlags="scroll|enterAlways" /> 

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

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

       <LinearLayout 
        android:id="@+id/linear_total_conso" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:orientation="horizontal"> 

        <TextView 
         android:id="@+id/current_consumption" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="" 
         android:textColor="@color/graph_consumption_color" 
         android:textSize="30sp" /> 

        <TextView 
         android:id="@+id/consumption_unit" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="5dp" 
         android:layout_marginStart="5dp" 
         android:text="" 
         android:textAppearance="@style/TextAppearance.Smartlink.Large" 
         android:textColor="@color/graph_consumption_color" /> 
       </LinearLayout> 

       <android.support.v7.widget.RecyclerView 
        android:id="@+id/recycler_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal" /> 
      </LinearLayout> 
     </android.support.design.widget.CoordinatorLayout> 

И код RecyclerView:

recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 
    recyclerView.setLayoutManager(new LinearLayoutManager(context)); 
    recyclerView.setAdapter(adapter); 

Адаптер представляет собой простой текстовый адаптер ........

Когда я попробуйте выполнить прокрутку после получения события из Сервиса, поэтому есть BroadcastReceiver, который принимает событие и управляет результатом:

@Override 
    public void onReceive(Context context, Intent intent) { 
      int position = intent.getExtras().getInt(POSITION); 
      recyclerView.scrollToPosition(position); 
    } 

Моя идея заключается в том, что я думаю, что RecyclerView does't понимает, что позиция не видно .....

+0

Хорошо Телль мне, что вам нужно, recyclerView не должен прокручиваться, если элемент не отображается или вы хотите прокрутить RecyclerView, а также AppbarLayout? –

+0

Я хочу, чтобы RecyclerView прокручивался до положения, которое я хочу (и оно должно быть видимым для реального). Если я удалю CoordinatorLayout и AppBarLayout, он работает так, как я хочу, проблема в том, что RecyclerView прокручивается до «невидимой» (вне видимости пользователя) позиции, когда я выполняю scrollToPosition для последних элементов списка ... .. – zapotec

ответ

4

Попробуйте этот код, но это будет разрушаться appbarlayout:

@Override public void onReceive(Context context, Intent intent) { 
     int position = intent.getExtras().getInt(POSITION); 
     app_bar_layout.setExpanded(false); 
     recyclerView.scrollToPosition(position); 
    } 
+0

Я пробовал использовать это, но это правда, что это разрушит мой app_bar_layout ....... Я не знаю, есть ли другой способ для вычисления элементов, которые являются видимыми и невидимыми для реального (I пробовал использовать метод LayoutManager linearLayoutManager.findLastVisibleItemPosition(), но возврат является невидимым элементом .......) – zapotec

+0

То, что вы пытаетесь, возможно, не будет возможным, потому что любой элемент списка или recyclerview создается, когда он виден , – Drv

+0

Дело в том, что одно, на самом деле это не видно с точки зрения пользователя, потому что AppBarLayout не рушится ....... В любом случае, возможно, это ограничение с помощью CoordinatorLayout и AppBarLayout. – zapotec

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