排序(正序与反序)与popupwindow

先看看实现的效果:


大概需要实现的效果就是:

网络获取数据,根据数据里的价格进行排序(反序和正序),

点击条目,弹出自定义popupwindow,点击”是“删除条目并隐藏popupwindow,

点击“否”隐藏popupwindow;

下面直接上代码:

MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private String path = "http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603";
    private Button zheng;
    private Button fan;
    private ListView listView;
    private List list2;
    private MyAdapter adapter;
    private PopupWindow popupWindow;
    private Button but1;
    private Button but2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        listView.setOnItemClickListener(this);
        RequestParams params=new RequestParams(path);
        org.xutils.x.http().get(params, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                RootBean rootBean = gson.fromJson(result, RootBean.class);
                List rows = rootBean.result.rows;

                list2 = new ArrayList<>();
                for (int i = 0; i  adapterView, View view, final int i, long l) {
        View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item2, null);
        popupWindow = new PopupWindow(inflate, 300,200);
        //设置popuwindow外部可以点击
        popupWindow.setOutsideTouchable(true);//可以点击
        //设置popuwindow里面的listView有焦点
        popupWindow.setFocusable(true);
        //如果想让popuwindow有动画效果,下面这行代码必须写,没有不会执行动画
        popupWindow.setBackgroundDrawable(new ColorDrawable());
       // 计算出来的位置,y方向就在anchorView的上面和下面对齐显示,x方向就是与屏幕右边对齐显示
         //如果anchorView的位置有变化,就可以适当自己额外加入偏移来修正
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        view.getLocationOnScreen(anchorLoc);
        final int anchorHeight = view.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = ScreenUtils.getScreenHeight(view.getContext());
        final int screenWidth = ScreenUtils.getScreenWidth(view.getContext());
        inflate.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 计算contentView的高宽
        final int windowHeight = inflate.getMeasuredHeight();
        final int windowWidth = inflate.getMeasuredWidth();
        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
        } else {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        int xOff = 20;
        windowPos[0] -= xOff;
        popupWindow.showAtLocation(view, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
        but1 = inflate.findViewById(R.id.but1);
        but2 = inflate.findViewById(R.id.but2);
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               list2.remove(i);
                adapter.notifyDataSetChanged();
                popupWindow.dismiss();
            }
        });
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list2.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder=null;
            if (view==null){
                holder=new ViewHolder();
                view=View.inflate(MainActivity.this,R.layout.item,null);
                holder.imageView= view.findViewById(R.id.imageView);
                holder.textView=view.findViewById(R.id.textView);
                holder.textView2=view.findViewById(R.id.textView2);
                view.setTag(holder);
            }else {
                holder=(ViewHolder)view.getTag();
            }
            Image image=new Image();
            image.setImageView(list2.get(i).default_image,MainActivity.this,holder.imageView);
            holder.textView.setText(list2.get(i).loupan_name);
            holder.textView2.setText(list2.get(i).price+"");
            return view;
        }
        class ViewHolder{
              ImageView imageView;
            TextView textView,textView2;

        }
    }
}
里面用到了Xutils:

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
    }
}
imageLoader的工具类

public class Image {

    public static void setImageView(String url, Context context, ImageView imageView) {
        //初始化
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
        //创建Imageloader 对象
        ImageLoader imageLoader = ImageLoader.getInstance();
        //设置初始化
        imageLoader.init(config);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型
                .build();//构建完成

        imageLoader.displayImage(url, imageView, options);
        //.displayer(new RoundedBitmapDisplayer(90))  构建圆形图片
    }
}
获取屏幕宽高的工具类:

public class ScreenUtils {
    /**
     * 获取屏幕高度(px)
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }
}
下面是javaBean:(注意里面有实现的接口(排序的接口))

public class RootBean {

  

    public String status;
    public ResultBean result;

    public static class ResultBean {

        public String title;
        public String total;
        public int next_page;
        public List rows;

        public static class RowsBean {


            public String fang_type;
            public InfoBean info;

            public static class InfoBean implements Comparator{
                @Override
                public int compare(Object o, Object t1) {
                    InfoBean p1 = (InfoBean) o; // 强制转换
                    InfoBean p2 = (InfoBean) t1;
                    return new Double(p1.price).compareTo(new Double(p2.price));

                }
                public String loupan_id;
                public String loupan_name;
                public String city_id;
                public String new_price_value;
                public String new_price_back;
                public int price;
                public String region_id;
                public String region_title;
                public String sub_region_id;
                public String sub_region_title;
                public String default_image;
                public String address;
                public ShowActivityBean show_activity;
                public TuangouBean tuangou;
                public ZhuankftBean zhuankft;
                public KftBean kft;
                public String sale_label;
                public String loupan_property_type;
                public String tags;
                public String status_sale;
                public int has_sale;
                public String lat;
                public String lng;
                public String baidu_lat;
                public String baidu_lng;
                public String kaipan_new_date;
                public ActivityTagsBean activity_tags;
                public int has_consultant;
                public String sale_title;
                public RecommendPriceBean recommend_price;
                public String fang_type;
                public BrandBean brand;
                public int has_video;
                public int has_aerial_photo;
                public String area_rage;
                public String jianzhu_area;
                public List activity_tags_icon;
                public List multi_image;
                public List activity;




                public static class ShowActivityBean {
                }

                public static class TuangouBean {
                }

                public static class ZhuankftBean {
                }

                public static class KftBean {
                }

                public static class ActivityTagsBean {
                }

                public static class RecommendPriceBean {
                }

                public static class BrandBean {
                }

                public static class ActivityBean {
                    /**
                     * type : hui
                     * icon : http://pic1.ajkimg.com/material/get/8654d43460b906afd0355d73a3fb86ec.png
                     * title : 长安街西延线南约300米
                     */

                    public String type;
                    public String icon;
                    public String title;
                }
            }
        }
    }
}

下面就是几个布局:

activity_main:


 
     

listView中的条目:


    
    
        
        

    
填充popupwindow的布局:


    
    
        

最后别忘记jar包:



如有什么bug,请大神多多指点;





你可能感兴趣的:(移动互联,自定义控件)