android开发怎么实现lixtview页面点击每个item出现不同的popupwindow
1个回答
展开全部
学编程应该与时俱进,建议使用recyclerview,这是listview的替代品。
下面是recyclerview的一些用法:
activity:
recyclerView = (RecyclerView) view.findViewById(R.id.list_btn); //获取控件
recyclerView.setLayoutManager(new GridLayoutManager(activity, 3)); //设置布局
homeBtnAdapter = new HomeBtnAdapter(recyclerView); //设置适配器
//分割线 build.gradle 添加依赖 compile 'com.yqritc:recyclerview-flexibledivider:1.3.0'
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(activity .this).build());
recyclerView.setItemAnimator( new DefaultItemAnimator()); //设置出现消失动画
recyclerView.setAdapter(homeBtnAdapter); //添加适配器
homeBtnAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() { //item点击事件
@Override
public void onRVItemClick(ViewGroup viewGroup, View view, int i) {
if(null != homeBtnAdapter && homeBtnAdapter.getItemCount() > 0 && null != activity &&!activity.isFinishing()){
HomeBtn entity = homeBtnAdapter.getItem(i); //获取点击的项
if(null != entity && null != entity.getCls()) {
//点击事件的实现
}
}
}
});
Adapter:
public class HomeBtnAdapter extends BGARecyclerViewAdapter<HomeBtn> { //适配器继承BGARecyclerViewAdapter
private LinearLayout.LayoutParams params; //定义适配器的布局
public HomeBtnAdapter(RecyclerView recyclerView) {
super(recyclerView, R.layout.home_home_btn_item); //定义项的样式
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //初始化布局
}
@Override
protected void fillData(BGAViewHolderHelper bgaViewHolderHelper, int i, HomeBtn entity) {
((LinearLayout) bgaViewHolderHelper.getView(R.id.ll_btn)).setLayoutParams(params);
//背景颜色
bgaViewHolderHelper.setBackgroundColor(R.id.ll_btn, entity.getBackColorId());
//文字
String name = (null != entity && !TextUtils.isEmpty(entity.getName()))?entity.getName():"";
bgaViewHolderHelper.setText(R.id.text_name, name);
//图标
bgaViewHolderHelper.setImageResource(R.id.img_name, entity.getImgResourceId());
}
}
上面是代码仅供参考!
如果一定要使用listview,只要给他添加setOnItemClickListener监听器,就可以了
下面是recyclerview的一些用法:
activity:
recyclerView = (RecyclerView) view.findViewById(R.id.list_btn); //获取控件
recyclerView.setLayoutManager(new GridLayoutManager(activity, 3)); //设置布局
homeBtnAdapter = new HomeBtnAdapter(recyclerView); //设置适配器
//分割线 build.gradle 添加依赖 compile 'com.yqritc:recyclerview-flexibledivider:1.3.0'
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(activity .this).build());
recyclerView.setItemAnimator( new DefaultItemAnimator()); //设置出现消失动画
recyclerView.setAdapter(homeBtnAdapter); //添加适配器
homeBtnAdapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() { //item点击事件
@Override
public void onRVItemClick(ViewGroup viewGroup, View view, int i) {
if(null != homeBtnAdapter && homeBtnAdapter.getItemCount() > 0 && null != activity &&!activity.isFinishing()){
HomeBtn entity = homeBtnAdapter.getItem(i); //获取点击的项
if(null != entity && null != entity.getCls()) {
//点击事件的实现
}
}
}
});
Adapter:
public class HomeBtnAdapter extends BGARecyclerViewAdapter<HomeBtn> { //适配器继承BGARecyclerViewAdapter
private LinearLayout.LayoutParams params; //定义适配器的布局
public HomeBtnAdapter(RecyclerView recyclerView) {
super(recyclerView, R.layout.home_home_btn_item); //定义项的样式
params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); //初始化布局
}
@Override
protected void fillData(BGAViewHolderHelper bgaViewHolderHelper, int i, HomeBtn entity) {
((LinearLayout) bgaViewHolderHelper.getView(R.id.ll_btn)).setLayoutParams(params);
//背景颜色
bgaViewHolderHelper.setBackgroundColor(R.id.ll_btn, entity.getBackColorId());
//文字
String name = (null != entity && !TextUtils.isEmpty(entity.getName()))?entity.getName():"";
bgaViewHolderHelper.setText(R.id.text_name, name);
//图标
bgaViewHolderHelper.setImageResource(R.id.img_name, entity.getImgResourceId());
}
}
上面是代码仅供参考!
如果一定要使用listview,只要给他添加setOnItemClickListener监听器,就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询