Guest User

Untitled

a guest
Jan 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
  2.  
  3.  
  4. private Context mCtx;
  5.  
  6. private List<Product> productList;
  7.  
  8. public ProductAdapter(Context mCtx, List<Product> productList) {
  9. this.mCtx = mCtx;
  10. this.productList = productList;
  11. }
  12.  
  13. private Fragment fragment;
  14.  
  15.  
  16. @Override
  17. public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  18. LayoutInflater inflater = LayoutInflater.from(mCtx);
  19. View view = inflater.inflate(R.layout.list_layout, null);
  20. return new ProductViewHolder(view);
  21. }
  22.  
  23. @Override
  24. public void onBindViewHolder(ProductViewHolder holder, int position) {
  25. Product product = productList.get(position);
  26.  
  27. holder.textViewTitle.setText(product.getTitle());
  28.  
  29. holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
  30.  
  31. }
  32.  
  33.  
  34. @Override
  35. public int getItemCount() {
  36. return productList.size();
  37. }
  38.  
  39.  
  40. public class ProductViewHolder extends RecyclerView.ViewHolder {
  41. ImageView imageView;
  42. TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
  43.  
  44. Button buttonImage;
  45.  
  46. public ImageView getImageView() {
  47. return imageView;
  48. }
  49.  
  50. public void setImageView(ImageView imageView) {
  51. this.imageView = imageView;
  52. }
  53.  
  54. public ProductViewHolder(final View itemView) {
  55. super(itemView);
  56.  
  57. textViewTitle = itemView.findViewById(R.id.textViewTitle);
  58.  
  59. imageView = itemView.findViewById(R.id.imageView);
  60.  
  61. buttonImage = itemView.findViewById(R.id.buttonImage);
  62.  
  63.  
  64. buttonImage.setOnClickListener(new View.OnClickListener() {
  65. @Override
  66. public void onClick(View view) {
  67.  
  68.  
  69. FragmentBigPicture fragmentBigPicture = new FragmentBigPicture();
  70. FragmentManager fragmentManager = ((Activity) mCtx).getFragmentManager();
  71.  
  72. fragmentManager.beginTransaction().replace(R.id.container, fragment)
  73. .commit();
  74.  
  75.  
  76. }
  77. });
  78. }
  79.  
  80.  
  81. }
  82.  
  83. public class Product {
  84. //private int id;
  85. private String title;
  86. /* private String shortdesc;
  87. private double rating;
  88. private double price;*/
  89. private int image;
  90.  
  91. public String getTitle() {
  92. return title;
  93. }
  94.  
  95. public Product(int image) {
  96. this.image = image;
  97. }
  98.  
  99. public Product(String title, int image) {
  100. this.title = title;
  101. this.image = image;
  102. }
  103.  
  104. public int getImage() {
  105. return image;
  106. }}
  107.  
  108. public class FragmentImport extends Fragment {
  109. // TODO: Rename parameter arguments, choose names that match
  110. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  111. private static final String ARG_PARAM1 = "param1";
  112. private static final String ARG_PARAM2 = "param2";
  113.  
  114. // TODO: Rename and change types of parameters
  115. private String mParam1;
  116. private String mParam2;
  117. List<Product> productList;
  118.  
  119. //the recyclerview
  120. RecyclerView recyclerView;
  121.  
  122.  
  123.  
  124. private OnFragmentInteractionListener mListener;
  125.  
  126. public FragmentImport() {
  127. // Required empty public constructor
  128. }
  129.  
  130. /**
  131. * Use this factory method to create a new instance of
  132. * this fragment using the provided parameters.
  133. *
  134. * @param param1 Parameter 1.
  135. * @param param2 Parameter 2.
  136. * @return A new instance of fragment FragmentImport.
  137. */
  138. // TODO: Rename and change types and number of parameters
  139. public static FragmentImport newInstance(String param1, String param2) {
  140. FragmentImport fragment = new FragmentImport();
  141. Bundle args = new Bundle();
  142. args.putString(ARG_PARAM1, param1);
  143. args.putString(ARG_PARAM2, param2);
  144. fragment.setArguments(args);
  145. return fragment;
  146. }
  147.  
  148. @Override
  149. public void onCreate(Bundle savedInstanceState) {
  150. super.onCreate(savedInstanceState);
  151. if (getArguments() != null) {
  152. mParam1 = getArguments().getString(ARG_PARAM1);
  153. mParam2 = getArguments().getString(ARG_PARAM2);
  154.  
  155. }
  156. }
  157.  
  158.  
  159. @Override
  160. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  161. Bundle savedInstanceState) {
  162.  
  163.  
  164.  
  165.  
  166. View view = inflater.inflate(R.layout.fragment_import, container, false);
  167. recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
  168. recyclerView.setHasFixedSize(true);
  169. recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  170.  
  171.  
  172. productList = new ArrayList<>();
  173.  
  174.  
  175. productList.add(
  176. new Product("Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)",
  177. R.drawable.macbook));
  178.  
  179. productList.add(
  180. new Product(
  181. "Dell Inspiron 7000 Core i5 7th Gen - (8 GB/1 TB HDD/Windows 10 Home)",
  182. R.drawable.dellinspiron));
  183.  
  184. productList.add(
  185. new Product(
  186. "Microsoft Surface Pro 4 Core m3 6th Gen - (4 GB/128 GB SSD/Windows 10)",
  187. R.drawable.surface));
  188.  
  189. ProductAdapter adapter = new ProductAdapter(getActivity(), productList);
  190.  
  191.  
  192. recyclerView.setAdapter(adapter);
  193.  
  194. return view;
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. @Override
  203. public void onDetach() {
  204. super.onDetach();
  205. mListener = null;
  206. }}
  207.  
  208. /**
  209. * This interface must be implemented by activities that contain this
  210. * fragment to allow an interaction in this fragment to be communicated
  211. * to the activity and potentially other fragments contained in that
  212. * activity.
  213. * <p>
  214. * See the Android Training lesson <a href=
  215. * "http://developer.android.com/training/basics/fragments/communicating.html"
  216. * >Communicating with Other Fragments</a> for more information.
  217. */
  218. interface OnFragmentInteractionListener {
  219. // TODO: Update argument type and name
  220. void onFragmentInteraction(Uri uri);
  221. }
  222.  
  223. public class FragmentBigPicture extends Fragment {
  224. // TODO: Rename parameter arguments, choose names that match
  225. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  226. private static final String ARG_PARAM1 = "param1";
  227. private static final String ARG_PARAM2 = "param2";
  228.  
  229. // TODO: Rename and change types of parameters
  230. private String mParam1;
  231. private String mParam2;
  232. ImageView imageView2;
  233.  
  234.  
  235. private OnFragmentInteractionListener mListener;
  236.  
  237. public FragmentBigPicture() {
  238. // Required empty public constructor
  239. }
  240.  
  241. /**
  242. * Use this factory method to create a new instance of
  243. * this fragment using the provided parameters.
  244. *
  245. * @param param1 Parameter 1.
  246. * @param param2 Parameter 2.
  247. * @return A new instance of fragment FragmentBigPicture.
  248. */
  249. // TODO: Rename and change types and number of parameters
  250. public static FragmentBigPicture newInstance(String param1, String param2) {
  251. FragmentBigPicture fragment = new FragmentBigPicture();
  252. Bundle args = new Bundle();
  253. args.putString(ARG_PARAM1, param1);
  254. args.putString(ARG_PARAM2, param2);
  255. fragment.setArguments(args);
  256. return fragment;
  257. }
  258.  
  259. @Override
  260. public void onCreate(Bundle savedInstanceState) {
  261. super.onCreate(savedInstanceState);
  262. if (getArguments() != null) {
  263. mParam1 = getArguments().getString(ARG_PARAM1);
  264. mParam2 = getArguments().getString(ARG_PARAM2);
  265. }
  266. }
  267.  
  268. @Override
  269. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  270. Bundle savedInstanceState) {
  271. // Inflate the layout for this fragment
  272. View view = inflater.inflate(R.layout.fragment_big_picture, container, false);
  273. imageView2 = (ImageView) view.findViewById(R.id.imageView2);
  274. FragmentImport fImport = new FragmentImport();
  275.  
  276.  
  277. return view;
  278. }
  279.  
  280.  
  281. // TODO: Rename method, update argument and hook method into UI event
  282. public void onButtonPressed(Uri uri) {
  283. if (mListener != null) {
  284. mListener.onFragmentInteraction(uri);
  285. }
  286. }
  287.  
  288.  
  289. @Override
  290. public void onDetach() {
  291. super.onDetach();
  292. mListener = null;
  293. }
  294.  
  295. /**
  296. * This interface must be implemented by activities that contain this
  297. * fragment to allow an interaction in this fragment to be communicated
  298. * to the activity and potentially other fragments contained in that
  299. * activity.
  300. * <p>
  301. * See the Android Training lesson <a href=
  302. * "http://developer.android.com/training/basics/fragments/communicating.html"
  303. * >Communicating with Other Fragments</a> for more information.
  304. */
  305. public interface OnFragmentInteractionListener {
  306. // TODO: Update argument type and name
  307. void onFragmentInteraction(Uri uri);
  308. }}
Add Comment
Please, Sign In to add comment