AndroidSweetSheet:ViewPager的实现(2)
附录文章9说明了AndroidSweetSheet典型的列表样式实现,本文写一个例子,说明AndroidSweetSheet以翻页的ViewPager样式实现。本例所需的布局文件和附录文章9相同,只是上层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.DimEffect; import com.mingle.sweetpick.SweetSheet; import com.mingle.sweetpick.ViewPagerDelegate; 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(); } }); setupViewpager(); } private void setupViewpager() { mSweetSheet = new SweetSheet(rl); final ArrayList<MenuEntity> list = new ArrayList<>(); for (int i = 0; i < 100; 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); //从menu 中设置数据源 //mSweetSheet.setMenuList(R.menu.menu_sweet); //ViewPager样式的底部面板 mSweetSheet.setDelegate(new ViewPagerDelegate()); mSweetSheet.setBackgroundEffect(new DimEffect(10f)); mSweetSheet.setOnMenuItemClickListener(new SweetSheet.OnMenuItemClickListener() { @Override public boolean onItemClick(int position, MenuEntity menuEntity) { Toast.makeText(MainActivity.this, menuEntity.title + " " + position, Toast.LENGTH_SHORT).show(); return true; } }); } /** 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(); } } }
代码运行结果:
附录文章:
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
9,《AndroidSweetSheet:从底部弹出面板(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/51790845