AndroidSweetSheet:從底部彈出面闆(1)
AndroidSweetSheet又是一個從底部彈出面闆的開源項目。我在以前寫的文章中介紹了不少這些項目,見附錄文章5,6,7,8。現在再介紹一個AndroidSweetSheet。
AndroidSweetSheet項目首頁是:
https://github.com/zzz40500/AndroidSweetSheetAndroidSweetSheet實作的結果如動态圖所示:
AndroidSweetSheet本身的代碼不曉得是咋回事,跑原作者的代碼沒問題,但是把其庫導入到自己的項目中就有些問題,我重新把項目整理成ok的代碼包,push到github上,新的連結位址是:
https://github.com/zhangphil/AndroidSweetSheet_by_phil如果以後使用,可以直接使用新代碼包中的庫。
寫一個簡單的AndroidSweetSheet例子。
寫布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ic_launcher"
tools:context="zhangphil.demo.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="展示" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_centerHorizontal="true"
android:text="這是一段文字,測試背景變化效果"
android:textSize="15sp"/>
</RelativeLayout>
上層Java代碼:
package zhangphil.demo;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.mingle.entity.MenuEntity;
import com.mingle.sweetpick.BlurEffect;
import com.mingle.sweetpick.DimEffect;
import com.mingle.sweetpick.RecyclerViewDelegate;
import com.mingle.sweetpick.SweetSheet;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private SweetSheet mSweetSheet;
private RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RelativeLayout) findViewById(R.id.root);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mSweetSheet.isShow())
mSweetSheet.dismiss();
else
mSweetSheet.show();
}
});
setupRecyclerView();
}
private void setupRecyclerView() {
final ArrayList<MenuEntity> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
MenuEntity menuEntity = new MenuEntity();
menuEntity.iconId = R.mipmap.ic_launcher;
menuEntity.titleColor = Color.RED;
menuEntity.title = "zhang phil @ csdn " + i;
list.add(menuEntity);
}
// SweetSheet 控件,根據 rl 确認位置
mSweetSheet = new SweetSheet(rl);
//設定資料源 (資料源支援設定 list 數組,也支援從菜單中擷取)
//如果是從菜單中加載,那麼是 .setMenuList(R.menu.menu_sweet);
mSweetSheet.setMenuList(list);
//根據設定不同的 Delegate 來顯示不同的風格.
mSweetSheet.setDelegate(new RecyclerViewDelegate(true));
//根據設定不同Effect 來顯示背景效果
// BlurEffect:模糊效果.
// DimEffect 變暗效果
mSweetSheet.setBackgroundEffect(new DimEffect(10f));
//設定點選事件
mSweetSheet.setOnMenuItemClickListener(new SweetSheet.OnMenuItemClickListener() {
@Override
public boolean onItemClick(int position, MenuEntity menuEntity) {
//即時改變目前項的顔色
list.get(position).titleColor = Color.GREEN;
((RecyclerViewDelegate) mSweetSheet.getDelegate()).notifyDataSetChanged();
Toast.makeText(MainActivity.this, menuEntity.title + " " + position, Toast.LENGTH_SHORT).show();
//根據傳回值, true 會關閉 SweetSheet ,false 則不會.
return false;
}
});
}
@Override
public void onBackPressed() {
if (mSweetSheet.isShow()) {
mSweetSheet.dismiss();
} else {
super.onBackPressed();
}
}
}
代碼運作結果。
初始化狀态:
點選button按鈕彈出AndroidSweetSheet:
AndroidSweetSheet某一項選中時:
附錄文章:
1,《Android自底部平滑向上滑出面闆的AndroidSlidingUpPanel》連結位址:
http://blog.csdn.net/zhangphil/article/details/514445092,《Android音樂、視訊類APP常用控件:DraggablePanel(1)》連結位址:
http://blog.csdn.net/zhangphil/article/details/515668603,《Android音樂、視訊類APP常用控件:DraggablePanel(2)》連結位址:
http://blog.csdn.net/zhangphil/article/details/515786654,《Android圖檔加載與緩存開源架構:Android Glide》連結位址
http://blog.csdn.net/zhangphil/article/details/45535693:
5,《Android BottomSheet:便捷易用的底部滑出面闆(1)》連結位址:
http://blog.csdn.net/zhangphil/article/details/517759556,《Android BottomSheet:以選取圖檔為例(2)》連結位址:
http://blog.csdn.net/zhangphil/article/details/517764087,《Android BottomSheet:List清單或Grid網格展示(3)》連結位址:
http://blog.csdn.net/zhangphil/article/details/517816988,《Android BottomSheet:底部彈出Fragment面闆(4)》連結位址:
http://blog.csdn.net/zhangphil/article/details/51787875