【问题标题】:SwipeRefreshView, CollapsingToolbar, and RecyclerViewSwipeRefreshView、CollapsingToolbar 和 RecyclerView
【发布时间】:2015-09-23 13:13:09
【问题描述】:

刷新指示器不显示在顶部。相反,它显示在 Recycler 视图的顶部。但是,我需要它像 Gmail、Google Plus 等一样显示在顶部。

这是布局代码和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);
}

}

【问题讨论】:

  • 我认为它显示正确,从 xml 中看到的滑动刷新位于工具栏和图像周围的应用栏布局下方,因此从技术上讲,您告诉 android 它应该位于图像下方正是它放置的位置
  • 现在我从未尝试过,但您始终可以尝试和错误解决方案,看看最适合您的解决方案,如果不确定这里有人会为您提供解决方案需要。

标签: android toolbar swiperefreshlayout appbar android-collapsingtoolbarlayout


【解决方案1】:

试试这段代码,它必须在 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);

【讨论】:

  • 什么是变量“act”?
  • 尝试更新的答案。我添加了这一行:swipeRef.setProgressViewOffset(false, 0, actionBarSize);
猜你喜欢
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
相关资源
最近更新 更多