android 小功能 超简单 仿pickerView 图片滚筒选择列表

近期项目需求 一个滚筒图片选择列表 想来想去没有合适的 ios有一个pickerView 是可以的 而我大安卓我没有想起来有这样效果图 ( 可能是我没想起来) 时间比较赶 简易性 超简单的图片滚筒选择列表
思路 : 其实很简单 popupwindow+listview 你可以给list view固定高度 listview的item 也固定 popupwindow视图内显示你想要的个数 可以上下滑动 再给listview的item一个点击变色 就可以了 下面上代码

popwindow的布局代码

android 小功能 超简单 仿pickerView 图片滚筒选择列表_第1张图片

给个效果图吧

listview 条目是40x40的图片 让它居中就行了

java 代码

public class MainActivity extends AppCompatActivity {

private Button mainbt;
private PopupWindow popupWindow;
private WindowManager.LayoutParams params;
private ImageView mainimg;
private int p = 1001;
private ImageView img1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainbt = (Button) findViewById(R.id.main_bt);
    mainimg = (ImageView) findViewById(R.id.main_img);
    img1 = (ImageView) findViewById(R.id.main_img1);
    RotateAnimation animation = new RotateAnimation(0,-15,50,50);
    animation.setRepeatCount(1);
    animation.setFillAfter(true);
    animation.setDuration(1);
    img1.startAnimation(animation);
    mainbt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           showPopupWindow(v);
        }
    });
}

private void showPopupWindow(View v) {
    View view = getLayoutInflater().inflate(R.layout.popup_window, null);

    popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);

    //点击外部区域popwindow消失

    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setFocusable(true);
    //显示(靠中间)
    popupWindow.showAtLocation(findViewById(R.id.main_view), Gravity.BOTTOM, 0, 0);
    TextView popsure = (TextView) view.findViewById(R.id.pop_sure);
    TextView popcancel = (TextView) view.findViewById(R.id.pop_cancel);


    List list = new ArrayList<>();
    for (int i = 0; i < 6; i++) {
        list.add(i);
    }
    ListView lv_list = (ListView) view.findViewById(R.id.grade_list);
    final PopAdapter popAdapter = new PopAdapter(MainActivity.this, (ArrayList) list);

    lv_list.setAdapter(popAdapter);


    lv_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView parent, View view,
                                int position, long id) {

            p = position;
            Toast.makeText(MainActivity.this, "点击了条目--"+position, Toast.LENGTH_SHORT).show();
           // popupWindow.dismiss();
        }
    });
    lv_list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "选中了---"+position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView parent) {
            Toast.makeText(MainActivity.this, "没选中", Toast.LENGTH_SHORT).show();
        }
    });

    popsure.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "确定--"+p, Toast.LENGTH_SHORT).show();
           switch (p){
               case 1001:
                   break;
               case 0:
                   mainimg.setImageResource(R.drawable.gouone);
                   break;
               case 1:
                   mainimg.setImageResource(R.drawable.goutwo);
                   break;
               case 2:
                   mainimg.setImageResource(R.drawable.gouthree);
                   break;
               case 3:
                   mainimg.setImageResource(R.drawable.goufour);
                   break;
               case 4:
                   mainimg.setImageResource(R.drawable.goufive);
                   break;
               case 5:
                   mainimg.setImageResource(R.drawable.gousix);
                   break;
           }
            popupWindow.dismiss();
        }
    });
    popcancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });
}


// listview 适配器

class PopAdapter extends BaseAdapter{
    Context context;
    ArrayList list;
    public PopAdapter(Context context, ArrayList list) {
        this.context=context;
        this.list=list;
    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView==null){
            view = View.inflate(context,R.layout.item_pop,null);
            holder = new ViewHolder();
            holder.popimg = (ImageView) view.findViewById(R.id.pop_img);
            view.setTag(holder);

        }else {
            view= convertView;
            holder = (ViewHolder) view.getTag();
        }
        switch (list.get(position)){
            case 0:
                holder.popimg.setImageResource(R.drawable.gouone);
                break;
            case 1:
                holder.popimg.setImageResource(R.drawable.goutwo);
                break;
            case 2:
                holder.popimg.setImageResource(R.drawable.gouthree);
                break;
            case 3:
                holder.popimg.setImageResource(R.drawable.goufour);
                break;
            case 4:
                holder.popimg.setImageResource(R.drawable.goufive);
                break;
            case 5:
                holder.popimg.setImageResource(R.drawable.gousix);
                break;
        }
        return view;
    }

    class ViewHolder{
        ImageView popimg;
    }
}

}
效果图

android 小功能 超简单 仿pickerView 图片滚筒选择列表_第2张图片

喜欢的可以点个赞 有时间再研究一下 ok的话给大家自定义一个 仿ios的pickerview

你可能感兴趣的:(android开发)