AndroidSweetSheet:从底部弹出面板(1)



AndroidSweetSheet:从底部弹出面板(1)

AndroidSweetSheet又是一个从底部弹出面板的开源项目。我在以前写的文章中介绍了不少这些项目,见附录文章5,6,7,8。现在再介绍一个AndroidSweetSheet。
AndroidSweetSheet项目主页是:https://github.com/zzz40500/AndroidSweetSheet

AndroidSweetSheet实现的结果如动态图所示:

AndroidSweetSheet:从底部弹出面板(1)



AndroidSweetSheet本身的代码不晓得是咋回事,跑原作者的代码没问题,但是把其库导入到自己的项目中就有些问题,我重新把项目整理成ok的代码包,push到github上,新的链接地址是:https://github.com/zhangphil/AndroidSweetSheet_by_phil
如果以后使用,可以直接使用新代码包中的库。
写一个简单的AndroidSweetSheet例子。
写布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
    xmlns:tools="http://schemas.android.com/tools"
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/ic_launcher"
    tools:context="zhangphil.demo.MainActivity">

    <Button
        android:
        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();
        }
    }
}



代码运行结果。
初始化状态:

AndroidSweetSheet:从底部弹出面板(1)



点击button按钮弹出AndroidSweetSheet:

AndroidSweetSheet:从底部弹出面板(1)




AndroidSweetSheet某一项选中时:

AndroidSweetSheet:从底部弹出面板(1)



附录文章:
1,《Android自底部平滑向上滑出面板的AndroidSlidingUpPanel》链接地址:http://blog.csdn.net/zhangphil/article/details/51444509 
2,《Android音乐、视频类APP常用控件:DraggablePanel(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/51566860 
3,《Android音乐、视频类APP常用控件:DraggablePanel(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/51578665
4,《Android图片加载与缓存开源框架:Android Glide》链接地址http://blog.csdn.net/zhangphil/article/details/45535693
5,《Android BottomSheet:便捷易用的底部滑出面板(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/51775955
6,《Android BottomSheet:以选取图片为例(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/51776408
7,《Android BottomSheet:List列表或Grid网格展示(3)》链接地址:http://blog.csdn.net/zhangphil/article/details/51781698
8,《Android BottomSheet:底部弹出Fragment面板(4)》链接地址:http://blog.csdn.net/zhangphil/article/details/51787875