BottomSheetBehavior底部彈出視窗的用法
阿新 • • 發佈:2019-02-08
需要的依賴:
compile ‘com.android.support:appcompat-v7:23.2.1’
compile ‘com.android.support:design:23.2.1’
效果圖如下:
一直出現的問題是彈出視窗只顯示部分,很煩人,浪費半個下午終於解決了
下面看程式碼:
package com.example.mybottomdialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget .BottomSheetBehavior;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import com.example.mybottomdialog.adapter.Item;
import com.example.mybottomdialog .adapter.ItemAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button btn_window;
View mBottomSheet;
BottomSheetBehavior mBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
btn_window = (Button) findViewById(R.id.btn_window);
mBottomSheet = findViewById(R.id.bottomSheet);
RecyclerView recyclerView = (RecyclerView) mBottomSheet.findViewById(R.id.recyclerview);
mBehavior = BottomSheetBehavior.from(mBottomSheet);
mBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(new ItemAdapter(createItems()));
btn_window.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
}
public List<Item> createItems() {
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(R.drawable.f001, "開心"));
items.add(new Item(R.drawable.f002, "哈哈"));
items.add(new Item(R.drawable.f003, "愛你"));
items.add(new Item(R.drawable.f004, "汗"));
items.add(new Item(R.drawable.f005, "暈了"));
items.add(new Item(R.drawable.f006, "無語"));
items.add(new Item(R.drawable.f007, "呵呵"));
items.add(new Item(R.drawable.f008, "日"));
return items;
}
/* private void showBottomSheetDialog() {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.sheet, null);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setAdapter(new ItemAdapter(createItems()));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getParent()));
bottomSheetDialog.setContentView(view);
bottomSheetDialog.show();
}*/
}
activity_main.xml佈局檔案
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示底部彈出視窗" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:elevation="4dp"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior"
tools:ignore="UnusedAttribute">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
相關Adpter
package com.example.mybottomdialog.adapter;
import android.support.annotation.DrawableRes;
public class Item {
private int mDrawableRes;
private String mTitle;
public Item(@DrawableRes int drawable, String title) {
mDrawableRes = drawable;
mTitle = title;
}
public int getDrawableResource() {
return mDrawableRes;
}
public String getTitle() {
return mTitle;
}
}
package com.example.mybottomdialog.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mybottomdialog.R;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
private List<Item> mItems;
public ItemAdapter(List<Item> items) {
mItems = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setData(mItems.get(position));
}
@Override
public int getItemCount() {
return mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public Item item;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
public void setData(Item item) {
this.item = item;
imageView.setImageResource(item.getDrawableResource());
textView.setText(item.getTitle());
}
}
}
styles.xml格式檔案,這個很有用
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
item.xml檔案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f008" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="xx"
android:textSize="20dp" />
</RelativeLayout>
哈哈哈,搞定了,懶得廢話!程式碼是學coding的最好老師!