Guest User

Untitled

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. The top two items are card views and the third item is a list of hotels.
  2.  
  3. public class **MainActivity** extends AppCompatActivity {
  4. @Override
  5. protected void onCreate(@Nullable Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. RecyclerView recyclerView = findViewById(R.id.recycler_view);
  9. ArrayList<DataModel> items = new ArrayList<>();
  10. items.add(new DataModel("Bus"));
  11. items.add(new DataModel("Flight"));
  12. ArrayList<String> hotelsList=new ArrayList<>();
  13. hotelsList.add("Hotel 1");
  14. hotelsList.add("Hotel 2");
  15. hotelsList.add("Hotel 2");
  16. items.add(new DataModel("Hotels",hotelsList));
  17. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
  18. linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  19. recyclerView.setLayoutManager(linearLayoutManager);
  20. ParentAdapter parentAdapter = new ParentAdapter(items,MainActivity.this);
  21. recyclerView.setAdapter(parentAdapter);
  22. }
  23. }
  24.  
  25. <?xml version="1.0" encoding="utf-8"?>
  26. <RelativeLayout
  27. xmlns:android="http://schemas.android.com/apk/res/android"
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent">
  30.  
  31. <android.support.v7.widget.RecyclerView
  32. android:id="@+id/recycler_view"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"/>
  35.  
  36. </RelativeLayout>
  37.  
  38. public class DataModel {
  39.  
  40. private String title;
  41. private ArrayList<String> hotels;
  42.  
  43. public DataModel(String title){
  44. this.title=title;
  45. }
  46.  
  47. public DataModel(String title, ArrayList<String> hotels){
  48. this.title=title;
  49. this.hotels=hotels;
  50. }
  51.  
  52. public String getTitle() {
  53. return title;
  54. }
  55.  
  56. public ArrayList<String> getHotels() {
  57. return hotels;
  58. }
  59.  
  60. }
  61.  
  62. public class ParentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  63.  
  64. private ArrayList<DataModel> items;
  65. private Context mContext;
  66.  
  67. public ParentAdapter(ArrayList<DataModel> items, Context mContext) {
  68. this.items = items;
  69. this.mContext=mContext;
  70. }
  71.  
  72.  
  73. @Override
  74. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  75. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  76. if (viewType == ViewType.NORMAL_ITEM) {
  77. return new NormalHolder(layoutInflater.inflate(R.layout.normal_view, parent, false));
  78. } else if (viewType == ViewType.HOTELS_LIST_ITEM) {
  79. return new ListHolder(layoutInflater.inflate(R.layout.list_view, parent, false));
  80. }
  81. return null;
  82. }
  83.  
  84.  
  85. @Override
  86. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  87. int cardType = getItemViewType(position);
  88. switch (cardType) {
  89. case ViewType.NORMAL_ITEM:
  90. NormalHolder normalHolder=(NormalHolder) holder;
  91. normalHolder.title.setText(items.get(position).getTitle());
  92. break;
  93. case ViewType.HOTELS_LIST_ITEM:
  94. ListHolder listHolder=(ListHolder) holder;
  95. HotelCardAdapter hotelCardAdapter = new HotelCardAdapter(mContext, items.get(position).getHotels());
  96. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
  97. linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
  98. listHolder.hotelsList.setLayoutManager(linearLayoutManager);
  99. listHolder.hotelsList.setAdapter(hotelCardAdapter);
  100. break;
  101. }
  102. }
  103.  
  104. private interface ViewType {
  105. int NORMAL_ITEM = 0;
  106. int HOTELS_LIST_ITEM = 1;
  107. }
  108.  
  109. @Override
  110. public int getItemViewType(int position) {
  111. if (position == 0 || position == 1) {
  112. return ViewType.NORMAL_ITEM;
  113. } else return ViewType.HOTELS_LIST_ITEM;
  114. }
  115.  
  116. @Override
  117. public int getItemCount() {
  118. return items.size();
  119. }
  120.  
  121. private class NormalHolder extends RecyclerView.ViewHolder {
  122. TextView title;
  123. public NormalHolder(View inflate) {
  124. super(inflate);
  125. title=inflate.findViewById(R.id.title);
  126. }
  127. }
  128.  
  129. private class ListHolder extends RecyclerView.ViewHolder {
  130. RecyclerView hotelsList;
  131.  
  132. public ListHolder(View inflate) {
  133. super(inflate);
  134. hotelsList = inflate.findViewById(R.id.hotel_list);
  135. }
  136. }
  137. }
  138.  
  139. <?xml version="1.0" encoding="utf-8"?>
  140. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  141. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  142. android:layout_width="match_parent"
  143. android:layout_height="wrap_content"
  144. android:layout_marginBottom="5dp"
  145. android:layout_marginTop="5dp"
  146. card_view:cardCornerRadius="2dp"
  147. card_view:contentPadding="10dp">
  148.  
  149. <RelativeLayout
  150. android:layout_width="match_parent"
  151. android:layout_height="100dp"
  152. android:padding="6dp">
  153.  
  154.  
  155. <TextView
  156. android:id="@+id/title"
  157. android:layout_width="wrap_content"
  158. android:layout_height="wrap_content"
  159. android:text="Bus"
  160. android:textColor="@color/colorPrimary"
  161. android:textSize="20sp"/>
  162.  
  163. <Button
  164. android:id="@+id/view_all"
  165. android:layout_width="wrap_content"
  166. android:layout_height="wrap_content"
  167. android:layout_alignParentEnd="true"
  168. android:layout_alignParentRight="true"
  169. android:gravity="center"
  170. android:text="View All"
  171. android:textSize="12sp"/>
  172.  
  173.  
  174. </RelativeLayout>
  175.  
  176. </android.support.v7.widget.CardView>
  177.  
  178. <?xml version="1.0" encoding="utf-8"?>
  179. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  180. xmlns:card_view="http://schemas.android.com/apk/res-auto"
  181. android:layout_width="match_parent"
  182. android:layout_height="wrap_content"
  183. android:layout_marginBottom="5dp"
  184. android:layout_marginTop="5dp"
  185. card_view:cardCornerRadius="2dp"
  186. card_view:contentPadding="10dp">
  187.  
  188. <RelativeLayout
  189. android:layout_width="match_parent"
  190. android:layout_height="wrap_content"
  191. android:padding="6dp">
  192.  
  193. <TextView
  194. android:id="@+id/header"
  195. android:layout_width="wrap_content"
  196. android:layout_height="wrap_content"
  197. android:text="Hotels"
  198. android:textColor="@color/colorPrimary"
  199. android:textSize="17sp"/>
  200.  
  201. <android.support.v7.widget.RecyclerView
  202. android:id="@+id/hotel_list"
  203. android:layout_width="match_parent"
  204. android:layout_height="wrap_content"
  205. android:layout_below="@+id/header"
  206. android:layout_marginTop="12dp"
  207. android:orientation="horizontal"/>
  208.  
  209.  
  210. </RelativeLayout>
  211.  
  212. </android.support.v7.widget.CardView>
  213.  
  214. public class HotelCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  215.  
  216. private ArrayList<String> hotelsObject;
  217. private Context context;
  218.  
  219. public HotelCardAdapter(Context context, ArrayList<String> hotelsObject) {
  220. this.hotelsObject = hotelsObject;
  221. this.context = context;
  222.  
  223. }
  224.  
  225. @Override
  226. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  227. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  228. return new HotelCarouselHolder(layoutInflater.inflate(R.layout.hotel_carousel_item, null));
  229. }
  230.  
  231. @Override
  232. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  233. HotelCarouselHolder hotelCarouselHolder = (HotelCarouselHolder) holder;
  234. hotelCarouselHolder.title.setText(hotelsObject.get(position));
  235. }
  236.  
  237. @Override
  238. public int getItemCount() {
  239. return hotelsObject.size();
  240. }
  241.  
  242. private class HotelCarouselHolder extends RecyclerView.ViewHolder {
  243. private TextView title;
  244.  
  245. public HotelCarouselHolder(View inflate) {
  246. super(inflate);
  247. title = inflate.findViewById(R.id.title);
  248. }
  249. }
  250. }
  251.  
  252. <?xml version="1.0" encoding="utf-8"?>
  253. <RelativeLayout
  254. xmlns:android="http://schemas.android.com/apk/res/android"
  255. android:layout_width="match_parent"
  256. android:layout_height="match_parent"
  257. android:padding="16dp">
  258.  
  259. <RelativeLayout
  260. android:id="@+id/hotel_details_layout"
  261. android:layout_width="match_parent"
  262. android:layout_height="wrap_content">
  263.  
  264. <ImageView
  265. android:id="@+id/hotel_image"
  266. android:layout_width="256dp"
  267. android:layout_height="144dp"
  268. android:scaleType="centerCrop"
  269. android:src="#999999"/>
  270.  
  271. <TextView
  272. android:id="@+id/title"
  273. android:layout_width="wrap_content"
  274. android:layout_height="wrap_content"
  275. android:layout_below="@+id/hotel_image"
  276. android:textSize="16sp"
  277. android:text="Hotels 1"/>
  278.  
  279. </RelativeLayout>
  280. </RelativeLayout>
Add Comment
Please, Sign In to add comment