Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. <ImageView
  2. android:contentDescription="@string/app_name"
  3. android:id="@+id/currentStreamImage"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:scaleType="centerCrop"/>
  7.  
  8. <ImageView
  9. android:id="@+id/selected_photo"
  10. android:contentDescription="@string/app_name"
  11. android:background="@null"
  12. android:layout_margin="12dp"
  13. android:layout_alignParentEnd="true"
  14. android:src="@drawable/add_image_icon"
  15. android:layout_width="40dp"
  16. android:layout_height="40dp" />
  17.  
  18. <LinearLayout
  19. android:layout_alignParentBottom="true"
  20. android:orientation="vertical"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content">
  23.  
  24. <LinearLayout
  25. android:padding="10dp"
  26. android:background="@drawable/fade_in_black"
  27. android:id="@+id/captionArea"
  28. android:orientation="horizontal"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content">
  31.  
  32. <EditText
  33. android:id="@+id/caption"
  34. android:hint="@string/enter_caption_here"
  35. android:textStyle="italic"
  36. android:textColor="@android:color/white"
  37. android:textColorHint="@android:color/white"
  38. android:layout_width="0dp"
  39. android:layout_height="wrap_content"
  40. android:layout_weight="1"/>
  41.  
  42. <android.support.design.widget.FloatingActionButton
  43. android:id="@+id/fab"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="bottom|end"
  47. android:src="@android:drawable/ic_menu_send" />
  48.  
  49. </LinearLayout>
  50.  
  51.  
  52. <android.support.v7.widget.RecyclerView
  53. android:id="@+id/recyclerView"
  54. android:layout_width="match_parent"
  55. android:layout_height="100dp"/>
  56.  
  57. </LinearLayout>
  58.  
  59. public class AddImageWithCaptionFragment extends Fragment implements ImageWithCaptionListener {
  60.  
  61. private ArrayList<ImgCap> imgCapArrayList = new ArrayList<>();
  62. private PerfectAdapter adapter;
  63. private RecyclerView recyclerView;
  64. private ImageView select,mainStream;
  65. private EditText captionEt;
  66. private int mCurrentPosition;
  67.  
  68. @Override
  69. public void onCreate(Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71. }
  72.  
  73. @Override
  74. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  75. Bundle savedInstanceState) {
  76. // Inflate the layout for this fragment
  77. View view = inflater.inflate(R.layout.add_img_with_cap_layout, container, false);
  78.  
  79. recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
  80. select = (ImageView) view.findViewById(R.id.selected_photo);
  81. mainStream = (ImageView) view.findViewById(R.id.currentStreamImage);
  82. captionEt = (EditText) view.findViewById(R.id.caption);
  83.  
  84. select.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87.  
  88. TedBottomPicker bottomSheetDialogFragment = new TedBottomPicker.Builder(getActivity())
  89. .setOnMultiImageSelectedListener(new TedBottomPicker.OnMultiImageSelectedListener() {
  90. @Override
  91. public void onImagesSelected(ArrayList<Uri> uriList) {
  92.  
  93. imgCapArrayList.clear();
  94. for (int i=0;i<uriList.size();i++) {
  95. ImgCap imgCap = new ImgCap(i,"", uriList.get(i));
  96. imgCapArrayList.add(imgCap);
  97. }
  98.  
  99. adapter = new PerfectAdapter(getActivity(),imgCapArrayList,mainStream,AddImageWithCaptionFragment.this);
  100. LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
  101. recyclerView.setLayoutManager(mLayoutManager);
  102. recyclerView.setAdapter(adapter);
  103. }
  104. })
  105. .setPeekHeight(1600)
  106. .showTitle(false)
  107. .setCompleteButtonText("Done")
  108. .setEmptySelectionText("No Select")
  109. .create();
  110.  
  111. bottomSheetDialogFragment.show(getActivity().getSupportFragmentManager());
  112.  
  113. }
  114. });
  115.  
  116. captionEt.addTextChangedListener(new TextWatcher() {
  117. @Override
  118. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  119.  
  120. }
  121.  
  122. @Override
  123. public void onTextChanged(CharSequence s, int start, int before, int count) {
  124. imgCapArrayList.get(mCurrentPosition).setCaption(s.toString());
  125. }
  126.  
  127. @Override
  128. public void afterTextChanged(Editable s) {
  129.  
  130. }
  131. });
  132.  
  133. return view;
  134. }
  135.  
  136. @Override
  137. public void imgCaptionCallBack(int position) {
  138. mCurrentPosition = position;
  139. captionEt.setText(imgCapArrayList.get(mCurrentPosition).getCaption());
  140. }
  141. }
  142.  
  143. public class PerfectAdapter extends RecyclerView.Adapter<PerfectAdapter.MyViewHolder>{
  144.  
  145. private LayoutInflater inflater;
  146. private Context context;
  147. private ArrayList<ImgCap> imgCapsList;
  148. private ImageView mainStream;
  149. private ImageWithCaptionListener mCallBack;
  150.  
  151. public PerfectAdapter(Context context,ArrayList<ImgCap> imgCapsList,ImageView mainStream,ImageWithCaptionListener mCallBack) {
  152. inflater = LayoutInflater.from(context);
  153. this.context = context;
  154. this.imgCapsList = imgCapsList;
  155. this.mainStream = mainStream;
  156. this.mCallBack = mCallBack;
  157. }
  158.  
  159. @Override
  160. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  161. View view = inflater.inflate(R.layout.image_item_layout, parent, false);
  162. return new MyViewHolder(view);
  163. }
  164.  
  165. @Override
  166. public void onBindViewHolder(MyViewHolder holder,final int position) {
  167. final ImgCap element = imgCapsList.get(holder.getAdapterPosition());
  168. Glide.with(context).load(element.getImagePath()).into(holder.image);
  169. Glide.with(context).load(imgCapsList.get(0).getImagePath()).into(mainStream);
  170.  
  171. holder.image.setOnClickListener(new View.OnClickListener() {
  172. @Override
  173. public void onClick(View v) {
  174. Glide.with(context).load(element.getImagePath()).into(mainStream);
  175. mCallBack.imgCaptionCallBack(position);
  176. }
  177. });
  178. }
  179.  
  180. @Override
  181. public int getItemCount() {
  182. return imgCapsList.size();
  183. }
  184.  
  185. class MyViewHolder extends RecyclerView.ViewHolder
  186. {
  187. ImageView image;
  188.  
  189. public MyViewHolder(View itemView) {
  190. super(itemView);
  191. image = (ImageView) itemView.findViewById(R.id.image);
  192. }
  193. }
  194. }
  195.  
  196. public interface ImageWithCaptionListener {
  197.  
  198. void imgCaptionCallBack(int position);
  199.  
  200. }
  201.  
  202. <RelativeLayout
  203. android:background="@android:color/white"
  204. android:padding="1dp"
  205. android:layout_width="wrap_content"
  206. android:layout_height="wrap_content">
  207.  
  208. <ImageView
  209. android:contentDescription="@string/app_name"
  210. android:id="@+id/image"
  211. android:scaleType="centerCrop"
  212. android:layout_width="100dp"
  213. android:layout_height="100dp" />
  214.  
  215. </RelativeLayout>
  216.  
  217. public class ImgCap {
  218.  
  219. private int position;
  220. private String caption;
  221. private Uri imagePath;
  222.  
  223. public ImgCap(int position, String caption, Uri imagePath) {
  224. this.position = position;
  225. this.caption = caption;
  226. this.imagePath = imagePath;
  227. }
  228.  
  229. public int getPosition() {
  230. return position;
  231. }
  232.  
  233. public String getCaption() {
  234. return caption;
  235. }
  236.  
  237. public Uri getImagePath() {
  238. return imagePath;
  239. }
  240.  
  241. public void setPosition(int position) {
  242. this.position = position;
  243. }
  244.  
  245. public void setCaption(String caption) {
  246. this.caption = caption;
  247. }
  248.  
  249. public void setImagePath(Uri imagePath) {
  250. this.imagePath = imagePath;
  251. }
  252. }
Add Comment
Please, Sign In to add comment