亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android BottomSheet效果的兩種實(shí)現(xiàn)方式

 更新時(shí)間:2018年08月10日 15:25:09   作者:JackMeGo  
這篇文章主要介紹了Android BottomSheet效果的兩種實(shí)現(xiàn)方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了Android BottomSheet效果的兩種實(shí)現(xiàn)方式,分享給大家,具體如下:

BottomSheet效果

BottomSheet的效果是指從屏幕底部向上滑的效果,是MaterialDesign風(fēng)格的一種,視覺(jué)效果如下:

BottomSheet效果

實(shí)現(xiàn)這種效果有幾種不同的方式,如果是在一個(gè)固定的頁(yè)面上添加這種效果,可以在該頁(yè)面布局中添加BoottomSheet相關(guān)的控件。如果是作為通用控件來(lái)提供給不同頁(yè)面使用,則可以使用BottomSheetDialog實(shí)現(xiàn),本文將對(duì)兩種方法進(jìn)行講解,其中會(huì)講到一些使用上的細(xì)節(jié),處理不好這些細(xì)節(jié),會(huì)出現(xiàn)非常怪異的效果。

單頁(yè)面添加BottomSheet效果

首先引入依賴包:

compile 'com.android.support:design:27.1.1'

頁(yè)面布局如下:

<?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"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:id="@+id/customActionWebView"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="文本一"/>

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="文本二"/>
  </LinearLayout>

  <android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="150dp"
    android:layout_gravity="bottom"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <WebView
      android:id="@+id/web_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    </WebView>
  </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

根布局需要使用CoordinatorLayout,同時(shí)在其直接子布局——這里是NestedScrollView——里添加behavior app:layout_behavior = "@string/bottom_sheet_behavior" 。很多文章說(shuō)指定behavior的控件必須是NestedScrollView,這是錯(cuò)誤的,實(shí)際上任何view或viewgroup都可以。該behavior配合CoordinateLayout就可以實(shí)現(xiàn)behavior所在控件的上滑效果。如果需要上滑的布局展示的時(shí)候先漏出一部分,如上面視頻所示,可以通過(guò)設(shè)置 app:behavior_peekHeight 實(shí)現(xiàn),它用來(lái)指定漏出的高度。

在代碼部分,首先獲取NestedScrollView的behavior,然后通過(guò)behavior控制底部卡片什么時(shí)候彈出,同時(shí)會(huì)有一些狀態(tài)回調(diào)函數(shù)可供調(diào)用。

public class BrowserActivity extends AppCompatActivity {
  private NestedScrollView nestedScrollView;
  private BottomSheetBehavior behavior;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.browser_layout);

    nestedScrollView = (NestedScrollView) findViewById(R.id.nested_scroll_view);
    behavior = BottomSheetBehavior.from(nestedScrollView);
    behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
      @Override
      public void onStateChanged(@NonNull View bottomSheet, int newState) {
        //這里是bottomSheet 狀態(tài)的改變
      }

      @Override
      public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        //這里是拖拽中的回調(diào),根據(jù)slideOffset可以做一些動(dòng)畫
      }
    });
  }

  public void showBottomSheet() {
    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
  }
}

通過(guò)這種方式可以在特定的頁(yè)面添加底部上滑的效果。

BottomSheetDialog實(shí)現(xiàn)通用效果

BottomSheetDialog是BottomSheet效果實(shí)現(xiàn)的一種更加通用的方法,比如我們需要在不同的頁(yè)面實(shí)現(xiàn)長(zhǎng)按文本彈出卡片列表效果,下面給出實(shí)現(xiàn)。

我們集成BottomSheetDialog實(shí)現(xiàn)自定義的Dialog,其布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <view class="com.example.trio.tensordemo.CustomBottomSheetDialogForWebView$NestViewEmbeddedListView"
      android:id="@+id/card_list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="@color/transparent"
      android:scrollbars="none"
      android:dividerHeight="15dp"
      android:layout_margin="20dp">
    </view>
  </LinearLayout>

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

注意,這里最外層布局需要是 NestedScrollView ,而 不能是CoordinateLayout ,因?yàn)锽ottomSheetDialog本身已經(jīng)有個(gè)CoordinateLayout根布局,它會(huì)把你的布局文件包裹起來(lái),如果你在自己的布局里把最外層布局寫成CoordinateLayout,會(huì)導(dǎo)致底部上滑的卡片,在下滑消失后屏幕依舊變暗的問(wèn)題,這是因?yàn)檎麄€(gè)布局變成了兩個(gè)CoordinateLayout嵌套,下滑的時(shí)候里面的CoordinateLayout滑出屏幕,但外層的CoordinateLayout仍然在展示。通過(guò)查閱BottomSheetDialog源碼可以看出,它是這樣包裹你的布局文件的:

public class BottomSheetDialog extends AppCompatDialog {
  ...
  @Override
  public void setContentView(@LayoutRes int layoutResId) {
    super.setContentView(wrapInBottomSheet(layoutResId, null, null));
  }
  
  private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
    final FrameLayout container = (FrameLayout) View.inflate(getContext(),
        R.layout.design_bottom_sheet_dialog, null);
    final CoordinatorLayout coordinator =
        (CoordinatorLayout) container.findViewById(R.id.coordinator);
    if (layoutResId != 0 && view == null) {
      view = getLayoutInflater().inflate(layoutResId, coordinator, false);
    }
    FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
    mBehavior = BottomSheetBehavior.from(bottomSheet);
    mBehavior.setBottomSheetCallback(mBottomSheetCallback);
    mBehavior.setHideable(mCancelable);
    if (params == null) {
      bottomSheet.addView(view);
    } else {
      bottomSheet.addView(view, params);
    }
    // We treat the CoordinatorLayout as outside the dialog though it is technically inside
    coordinator.findViewById(R.id.touch_outside).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        if (mCancelable && isShowing() && shouldWindowCloseOnTouchOutside()) {
          cancel();
        }
      }
    });
    ...
    return container;
  }
  ...
}

所以,BottomSheetDialog本身的布局實(shí)際如下:

BottomSheetDialog頁(yè)面布局

我們可以看到最外層是FrameLayout,里面有個(gè)CoordinateLayout,CoordinateLayout里面有個(gè)直接子布局FrameLayout,該CoordinateLayout指定了Behavior,最里面才是用戶自定義的布局,所以不應(yīng)該在自定義布局里再添加CoordinateLayout,也不應(yīng)該再次指定Behavior,直接擺放你的內(nèi)容就行。

我們的自定義布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <view class="com.example.trio.tensordemo.CustomBottomSheetDialogForWebView$NestViewEmbeddedListView"
      android:id="@+id/card_list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="@color/transparent"
      android:scrollbars="none"
      android:dividerHeight="15dp"
      android:layout_margin="20dp">
    </view>
  </LinearLayout>

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

布局的核心是一個(gè)ListView,注意,由于ListView和behavior都需要處理滑動(dòng)事件,所以直接使用ListView會(huì)導(dǎo)致滑動(dòng)沖突,解決辦法是采用ScrollView嵌套ListView實(shí)現(xiàn),同時(shí)使用自定義的ListView將所有列表項(xiàng)展開(kāi)。

自定義的NestViewEmbeddedListView如下:

public static class NestViewEmbeddedListView extends ListView {
    public NestViewEmbeddedListView(android.content.Context context, android.util.AttributeSet attrs){
      super(context, attrs);
    }
    /**
     * 設(shè)置不滾動(dòng)
     */
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
      int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
          MeasureSpec.AT_MOST);
      super.onMeasure(widthMeasureSpec, expandSpec);

    }
  }

自定義的BottomSheetDialog代碼如下:

public class CustomBottomSheetDialogForWebView extends BottomSheetDialog {
  private Context context;
  private NestViewEmbeddedListView listView;
  private NerResult nerResult;
  private CardListAdapter cardListAdapter = new CardListAdapter();

  public CustomBottomSheetDialogForWebView(@NonNull Context context, NerResult nerResult) {
    super(context);
    this.context = context;
    this.nerResult = nerResult;
    createView();
  }

  public void createView() {
    View bottomSheetView = getLayoutInflater().inflate(R.layout.webview_bottom_sheet_layout, null);
    setContentView(bottomSheetView);

    // 注意:這里要給layout的parent設(shè)置peekHeight,而不是在layout里給layout本身設(shè)置,下面設(shè)置背景色同理,坑爹?。。?
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(((View) bottomSheetView.getParent()));
    bottomSheetBehavior.setPeekHeight(730);

    ((View) bottomSheetView.getParent()).setBackgroundColor(context.getResources().getColor(R.color.transparent));
    listView = bottomSheetView.findViewById(R.id.card_list_view);

    cardListAdapter.setNerItems(nerResult);
    listView.setAdapter(cardListAdapter);
  }
}

這里需要注意的就是,設(shè)置背景透明和獲取Behavior都是對(duì)自定義布局的父布局,也就是bottomSheetView.getParent()進(jìn)行。最終的效果就是下面的效果:

BottomSheet效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android超詳細(xì)講解組件AdapterView的使用

    Android超詳細(xì)講解組件AdapterView的使用

    AdapterView組件是一組重要的組件,AdapterView本身是一個(gè)抽象基類,它派生的子類在用法上十分相似,從AdapterView派生出的三個(gè)子類:AdsListView、AdsSpinner、AdapterViewAnimator,這3個(gè)子類依然是抽象的,實(shí)際運(yùn)用時(shí)需要它們的子類
    2022-03-03
  • Android studio 3.0上進(jìn)行多渠道打包遇到的問(wèn)題小結(jié)(超簡(jiǎn)潔版)

    Android studio 3.0上進(jìn)行多渠道打包遇到的問(wèn)題小結(jié)(超簡(jiǎn)潔版)

    這篇文章主要介紹了Android studio 3.0上進(jìn)行多渠道打包遇到的問(wèn)題小結(jié)(超簡(jiǎn)潔版),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11
  • Android使用剪切板傳遞數(shù)據(jù)

    Android使用剪切板傳遞數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Android使用剪切板傳遞數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 詳解android 視頻圖片混合輪播實(shí)現(xiàn)

    詳解android 視頻圖片混合輪播實(shí)現(xiàn)

    這篇文章主要介紹了android 視頻圖片混合輪播實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Android連接服務(wù)器端的Socket的實(shí)例代碼

    Android連接服務(wù)器端的Socket的實(shí)例代碼

    這篇文章主要介紹了Android連接服務(wù)器端的Socket的實(shí)例代碼,需要的朋友可以參考下
    2017-05-05
  • Android開(kāi)發(fā)新手常見(jiàn)的10個(gè)誤區(qū)

    Android開(kāi)發(fā)新手常見(jiàn)的10個(gè)誤區(qū)

    這篇文章主要介紹了Android開(kāi)發(fā)新手常見(jiàn)的10個(gè)誤區(qū),我們?nèi)匀豢吹搅四男┬碌腁ndr&#8203;&#8203;oid開(kāi)發(fā)人員不斷重復(fù)的錯(cuò)誤,這里有10個(gè)最常見(jiàn)的誤區(qū),需要的朋友可以參考下
    2015-03-03
  • Android利用Theme自定義Activity間的切換動(dòng)畫

    Android利用Theme自定義Activity間的切換動(dòng)畫

    這篇文章主要為大家詳細(xì)介紹了Android利用Theme自定義Activity間的切換動(dòng)畫,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android基礎(chǔ)之startActivityForResult()的用法詳解

    Android基礎(chǔ)之startActivityForResult()的用法詳解

    這篇文章主要給大家介紹了Android中startActivityForResult()的用法,文中給出了詳細(xì)的介紹與示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。
    2017-01-01
  • Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

    Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

    這篇文章將借助開(kāi)源庫(kù)?MLKit?實(shí)現(xiàn)條形碼掃描,對(duì)于商品條形碼也可以很好地識(shí)別成功,該庫(kù)的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測(cè)等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下
    2023-08-08
  • Android實(shí)現(xiàn)志愿者系統(tǒng)詳細(xì)步驟與代碼

    Android實(shí)現(xiàn)志愿者系統(tǒng)詳細(xì)步驟與代碼

    這篇文章主要介紹了Android實(shí)現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設(shè)計(jì),SQLite數(shù)據(jù)表有用戶表、成員表和活動(dòng)表,有十多個(gè)Activity頁(yè)面。打開(kāi)應(yīng)用,進(jìn)入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊(cè)賬號(hào),登錄成功后進(jìn)入主界面
    2023-02-02

最新評(píng)論