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

这里二次封装一个批量上传的控件,因为调用的地方很多,所以用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

相关内容推荐