简单封装一个批量上传图片的控件

这里二次封装一个批量上传的控件,因为调用的地方很多,所以用view的形式来用,

 

 

 

 

先上主体代码:

 1 public class Photo extends LinearLayout {
 2     private ArrayList<String> selectedPhotos = new ArrayList<>();
 3     private PhotoAdapter photoAdapter;
 4     private View viewAppTitle;
 5     private Activity activity;
 6 
 7     public Photo(Context context) {
 8         super(context);
 9         init();
10     }
11 
12     public Photo(Context context, AttributeSet attrs) {
13         super(context, attrs);
14         init();
15     }
16 
17     public Photo(Context context, AttributeSet attrs, int defStyleAttr) {
18         super(context, attrs, defStyleAttr);
19         init();
20     }
21 
22     public Photo(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
23         super(context, attrs, defStyleAttr, defStyleRes);
24         init();
25     }
26 
27 
28     public void setActivity(Activity activity) {
29         this.activity = activity;
30 
31     }
32 
33     //设置图片
34     public void setPhoto(ArrayList<String> selectedPhotos) {
35         this.selectedPhotos = selectedPhotos;
36     }
37 
38     //获取图片
39     public ArrayList<String> getPhoto() {
40         return this.selectedPhotos;
41     }
42 
43 
44     public void setResult(Intent data) {
45         List<String> photos = null;
46         if (data != null) {
47             photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
48         }
49         selectedPhotos.clear();
50         if (photos != null) {
51             selectedPhotos.addAll(photos);
52         }
53         photoAdapter.notifyDataSetChanged();
54     }
55 
56     void init() {
57         LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
58         LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
59         viewAppTitle = inflater.inflate(R.layout.photoview, null);
60         RecyclerView recyclerView = (RecyclerView) viewAppTitle.findViewById(R.id.recycler_view);
61         photoAdapter = new PhotoAdapter(getContext(), selectedPhotos);
62         recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, OrientationHelper.VERTICAL));
63         recyclerView.setAdapter(photoAdapter);
64         recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(activity,
65                 new RecyclerItemClickListener.OnItemClickListener() {
66                     @Override
67                     public void onItemClick(View view, int position) {
68                         if (photoAdapter.getItemViewType(position) == 1) {
69                             PhotoPicker.builder()
70                                     .setPhotoCount(9)
71                                     .setShowCamera(true)
72                                     .setPreviewEnabled(false)
73                                     .setSelected(selectedPhotos)
74                                     .start(activity);
75                         } else {
76                             PhotoPreview.builder()
77                                     .setPhotos(selectedPhotos)
78                                     .setCurrentItem(position)
79                                     .start(activity);
80                         }
81                     }
82                 }));
83         this.addView(viewAppTitle, layoutParams);
84     }
85 }

里面的方法根据自己的业务逻辑来定制

layout 里只有一个recycview 就不上代码了。

调用也就是普通找打控件id即刻,注意要在onActivityResult中回传以及接受数据

        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {

            List<String> photos = null;
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();
            if (photos != null) {
                selectedPhotos.addAll(photos);
            }
            photoAdapter.notifyDataSetChanged();


            List<File> li = new ArrayList<>();
            for (int c = 0; c < selectedPhotos.size(); c++)
            ex.send(new File(selectedPhotos.get(c)));


        }

 ps :使用的第三方为PhotoPicker

转载于:https://www.cnblogs.com/Conker/p/6569248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值