package com.onetoo.www.onetoo.ui;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import com.onetoo.www.onetoo.R;
import com.onetoo.www.onetoo.abapter.home.HomeCategoryRecycleAdapter;
import com.onetoo.www.onetoo.config.SpaceItemDecoration;
import com.onetoo.www.onetoo.utils.DensityUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by longShun on 2017/2/6.
* desc 分類彈出框
*/
public class PopWindowForHomeCategory extends PopupWindow implements HomeCategoryRecycleAdapter.OnItemClickListener{
private Context mContext;
private View mPopWindowView;
public PopWindowForHomeCategory(Context context) {
super(context);
this.mContext = context;
initWindowView();
initPopWindow();
}
private void initPopWindow() {
//設置的View
setContentView(mPopWindowView);
//設置彈出窗體的寬
setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
//設置彈出窗體的高
setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
//設置彈出窗體可點擊
setFocusable(true);
//設置彈出窗體動畫效果
setAnimationStyle(R.style.PopWindowFadeInOut);//彈出的效果
//實例化一個ColorDrawable顏色爲半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
//設置彈出窗體的背景
setBackgroundDrawable(dw);
}
private void initWindowView() {
mPopWindowView = LayoutInflater.from(mContext).inflate(R.layout.home_category, null);
RecyclerView mRvCategory = (RecyclerView) mPopWindowView.findViewById(R.id.rv_home_category);
mRvCategory.addItemDecoration(new SpaceItemDecoration(DensityUtils.dp2px(mContext,10)));
mRvCategory.setLayoutManager(new GridLayoutManager(mContext,3));
List<String> mListCategory =new ArrayList<>();
mListCategory.add("餐飲美食");
mListCategory.add("購物");
mListCategory.add("生活服務");
mListCategory.add("麗人");
mListCategory.add("休閒娛樂");
mListCategory.add("母嬰親子");
List<Integer> mListIcon = new ArrayList<>();
mListIcon.add(R.drawable.home_food);
mListIcon.add(R.drawable.home_shop);
mListIcon.add(R.drawable.home_service);
mListIcon.add(R.drawable.home_beautiful);
mListIcon.add(R.drawable.home_funny);
mListIcon.add(R.drawable.home_baby);
HomeCategoryRecycleAdapter adapter = new HomeCategoryRecycleAdapter(mContext, mListCategory, mListIcon);
mRvCategory.setAdapter(adapter);
adapter.setOnItemClickListener(this);
}
@Override
public void onItemClick(int position) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(position);
}
}
private HomeCategoryRecycleAdapter.OnItemClickListener onItemClickListener;
public void setOnItemClickListener(HomeCategoryRecycleAdapter.OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}
動畫效果代碼:
values-styles.xml
<!--首頁分類彈出框透明漸變-->
<style name="PopWindowFadeInOut">
<item name="android:windowEnterAnimation">@anim/anim_fade_in</item>
<item name="android:windowExitAnimation">@anim/anim_fade_out</item>
</style>
res-anim目錄下動畫
anim_fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fillAfter="true"
>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
/>
</set>
anim_fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fillAfter="true"
>
<alpha
android:fromAlpha="1"
android:toAlpha="0"
/>
</set>
使用代碼:
popWindowForHomeCategory = new PopWindowForHomeCategory(getActivity());
popWindowForHomeCategory.setOnDismissListener(xxx);
popWindowForHomeCategory.setOnItemClickListener(xxx);
popWindowForHomeCategory.showAsDropDown(view);