http://www.myexception.cn/android/1346620.html
android弹出选择对话框-仿某团购网android客户端栏目选择
未完善,还比较简单:
弹出对话框,先只显示左边列表,右边列表需要宽度设置为0,用visiable会影响使用。
选中左边列表时,再根据数据情况展现右边或者是选中提交。
PopupCategory.java
package com.example.ysq.activity; import java.util.ArrayList; import java.util.List; import com.example.ysq.R; import com.example.ysq.adapter.CategoryAdapter; import com.example.ysq.entity.CategoryEntity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.LinearLayout.LayoutParams; import android.widget.ListView; import roboguice.fragment.RoboDialogFragment; import roboguice.inject.InjectView; public class PopupCategory extends RoboDialogFragment{ @InjectView(R.id.rootcategory) private ListView rootcategory; @InjectView(R.id.childcategory) private ListView childcategory; @InjectView(R.id.child_lay) private View child_lay; private Window window; public int x,y; @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); this.rootcategory.setDrawSelectorOnTop(true); this.rootcategory.setVerticalScrollBarEnabled(false); this.childcategory.setDrawSelectorOnTop(true); this.childcategory.setVerticalScrollBarEnabled(false); final List<CategoryEntity> data=new ArrayList<CategoryEntity>(); for(int i=0;i<20;i++){ CategoryEntity e=new CategoryEntity(); data.add(e); e.setSeq(i); e.setCount(i%3); e.setName("测试"+i); List<CategoryEntity> childList=new ArrayList<CategoryEntity>(); e.setChildList(childList); for(int j=0;j<e.getCount();j++){ CategoryEntity ce=new CategoryEntity(); childList.add(ce); ce.setSeq(j); ce.setCount(j); ce.setName("子目录"+i+":"+j); } } final CategoryAdapter adapter=new CategoryAdapter(getActivity(), data, R.layout.category_item); rootcategory.setAdapter(adapter); final CategoryAdapter childadapter=new CategoryAdapter(getActivity(), new ArrayList<CategoryEntity>(), R.layout.category_child_item); childcategory.setAdapter(childadapter); rootcategory.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { //用下面getChildAt会报异常,因为这个只能在手机屏幕可视范围内能获取到,超出返回null // View v; // Log.e("i:", parent.getCount()+""); // Log.e("i:", parent.getChildCount()+""); // for (int i = 0; i < parent.getChildCount(); i++) { // Log.e("i:", i+""); // v = parent.getChildAt(i); // if (position == i) { // v.setBackgroundColor(Color.argb(153, 0, 186, 255)); // } else { // v.setBackgroundColor(Color.TRANSPARENT); // } // } adapter.setFocus(position); CategoryEntity category=data.get(position); List<CategoryEntity> childList=category.getChildList(); if(childList.size()>0){ childadapter.setData(childList); WindowManager.LayoutParams localLayoutParams; localLayoutParams=window.getAttributes(); localLayoutParams.width = (int)(width-localLayoutParams.x*2); localLayoutParams.height = (int)(height-localLayoutParams.y*2); window.setAttributes(localLayoutParams); LayoutParams params=new LayoutParams((int)(width/2-localLayoutParams.x),localLayoutParams.height); child_lay.setLayoutParams(params); }else{ //todo: PopupCategory.this.dismiss(); } } }); childcategory.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { PopupCategory.this.dismiss(); } }); // child_lay.setVisibility(View.INVISIBLE); //设置宽度高度 LayoutParams params=new LayoutParams(0, 0); child_lay.setLayoutParams(params); } private int height; private int width; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { window = this.getDialog().getWindow(); WindowManager.LayoutParams localLayoutParams; window.setWindowAnimations(R.style.push_top); window.setBackgroundDrawableResource(R.drawable.bg_category); localLayoutParams=window.getAttributes(); localLayoutParams.gravity=Gravity.TOP|Gravity.LEFT; localLayoutParams.x=this.x; localLayoutParams.y=this.y; window.addFlags(Window.FEATURE_PROGRESS); localLayoutParams.dimAmount=0.6F; DisplayMetrics displaymetrics = new DisplayMetrics(); this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); height = displaymetrics.heightPixels; width = displaymetrics.widthPixels; localLayoutParams.width = (int)(width/2-localLayoutParams.x); localLayoutParams.height = (int)(height-localLayoutParams.y*2); window.setAttributes(localLayoutParams); this.setCancelable(true); this.getDialog().setCanceledOnTouchOutside(true); View localView = inflater.inflate(R.layout.popup_category, container, false); return localView; } }
CategoryAdapter.java
package com.example.ysq.adapter; import java.util.List; import com.example.ysq.R; import com.example.ysq.entity.CategoryEntity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class CategoryAdapter extends BaseAdapter { private List<CategoryEntity> entityList; private LayoutInflater inflater; private int itemViewResource; private int index = 0; public CategoryAdapter(Context paramContext, List<CategoryEntity> entityList, int itemViewResource) { this.inflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater")); this.entityList = entityList; this.itemViewResource = itemViewResource; } public void setData(List<CategoryEntity> paramList){ this.entityList=paramList; notifyDataSetChanged(); } private CategoryEntity b(int paramInt) { return (CategoryEntity) this.entityList.get(paramInt); } public final void setFocus(int index ) { this.index = index ; notifyDataSetChanged(); } public final int getCount() { if (isNull(this.entityList)) return 0; return this.entityList.size(); } public final long getItemId(int paramInt) { return paramInt; } public final View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { CategoryEntity localwk = b(paramInt); ListItemView itemView; if (paramView == null) { paramView = this.inflater.inflate(this.itemViewResource, paramViewGroup, false); itemView = new ListItemView(); itemView.name = ((TextView)paramView.findViewById(R.id.name)); itemView.count = ((TextView)paramView.findViewById(R.id.count)); itemView.haschild = ((ImageView)paramView.findViewById(R.id.haschild)); paramView.setTag(itemView); }else{ itemView = (ListItemView)paramView.getTag(); } if(this.itemViewResource==R.layout.category_item){//父结构 if(localwk.getSeq()==this.index){ paramView.setBackgroundColor(Color.argb(255,234,234,234));//#ffeaeaea }else{ paramView.setBackgroundDrawable(null); } } itemView.name.setText(localwk.getName()); if (localwk.getCount() != -1) itemView.count.setText(String.valueOf(localwk.getCount())); if (isNull(localwk.getChildList())) itemView.haschild.setVisibility(View.INVISIBLE); else itemView.haschild.setVisibility(View.VISIBLE); return paramView; } static class ListItemView { TextView name; TextView count; public ImageView haschild; } public <T> boolean isNull(List<T> paramList) { return (paramList == null) || (paramList.isEmpty()); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } }