android - 使用 Toolbar 和 SwipeRefreshLayout 的正确方法是什么?

标签 android toolbar android-toolbar swiperefreshlayout appcompatactivity

我正在尝试使用 SwipeRefreshLayout 和工具栏,但每次我同时使用两者时,进度圆圈都会出现在工具栏上方,这在设计方面确实很糟糕。我尝试将 SwipeRefreshLayout 放在工具栏下方,但这不起作用,进度圈仍然出现在它的顶部(状态栏上方)。我想像在 Gmail 应用程序中一样同时使用工具栏和布局。我正在使用 AppCompat 库。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>


   <android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">




    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <include layout="@layout/bluetooth_toolbar"
                android:id="@+id/bluetooth_toolbar"/>


            <TextView
                android:id="@+id/swipeTo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Swipe down to refresh"
                android:textSize="20sp"
                android:layout_below="@+id/bluetooth_toolbar"
                android:layout_centerHorizontal="true" />

            <ListView
                android:id="@+id/newDevices"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/swipeTo"
                android:layout_marginTop="50dp" />

            <ListView
                android:id="@+id/pairedDevices"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/newDevices"
                android:layout_marginTop="50dp" />
        </RelativeLayout>
    </ScrollView>

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

public class BluetoothActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {


private static final int REQUEST_ENABLE_BT = 0;
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private SwipeRefreshLayout mSwipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth);
    Toolbar bluetoothToolbar = (Toolbar) findViewById(R.id.bluetooth_toolbar);
    setSupportActionBar(bluetoothToolbar);
    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setColorScheme(
            R.color.yellow,
            R.color.flashy_blue,
            R.color.yellow,
            R.color.flashy_blue);


    if (bluetoothAdapter == null) {
        // Device does not support Bluetooth
    }
    if (!bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }


}


public void onRefresh() {
    Toast.makeText(this, getString(R.string.bluetooth_refresh), Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }, 2000);

    bluetoothAdapter.startDiscovery();

 if (bluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
     Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
       discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
     startActivity(discoverableIntent);
      }


}


protected void onDestroy() {
    super.onDestroy();
    bluetoothAdapter.cancelDiscovery();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bluetooth, 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();
    if (id == R.id.action_settings) {
        startActivityForResult(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS), 0);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

谢谢

最佳答案

这是因为您的工具栏是刷新的相关布局的一部分。你应该像这样排除工具栏:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <include layout="@layout/bluetooth_toolbar"
      android:id="@+id/bluetooth_toolbar"/>

  <android.support.v4.widget.SwipeRefreshLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/swipe_container"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

      <RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

        <TextView
            android:id="@+id/swipeTo"
            android:layout_width="wrap_content"
            ...

      </RelativeLayout>
    </ScrollView>
  </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

关于android - 使用 Toolbar 和 SwipeRefreshLayout 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31920048/

相关文章:

java - Android:如何显示相同标签名称的所有 XML 值

android - ExpandableListView 组 View 在 Android 中选中时发生变化

java - 字符串的Android工具栏字幕

android - Appbar 在 Android 设计库 22.2.1 与 22.2.0 中滚动

android - 工具栏上的图标不会打开抽屉导航,并且在滚动时不会自行消失

android - 如何在 TextView 中创建线条?

android-layout - "?actionbarsize","?attr:actionbarsize"和 "?android:actionBarSize"有什么区别?

delphi - View 工具栏上的图标没有响应

Android 工具栏和用户图像动画,如 Twitter

android - 带有BottomSheet的windowSoftInputMode不起作用