Advertisement
amjadArabia

recycler view

Mar 16th, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.62 KB | None | 0 0
  1. Item.java
  2. -----------------
  3. public class Item {
  4.  
  5. private String text, subText, subText01, subText02;
  6. private boolean isExpandable;
  7.  
  8. public Item(String text, String subText, String subText01, String subText02, boolean isExpandable) {
  9. this.text = text;
  10. this.subText = "Amount:" + subText;
  11. this.isExpandable = isExpandable;
  12. this.subText01 = "Address: " + subText01;
  13. this.subText02 = "Type: " + subText02;
  14.  
  15. }
  16.  
  17.  
  18. public String getSubText02() {
  19. return subText02;
  20. }
  21.  
  22. public String getSubText01() {
  23. return subText01;
  24. }
  25.  
  26. public String getText() {
  27. return text;
  28. }
  29.  
  30. public void setText(String text) {
  31. this.text = text;
  32. }
  33.  
  34. public String getSubText() {
  35. return subText;
  36. }
  37.  
  38.  
  39. public boolean isExpandable() {
  40. return isExpandable;
  41. }
  42.  
  43.  
  44. }
  45. ========================================================================================================
  46. Create InterFace:
  47.  
  48. public interface ItemClickListener {
  49.  
  50. void onClick(View view, int position, boolean isLongClick);
  51.  
  52. }
  53. ========================================================================================================
  54.  
  55. MainActivity.java
  56. ===========================
  57. import android.os.Bundle;
  58. import android.support.v7.app.AppCompatActivity;
  59. import android.support.v7.widget.LinearLayoutManager;
  60. import android.support.v7.widget.RecyclerView;
  61. import android.view.View;
  62. import android.widget.Button;
  63.  
  64. import java.util.ArrayList;
  65. import java.util.List;
  66.  
  67. public class MainActivity extends AppCompatActivity {
  68.  
  69. RecyclerView list;
  70. RecyclerView.LayoutManager layoutManager;
  71. List<Item> items = new ArrayList<>();
  72. Button button_make,button_make01;
  73.  
  74. @Override
  75. protected void onCreate(Bundle savedInstanceState) {
  76. super.onCreate(savedInstanceState);
  77. setContentView(R.layout.activity_main);
  78. button_make = findViewById(R.id.button_make);
  79. button_make01 = findViewById(R.id.button_make01);
  80.  
  81.  
  82. list = (RecyclerView) findViewById(R.id.recycler);
  83. list.setHasFixedSize(true);
  84. layoutManager = new LinearLayoutManager(this);
  85. list.setLayoutManager(layoutManager);
  86.  
  87. setPointer();
  88. }
  89.  
  90. private void setPointer() {
  91. button_make.setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View v) {
  94.  
  95. Item item = new Item("Requests for food", "5","Nazareth","Extra care" ,true);
  96. items.add(item);
  97. MyAdapter adapter = new MyAdapter(items);
  98. list.setAdapter(adapter);
  99. }
  100. });
  101. button_make01.setOnClickListener(new View.OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. Item item = new Item ("Food for pick up", "10","Haifa","Extra care",true);
  105. items.add(item);
  106. MyAdapter adapter = new MyAdapter(items);
  107. list.setAdapter(adapter);
  108. }
  109. });
  110.  
  111.  
  112. }
  113.  
  114.  
  115.  
  116. }
  117. =================================================================================================================
  118.  
  119. activity_main.xml
  120. --------------------
  121. <?xml version="1.0" encoding="utf-8"?>
  122. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  123. xmlns:app="http://schemas.android.com/apk/res-auto"
  124. xmlns:tools="http://schemas.android.com/tools"
  125. android:layout_width="match_parent"
  126. android:layout_height="match_parent"
  127. tools:context="com.arabiaexample.amjad.myapplication.MainActivity">
  128.  
  129. <android.support.v7.widget.RecyclerView
  130. android:id="@+id/recycler"
  131. android:layout_width="350dp"
  132. android:layout_height="400dp"
  133. android:layout_alignParentTop="true"
  134. android:layout_centerHorizontal="true"
  135. android:layout_marginTop="49dp"
  136. app:layout_constraintBottom_toBottomOf="parent"
  137. app:layout_constraintHorizontal_bias="1.0"
  138. app:layout_constraintLeft_toLeftOf="parent"
  139. app:layout_constraintRight_toRightOf="parent"
  140. app:layout_constraintTop_toTopOf="parent"
  141. app:layout_constraintVertical_bias="0.904">
  142.  
  143. </android.support.v7.widget.RecyclerView>
  144.  
  145. <Button
  146. android:id="@+id/button_make"
  147. android:layout_width="wrap_content"
  148. android:layout_height="50dp"
  149. android:layout_alignEnd="@+id/recycler"
  150. android:layout_alignParentBottom="true"
  151. android:layout_marginBottom="27dp"
  152. android:text="New Push for family" />
  153.  
  154. <Button
  155. android:id="@+id/button_make01"
  156. android:layout_width="wrap_content"
  157. android:layout_height="50dp"
  158.  
  159. android:layout_alignStart="@+id/recycler"
  160. android:layout_below="@+id/recycler"
  161. android:text="new push from donate" />
  162.  
  163. </RelativeLayout>
  164. ====================================================================================================
  165.  
  166. activity_with_child.xml
  167. -----------------------------
  168. <?xml version="1.0" encoding="utf-8"?>
  169. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  170. xmlns:app="http://schemas.android.com/apk/res-auto"
  171. android:layout_width="match_parent"
  172. android:layout_height="wrap_content"
  173. android:layout_marginBottom="1dp"
  174. android:background="#7986CB"
  175. android:padding="5dp">
  176.  
  177. <RelativeLayout
  178. android:id="@+id/relativelayout01"
  179. android:layout_width="40dp"
  180. android:layout_height="43dp"
  181. android:layout_alignParentRight="true"
  182. android:layout_alignParentTop="true">
  183.  
  184.  
  185. <View
  186. android:id="@+id/button1"
  187. android:layout_width="40dp"
  188. android:layout_height="40dp"
  189. android:layout_alignParentEnd="true"
  190. android:layout_centerVertical="true"
  191. android:background="@drawable/up_arrow" />
  192.  
  193.  
  194. </RelativeLayout>
  195.  
  196. <TextView
  197. android:id="@+id/textView"
  198. android:layout_width="match_parent"
  199. android:layout_height="wrap_content"
  200. android:layout_alignParentTop="true"
  201. android:layout_toLeftOf="@+id/relativelayout01"
  202. android:padding="8dp"
  203. android:text="Requests for food"
  204. android:textColor="#ffffff"
  205. android:textSize="17sp" />
  206.  
  207. <com.github.aakira.expandablelayout.ExpandableLinearLayout
  208. android:id="@+id/expandableLayout"
  209. android:layout_width="match_parent"
  210. android:layout_height="wrap_content"
  211. android:layout_below="@+id/textView"
  212. android:orientation="vertical"
  213. app:ael_duration="400"
  214. app:ael_expanded="false"
  215. app:ael_interpolator="bounce">
  216.  
  217. <TextView
  218. android:id="@+id/textViewChild"
  219. android:layout_width="match_parent"
  220. android:layout_height="wrap_content"
  221. android:layout_alignParentTop="true"
  222. android:layout_toLeftOf="@+id/relativelayout01"
  223. android:padding="8dp"
  224. android:text="this is child item 00"
  225. android:textColor="#ead6d6"
  226. android:textSize="14sp" />
  227.  
  228. <TextView
  229. android:id="@+id/textViewChild01"
  230. android:layout_width="match_parent"
  231. android:layout_height="wrap_content"
  232. android:layout_alignParentTop="true"
  233. android:layout_toLeftOf="@+id/relativelayout01"
  234. android:padding="8dp"
  235. android:text="this is child item 01"
  236. android:textColor="#ead6d6"
  237. android:textSize="14sp" />
  238.  
  239. <TextView
  240. android:id="@+id/textViewChild02"
  241. android:layout_width="match_parent"
  242. android:layout_height="wrap_content"
  243. android:layout_alignParentTop="true"
  244. android:layout_toLeftOf="@+id/relativelayout01"
  245. android:padding="8dp"
  246. android:text="this is child item 02"
  247. android:textColor="#ead6d6"
  248. android:textSize="14sp" />
  249.  
  250.  
  251. </com.github.aakira.expandablelayout.ExpandableLinearLayout>
  252.  
  253. <Button
  254. android:id="@+id/button01"
  255. android:layout_width="100dp"
  256. android:layout_height="40dp"
  257. android:layout_below="@+id/textView"
  258. android:layout_marginEnd="25dp"
  259. android:layout_toStartOf="@+id/relativelayout01"
  260. android:text="button 01"
  261. android:textAllCaps="false"
  262. android:visibility="gone" />
  263.  
  264. <Button
  265. android:id="@+id/button02"
  266. android:layout_width="100dp"
  267. android:layout_height="40dp"
  268. android:layout_alignStart="@+id/button01"
  269. android:layout_below="@+id/button01"
  270. android:text="button 02"
  271. android:textAllCaps="false"
  272. android:visibility="gone" />
  273.  
  274. <Button
  275. android:id="@+id/button03"
  276. android:layout_width="100dp"
  277. android:layout_height="40dp"
  278. android:layout_alignStart="@+id/button02"
  279. android:layout_below="@+id/button02"
  280. android:text="button 03"
  281. android:textAllCaps="false"
  282. android:visibility="gone" />
  283.  
  284.  
  285. </RelativeLayout>
  286. ============================================================================================
  287.  
  288. MyAdapter.java
  289. ------------------
  290. import android.animation.ObjectAnimator;
  291. import android.content.Context;
  292. import android.graphics.Color;
  293. import android.support.v7.widget.RecyclerView;
  294. import android.util.SparseBooleanArray;
  295. import android.view.LayoutInflater;
  296. import android.view.View;
  297. import android.view.ViewGroup;
  298. import android.widget.Button;
  299. import android.widget.RelativeLayout;
  300. import android.widget.TextView;
  301. import android.widget.Toast;
  302.  
  303. import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
  304. import com.github.aakira.expandablelayout.ExpandableLinearLayout;
  305. import com.github.aakira.expandablelayout.Utils;
  306.  
  307. import java.util.List;
  308.  
  309.  
  310.  
  311.  
  312. class MyViewHolderWithChild extends RecyclerView.ViewHolder implements View.OnClickListener{
  313.  
  314. public TextView textView, textViewChild,textViewChild01,textViewChild02;
  315. public RelativeLayout button;
  316. public Button send_messenger_button;
  317. public ExpandableLinearLayout expandableLayout;
  318. public Button button01, button02, button03;
  319. ItemClickListener itemClickListener;
  320.  
  321.  
  322. public MyViewHolderWithChild(View itemView) {
  323. super(itemView);
  324. textView = (TextView) itemView.findViewById(R.id.textView);
  325. textViewChild = (TextView) itemView.findViewById(R.id.textViewChild);
  326. textViewChild01= itemView.findViewById(R.id.textViewChild01);
  327. textViewChild02= itemView.findViewById(R.id.textViewChild02);
  328. button01 = itemView.findViewById(R.id.button01);
  329. button02 = itemView.findViewById(R.id.button02);
  330. button03 = itemView.findViewById(R.id.button03);
  331.  
  332. button = (RelativeLayout) itemView.findViewById(R.id.relativelayout01);
  333.  
  334. expandableLayout = (ExpandableLinearLayout) itemView.findViewById(R.id.expandableLayout);
  335.  
  336. itemView.setOnClickListener(this);
  337. }
  338.  
  339. public void setItemClickListener(ItemClickListener itemClickListener) {
  340. this.itemClickListener = itemClickListener;
  341. }
  342.  
  343. @Override
  344. public void onClick(View view) {
  345. itemClickListener.onClick(view,getAdapterPosition(),false);
  346. }
  347. }
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356. public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  357.  
  358. List<Item> items;
  359. Context context;
  360. SparseBooleanArray expandState = new SparseBooleanArray();
  361.  
  362. public MyAdapter(List<Item> items) {
  363. this.items = items;
  364. for (int i = 0; i < items.size(); i += 1) {
  365. expandState.append(i, false);
  366. }
  367. }
  368.  
  369. @Override
  370. public int getItemViewType(int position) {
  371. if (items.get(position).isExpandable()) {
  372. return 1;
  373. } else {
  374. return 0;
  375. }
  376. }
  377.  
  378. @Override
  379. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  380. this.context = parent.getContext();
  381.  
  382. LayoutInflater inflater = LayoutInflater.from(context);
  383. View view = inflater.inflate(R.layout.layout_with_child, parent, false);
  384. return new MyViewHolderWithChild(view);
  385.  
  386. }
  387.  
  388. @Override
  389. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
  390.  
  391. switch (holder.getItemViewType()) {
  392.  
  393. case 1: {
  394. final MyViewHolderWithChild viewHolder = (MyViewHolderWithChild) holder;
  395. final Item item = items.get(position);
  396. viewHolder.setIsRecyclable(false);
  397. viewHolder.textView.setText(item.getText());
  398.  
  399. viewHolder.expandableLayout.setInRecyclerView(true);
  400. viewHolder.expandableLayout.setExpanded(expandState.get(position));
  401. viewHolder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
  402. @Override
  403. public void onAnimationStart() {
  404.  
  405. }
  406.  
  407. @Override
  408. public void onAnimationEnd() {
  409.  
  410. }
  411.  
  412. @Override
  413. public void onPreOpen() {
  414. changeRotate((RelativeLayout) viewHolder.button, 0f, 180f).start();
  415. expandState.put(position, true);
  416. viewHolder.button01.setVisibility(View.VISIBLE);
  417. viewHolder.button02.setVisibility(View.VISIBLE);
  418. viewHolder.button03.setVisibility(View.VISIBLE);
  419. viewHolder.textView.setTextColor(Color.RED);
  420. }
  421.  
  422. @Override
  423. public void onPreClose() {
  424. changeRotate((RelativeLayout) viewHolder.button, 180f, 0f).start();
  425. expandState.put(position, false);
  426. viewHolder.button01.setVisibility(View.GONE);
  427. viewHolder.button02.setVisibility(View.GONE);
  428. viewHolder.button03.setVisibility(View.GONE);
  429. viewHolder.textView.setTextColor(Color.WHITE);
  430. }
  431.  
  432. });
  433.  
  434. viewHolder.button.setRotation(expandState.get(position) ? 180f : 0f);
  435. viewHolder.button.setOnClickListener(new View.OnClickListener() {
  436. @Override
  437. public void onClick(View v) {
  438. // Expandable child item
  439. viewHolder.expandableLayout.toggle();
  440.  
  441. }
  442. });
  443.  
  444.  
  445. viewHolder.textViewChild.setText(items.get(position).getSubText());
  446. viewHolder.textViewChild01.setText(items.get(position).getSubText01());
  447. viewHolder.textViewChild02.setText(items.get(position).getSubText02());
  448.  
  449.  
  450. viewHolder.textViewChild.setOnClickListener(new View.OnClickListener() {
  451. @Override
  452. public void onClick(View v) {
  453. Toast.makeText(context, "Child Item Click" + items.get(position).getSubText(), Toast.LENGTH_SHORT).show();
  454. }
  455. });
  456.  
  457. viewHolder.setItemClickListener(new ItemClickListener() {
  458. @Override
  459. public void onClick(View view, int position, boolean isLongClick) {
  460. Toast.makeText(context, "With child click :" + items.get(position).getText(), Toast.LENGTH_SHORT).show();
  461. }
  462. });
  463. }
  464. break;
  465. default:
  466. break;
  467. }
  468.  
  469. }
  470.  
  471. private ObjectAnimator changeRotate(RelativeLayout button, float from, float to) {
  472.  
  473. ObjectAnimator animator = ObjectAnimator.ofFloat(button, "rotation", from, to);
  474. animator.setDuration(300);
  475. animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
  476. return animator;
  477. }
  478.  
  479. @Override
  480. public int getItemCount() {
  481. return items.size();
  482. }
  483. }
  484. ========================================================================================================
  485. Create Drawable:
  486.  
  487. up_arrow.xml
  488. ------------------
  489. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  490. android:width="24dp"
  491. android:height="24dp"
  492. android:viewportWidth="24.0"
  493. android:viewportHeight="24.0">
  494. <path
  495. android:fillColor="#FF000000"
  496. android:pathData="M7.41,15.41L12,10.83l4.59,4.58L18,14l-6,-6 -6,6z"/>
  497. </vector>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement