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/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/517878759,《AndroidSweetSheet:從底部彈出面闆(1)》連結位址:
http://blog.csdn.net/zhangphil/article/details/51790845