0

enter image description hereSwipeRefreshView, CollapsingToolbar и RecyclerView

Индикатор обновления не отображается на верхней части. Вместо этого он отображается поверх представления Recycler. Тем не менее, мне это нужно, чтобы отобразить на вершине, как Gmail, Google Plus и т.д.

Heres код макета и MainActivity

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:fab="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/transparent" 
android:fitsSystemWindows="true"> 


<android.support.design.widget.AppBarLayout 
    android:id="@+id/bar" 
    android:layout_width="match_parent" 
    android:layout_height="252dp" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:collapsedTitleTextAppearance="@style/CollapsedAppBar" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginBottom="32dp" 
     app:expandedTitleMarginEnd="64dp" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleTextAppearance="@android:color/transparent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <ImageView 
      android:id="@+id/header" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/random" 
      android:fitsSystemWindows="true" 
      android:scaleType="centerCrop" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.7" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/anim_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.CollapsingToolbarLayout> 

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

<android.support.v4.widget.SwipeRefreshLayout 
    android:id="@+id/contentView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/scrollableview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.v4.widget.SwipeRefreshLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="152dp" 
    android:layout_height="152dp" 
    android:background="@android:color/transparent" 
    android:clickable="false" 
    android:scaleType="fitXY" 
    app:borderWidth="0dp" 
    app:layout_anchor="@id/bar" 
    app:layout_anchorGravity="bottom|center" /> 
</android.support.design.widget.CoordinatorLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener { 

int mutedColor = R.attr.colorPrimary; 
SwipeRefreshLayout mSwipeRefreshLayout; 
AppBarLayout appBarLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolBar = (Toolbar) findViewById(R.id.anim_toolbar); 
    setSupportActionBar(toolBar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
    collapsingToolbarLayout.setTitle("Sudarshan Sunder"); 
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.scrollableview); 
    recyclerView.setLayoutManager(layoutManager); 
    RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getApplicationContext()); 
    recyclerView.setAdapter(recyclerViewAdapter); 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.header); 
    bmp = CircularImageBitmap.getCroppedBitmap(bmp, 200); 
    FloatingActionButton floatingActionButton = (FloatingActionButton) findViewById(R.id.fab); 
    floatingActionButton.setImageBitmap(bmp); 
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contentView); 
    appBarLayout = (AppBarLayout) findViewById(R.id.bar); 
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 

      mSwipeRefreshLayout.setRefreshing(false); 
     } 
    }); 

} 

@Override 
public void onOffsetChanged(AppBarLayout appBarLayout, int i) { 
    if (i == 0) { 
     mSwipeRefreshLayout.setEnabled(true); 
    } else { 
     mSwipeRefreshLayout.setEnabled(false); 
    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    appBarLayout.addOnOffsetChangedListener(this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    appBarLayout.removeOnOffsetChangedListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

} 
+1

Я думаю, что это правильно отображается, обновление салфетки, как видно из xml i s ниже макета панели приложений, которые окружают панель инструментов и изображение, так что технически вы говорите андроиду, что оно должно быть ниже изображения, которое именно там, где оно разместило его. –

+1

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

ответ

0

Попробуйте этот код, который должен показать индикатор внизу дна ActionBar

int[] actionBarSizeAttr = new int[] { android.R.attr.actionBarSize }; 
TypedValue typedValue = new TypedValue(); 
TypedArray a = context.obtainStyledAttributes(typedValue.data, actionBarSizeAttr); 
int actionBarSize = a.getDimensionPixelSize(0, 100); 
a.recycle(); 
swipeRef.setProgressViewOffset(false, 0, actionBarSize); 
swipeRef.setProgressViewEndTarget(false, actionBarSize); 
+0

Что такое переменная «act»? –

+0

Это просто контекст) Я обновляю свой ответ – mohax

+0

Не работает. Та же проблема. Спасибо в любом случае –