前段时间的项目中,遇到了一个对于当时的我来说,是一个比较麻烦的事,就是在使用PopupWindow做顶部的弹出菜单的时候,我用了GridView来添加itemView,结果发现在点击的时候,会出现itemView的父布局也会出现点击现象,没达到项目要求,然后就使用了RecyclerView来做这个功能。下面贴部分代码:
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import java.util.List;
/**
* Created by Virczz on 2015/11/18
*/
public class Fm_ShouYe11 extends Fragment implements View.OnClickListener {
private View view;
private String totalUrl = "http://www.test.com";
private Gson gson = new GsonBuilder().create();
private PopupWindow popupWindow;
private RelativeLayout rl_main_titlebar;
/* 点击PopupWindow后的数据传递 */
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x11:
/* 对UI线程的操作 */
break;
}
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.shouye_fm, null);
/* 布局代码 */
initView();
return view;
}
@Override
/** PopupWindow的点击 */
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_main_menu:
/* 主页左上角的菜单 */
if (popupWindow != null && popupWindow.isShowing()) {
return;
} else {
/* 第一次打开PopupWindow */
showPop();
}
break;
default:
break;
}
}
/**
* 初始化视图
*/
private void initView() {
ImageView iv_main_menu;
rl_main_titlebar = (RelativeLayout) view.findViewById(R.id.rl_main_titlebar);
iv_main_menu = (ImageView) view.findViewById(R.id.iv_main_menu);
rl_main_titlebar.setOnClickListener(this);
iv_main_menu.setOnClickListener(this);
}
/**
* 展示CategoryPopupWindow
*/
private void showPop() {
CategoryPopupWindow catePop = null;
catePop = new CategoryPopupWindow(getActivity());
}
/**
* CategoryPopupWindow 封装了PopupWindow的类
*/
public class CategoryPopupWindow {
final String cagetoryUrl = "http://www.test.com";
private Context context;
private CategoriesRvAdapter rvAdapter;
private RecyclerView popupWindowRecycler;
/* 获取自定义布局文件categories_view的视图*/
private LayoutInflater mLayoutInflater;
private ViewGroup customView;
private StringBuffer sb = new StringBuffer();
private CategoryPopupWindow(Context context) {
this.context = context;
FirstOpenCategory();
}
/**
* 打开PopupWindow 加载数据和界面
*/
private void FirstOpenCategory() {
mLayoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customView = (ViewGroup) mLayoutInflater.inflate(R.layout.categories_view, null, false);
/* 初始化视图 */
popupWindowRecycler = (RecyclerView) customView.findViewById(R.id.recycler_categories);
/* 这句话可能导致popupWindowRecycler不能展示并报错:RecyclerView: No adapter attached; skipping layout */
popupWindowRecycler.requestFocus();
popupWindowRecycler.setHasFixedSize(true);
GridLayoutManager cateGridLayoutManager = new GridLayoutManager(context, 3);
popupWindowRecycler.setLayoutManager(cateGridLayoutManager);
popupWindowRecycler.setItemAnimator(new DefaultItemAnimator());
popupWindowRecycler.setFocusable(true);
/* 网络请求区域 */
rvAdapter = new CategoriesRvAdapter("你的List");
popupWindowRecycler.setAdapter(rvAdapter);
popupWindowRecycler.setClickable(true);
rvAdapter.setOnItemClickListener(new CategoriesRvAdapter.CustomOnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
sb.append("http://www.test.com");
Message message = mHandler.obtainMessage();
message.obj = sb.toString();
message.what = 0x11;
mHandler.sendMessage(message);
popupWindow.dismiss();
}
});
/* 创建PopupWindow实例 */
popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
/* 设置PopupWindow背景 */
popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorWhite)));
/* 设置动画效果 [R.style.AnimationFade] */
//popupWindow.setAnimationStyle(R.style.AnimationFade);
popupWindow.update();
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
/* 设置popupWindow的位置(相对rl_main_titlebar的位置) */
int topBarHeight = rl_main_titlebar.getBottom();
popupWindow.showAsDropDown(rl_main_titlebar, 0, (topBarHeight - rl_main_titlebar.getHeight()) / 2);
/* 自定义view 添加触摸事件 */
customView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = customView.findViewById(R.id.recycler_categories).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
// popupWindow.dismiss();//这里是点击空白处消失的方法
}
}
return true;
}
});
}
}
}
上面在CategiriesRvAdapter里为RecyclerView重写了监听接口。
在适配器里要设置item的参数列表
public class CategoriesRvAdapter extends RecyclerView.Adapter<CategoriesRvAdapter.ViewHolder> {
...
ViewGroup.LayoutParams layoutParams = holder.textView.getLayoutParams();
layoutParams.height = (int) (Math.random() * 100);
layoutParams.height = holder.textView.getMaxHeight();
layoutParams.width = holder.textView.getMaxWidth();
holder.textView.setLayoutParams(layoutParams);
...
}
大致就是这样了,用Handler来传递需要传递的数据,就可以操作UI线程了。