android弹出选择对话框-仿某团购网android客户端栏目选择

效果图一:
android弹出选择对话框-仿某团购网android客户端栏目选择_第1张图片
 效果图二:

android弹出选择对话框-仿某团购网android客户端栏目选择_第2张图片

 

未完善,还比较简单:

  弹出对话框,先只显示左边列表,右边列表需要宽度设置为0,用visiable会影响使用。

 选中左边列表时,再根据数据情况展现右边或者是选中提交。

 

 

PopupCategory.java

Java代码   收藏代码
  1. package com.example.ysq.activity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import com.example.ysq.R;  
  7. import com.example.ysq.adapter.CategoryAdapter;  
  8. import com.example.ysq.entity.CategoryEntity;  
  9.   
  10. import android.os.Bundle;  
  11. import android.util.DisplayMetrics;  
  12. import android.view.Gravity;  
  13. import android.view.LayoutInflater;  
  14. import android.view.View;  
  15. import android.view.ViewGroup;  
  16. import android.view.Window;  
  17. import android.view.WindowManager;  
  18. import android.widget.AdapterView;  
  19. import android.widget.AdapterView.OnItemClickListener;  
  20. import android.widget.LinearLayout.LayoutParams;  
  21. import android.widget.ListView;  
  22. import roboguice.fragment.RoboDialogFragment;  
  23. import roboguice.inject.InjectView;  
  24.   
  25. public class PopupCategory extends RoboDialogFragment{  
  26.       
  27.     @InjectView(R.id.rootcategory)  
  28.     private ListView rootcategory;  
  29.     @InjectView(R.id.childcategory)  
  30.     private ListView childcategory;  
  31.     @InjectView(R.id.child_lay)  
  32.     private View child_lay;  
  33.     private Window window;  
  34.     public int x,y;  
  35.       
  36.     @Override  
  37.     public void onActivityCreated(Bundle savedInstanceState){  
  38.         super.onActivityCreated(savedInstanceState);  
  39.         this.rootcategory.setDrawSelectorOnTop(true);  
  40.         this.rootcategory.setVerticalScrollBarEnabled(false);  
  41.         this.childcategory.setDrawSelectorOnTop(true);  
  42.         this.childcategory.setVerticalScrollBarEnabled(false);  
  43.           
  44.         final List data=new ArrayList();  
  45.         for(int i=0;i<20;i++){  
  46.             CategoryEntity e=new CategoryEntity();  
  47.             data.add(e);  
  48.             e.setSeq(i);  
  49.             e.setCount(i%3);  
  50.             e.setName("测试"+i);  
  51.             List childList=new ArrayList();  
  52.             e.setChildList(childList);  
  53.             for(int j=0;j
  54.                 CategoryEntity ce=new CategoryEntity();  
  55.                 childList.add(ce);  
  56.                 ce.setSeq(j);  
  57.                 ce.setCount(j);  
  58.                 ce.setName("子目录"+i+":"+j);  
  59.             }  
  60.         }  
  61.         final CategoryAdapter adapter=new CategoryAdapter(getActivity(), data, R.layout.category_item);  
  62.         rootcategory.setAdapter(adapter);  
  63.         final CategoryAdapter childadapter=new CategoryAdapter(getActivity(), new ArrayList(), R.layout.category_child_item);  
  64.         childcategory.setAdapter(childadapter);  
  65.         rootcategory.setOnItemClickListener(new OnItemClickListener() {  
  66.             @Override  
  67.             public void onItemClick(AdapterView parent, View view,int position, long id) {  
  68.                 //用下面getChildAt会报异常,因为这个只能在手机屏幕可视范围内能获取到,超出返回null  
  69. //              View v;  
  70. //              Log.e("i:", parent.getCount()+"");  
  71. //              Log.e("i:", parent.getChildCount()+"");  
  72. //              for (int i = 0; i < parent.getChildCount(); i++) {  
  73. //                  Log.e("i:", i+"");  
  74. //                  v = parent.getChildAt(i);  
  75. //                  if (position == i) {  
  76. //                      v.setBackgroundColor(Color.argb(153, 0, 186, 255));  
  77. //                  } else {  
  78. //                      v.setBackgroundColor(Color.TRANSPARENT);  
  79. //                  }  
  80. //              }  
  81.                 adapter.setFocus(position);  
  82.                 CategoryEntity category=data.get(position);  
  83.                 List childList=category.getChildList();  
  84.                 if(childList.size()>0){  
  85.                     childadapter.setData(childList);  
  86.                     WindowManager.LayoutParams localLayoutParams;  
  87.                     localLayoutParams=window.getAttributes();  
  88.                     localLayoutParams.width = (int)(width-localLayoutParams.x*2);  
  89.                     localLayoutParams.height = (int)(height-localLayoutParams.y*2);  
  90.                     window.setAttributes(localLayoutParams);  
  91.                     LayoutParams params=new LayoutParams((int)(width/2-localLayoutParams.x),localLayoutParams.height);  
  92.                     child_lay.setLayoutParams(params);  
  93.                 }else{  
  94.                     //todo:  
  95.                     PopupCategory.this.dismiss();  
  96.                 }  
  97.             }  
  98.         });  
  99.           
  100.         childcategory.setOnItemClickListener(new OnItemClickListener() {  
  101.             @Override  
  102.             public void onItemClick(AdapterView parent, View view,int position, long id) {  
  103.                 PopupCategory.this.dismiss();  
  104.             }  
  105.         });  
  106.           
  107. //      child_lay.setVisibility(View.INVISIBLE);  
  108.         //设置宽度高度  
  109.         LayoutParams params=new LayoutParams(00);  
  110.         child_lay.setLayoutParams(params);  
  111.     }  
  112.       
  113.     private int height;  
  114.     private int width;  
  115.     @Override  
  116.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  117.             Bundle savedInstanceState) {  
  118.         window = this.getDialog().getWindow();  
  119.         WindowManager.LayoutParams localLayoutParams;  
  120.         window.setWindowAnimations(R.style.push_top);  
  121.         window.setBackgroundDrawableResource(R.drawable.bg_category);  
  122.         localLayoutParams=window.getAttributes();  
  123.         localLayoutParams.gravity=Gravity.TOP|Gravity.LEFT;  
  124.         localLayoutParams.x=this.x;  
  125.         localLayoutParams.y=this.y;  
  126.         window.addFlags(Window.FEATURE_PROGRESS);  
  127.         localLayoutParams.dimAmount=0.6F;  
  128.           
  129.         DisplayMetrics displaymetrics = new DisplayMetrics();  
  130.         this.getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);  
  131.         height = displaymetrics.heightPixels;  
  132.         width = displaymetrics.widthPixels;  
  133.   
  134.         localLayoutParams.width = (int)(width/2-localLayoutParams.x);  
  135.         localLayoutParams.height = (int)(height-localLayoutParams.y*2);  
  136.         window.setAttributes(localLayoutParams);  
  137.           
  138.         this.setCancelable(true);  
  139.         this.getDialog().setCanceledOnTouchOutside(true);  
  140.         View localView = inflater.inflate(R.layout.popup_category, container, false);  
  141.         return localView;  
  142.     }  
  143. }  

 

CategoryAdapter.java

Java代码   收藏代码
  1. package com.example.ysq.adapter;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import com.example.ysq.R;  
  7. import com.example.ysq.entity.CategoryEntity;  
  8.   
  9. import android.content.Context;  
  10. import android.graphics.Color;  
  11. import android.view.LayoutInflater;  
  12. import android.view.View;  
  13. import android.view.ViewGroup;  
  14. import android.widget.BaseAdapter;  
  15. import android.widget.ImageView;  
  16. import android.widget.TextView;  
  17.   
  18. public class CategoryAdapter extends BaseAdapter {  
  19.     private List entityList;  
  20.     private LayoutInflater inflater;  
  21.     private int itemViewResource;  
  22.     private int index = 0;  
  23.   
  24.     public CategoryAdapter(Context paramContext, List entityList, int itemViewResource) {  
  25.         this.inflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater"));  
  26.         this.entityList = entityList;  
  27.         this.itemViewResource = itemViewResource;  
  28.     }  
  29.       
  30.     public void setData(List paramList){  
  31.         this.entityList=paramList;  
  32.         notifyDataSetChanged();  
  33.     }  
  34.   
  35.     private CategoryEntity b(int paramInt) {  
  36.         return (CategoryEntity) this.entityList.get(paramInt);  
  37.     }  
  38.   
  39.     public final void setFocus(int index ) {  
  40.         this.index  = index ;  
  41.         notifyDataSetChanged();  
  42.     }  
  43.   
  44.     public final int getCount() {  
  45.         if (isNull(this.entityList))  
  46.             return 0;  
  47.         return this.entityList.size();  
  48.     }  
  49.   
  50.     public final long getItemId(int paramInt) {  
  51.         return paramInt;  
  52.     }  
  53.   
  54.     public final View getView(int paramInt, View paramView,  
  55.             ViewGroup paramViewGroup) {  
  56.         CategoryEntity localwk = b(paramInt);  
  57.         ListItemView itemView;  
  58.         if (paramView == null)  
  59.         {  
  60.           paramView = this.inflater.inflate(this.itemViewResource, paramViewGroup, false);  
  61.           itemView = new ListItemView();  
  62.           itemView.name = ((TextView)paramView.findViewById(R.id.name));  
  63.           itemView.count = ((TextView)paramView.findViewById(R.id.count));  
  64.           itemView.haschild = ((ImageView)paramView.findViewById(R.id.haschild));  
  65.           paramView.setTag(itemView);  
  66.         }else{  
  67.             itemView = (ListItemView)paramView.getTag();  
  68.         }  
  69.   
  70.         if(this.itemViewResource==R.layout.category_item){//父结构  
  71.             if(localwk.getSeq()==this.index){  
  72.                 paramView.setBackgroundColor(Color.argb(255,234,234,234));//#ffeaeaea  
  73.             }else{  
  74.                 paramView.setBackgroundDrawable(null);  
  75.             }  
  76.         }  
  77.           
  78.         itemView.name.setText(localwk.getName());  
  79.           if (localwk.getCount() != -1)  
  80.               itemView.count.setText(String.valueOf(localwk.getCount()));  
  81.             
  82.           if (isNull(localwk.getChildList()))  
  83.               itemView.haschild.setVisibility(View.INVISIBLE);  
  84.           else  
  85.               itemView.haschild.setVisibility(View.VISIBLE);  
  86.             
  87.         return paramView;  
  88.     }  
  89.       
  90.     static class ListItemView  
  91.     {  
  92.       TextView name;  
  93.       TextView count;  
  94.       public ImageView haschild;  
  95.     }  
  96.       
  97.     public  boolean isNull(List paramList) {  
  98.         return (paramList == null) || (paramList.isEmpty());  
  99.     }  
  100.   
  101.     @Override  
  102.     public Object getItem(int arg0) {  
  103.         // TODO Auto-generated method stub  
  104.         return null;  
  105.     }  
  106. }  

转自:http://owftc.iteye.com/blog/1993283

你可能感兴趣的:(android)