Advertisement
Kvarz

congrats, single & cancalable choice mode in expandableListA

Aug 31st, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.07 KB | None | 0 0
  1. package com.gfl.linewise.ui.adapters;
  2.  
  3. import android.content.Context;
  4. import android.text.TextUtils;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseExpandableListAdapter;
  9. import android.widget.ExpandableListView;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.alshvets.core.structure.helpers.Logger;
  14. import com.alshvets.core.ui.widgets.NetworkImageViewWithSpinner;
  15. import com.gfl.linewise.R;
  16. import com.gfl.linewise.api.models.Poster;
  17. import com.gfl.linewise.api.models.responses.ReplyOn;
  18. import com.gfl.linewise.api.models.responses.SoundbiteWallResponse;
  19.  
  20. import java.util.HashMap;
  21. import java.util.List;
  22.  
  23. /**
  24. * Created by kvarivoda on 08.30.2015.
  25. */
  26. public class SoundBiteExpandableAdapter extends BaseExpandableListAdapter {
  27.  
  28. private Context context;
  29. private List<SoundbiteWallResponse> mainPosts;
  30. private HashMap<String, List<ReplyOn>> replyToMainPostMap;
  31.  
  32. private String selectedMainPostId;
  33. private String selectedMainAccountId;
  34.  
  35. private boolean isPostPicked;
  36.  
  37. private OnMainPostClickListener onMainPostClickListener;
  38.  
  39. private int selectedIndex = -1;
  40. private int lastSelectedIndex = -2;
  41.  
  42. public void setSelectedIndex(int selectedIndex) {
  43. this.selectedIndex = selectedIndex;
  44. }
  45.  
  46. public interface OnMainPostClickListener {
  47. void onMainPostClickListener(String id);
  48. }
  49.  
  50. public SoundBiteExpandableAdapter(Context context, List<SoundbiteWallResponse> mainPosts,
  51. HashMap<String, List<ReplyOn>> replyToMainPostMap, OnMainPostClickListener onMainPostClickListener) {
  52. this.context = context;
  53. this.mainPosts = mainPosts;
  54. this.replyToMainPostMap = replyToMainPostMap;
  55. this.onMainPostClickListener = onMainPostClickListener;
  56. }
  57.  
  58.  
  59. @Override
  60. public ReplyOn getChild(int groupPosition, int childPosition) {
  61. return replyToMainPostMap.get(mainPosts.get(groupPosition).getId()).get(childPosition);
  62. }
  63.  
  64. @Override
  65. public long getChildId(int groupPosition, int childPosition) {
  66. return childPosition;
  67. }
  68.  
  69. @Override
  70. public View getChildView(int groupPosition, int childPosition,
  71. boolean isLastChild, View convertView, ViewGroup parent) {
  72.  
  73. if (convertView == null) {
  74. LayoutInflater infalInflater = (LayoutInflater) context
  75. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  76. convertView = infalInflater.inflate(R.layout.message_right,
  77. null);
  78. }
  79.  
  80. TextView textViewItem = (TextView) convertView.findViewById(R.id.txtMessage);
  81.  
  82. NetworkImageViewWithSpinner photo = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.photo);
  83. NetworkImageViewWithSpinner avatar = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.avatar);
  84.  
  85. ReplyOn replyOn = getChild(groupPosition, childPosition);
  86.  
  87. if (!TextUtils.isEmpty(replyOn.getComment())) {
  88. textViewItem.setText(replyOn.getComment());
  89. }
  90. if (!TextUtils.isEmpty(replyOn.getPhoto())) {
  91. photo.setVisibility(View.VISIBLE);
  92. photo.setImageUrl(replyOn.getPhoto());
  93. } else {
  94. photo.setVisibility(View.GONE);
  95. }
  96.  
  97. Poster poster = replyOn.getReplyOn();
  98. if (!TextUtils.isEmpty(poster.getPhoto())) {
  99. avatar.setVisibility(View.VISIBLE);
  100. avatar.setImageUrl(poster.getPhoto());
  101. } else {
  102. avatar.setVisibility(View.GONE);
  103. }
  104.  
  105. return convertView;
  106. }
  107.  
  108. @Override
  109. public int getChildrenCount(int groupPosition) {
  110. return replyToMainPostMap.get(mainPosts.get(groupPosition).getId()).size();
  111. }
  112.  
  113. @Override
  114. public SoundbiteWallResponse getGroup(int groupPosition) {
  115. return mainPosts.get(groupPosition);
  116. }
  117.  
  118. @Override
  119. public int getGroupCount() {
  120. return mainPosts.size();
  121. }
  122.  
  123. @Override
  124. public long getGroupId(int groupPosition) {
  125. return groupPosition;
  126. }
  127.  
  128. @Override
  129. public View getGroupView(int groupPosition, boolean isExpanded,
  130. View convertView, ViewGroup parent) {
  131.  
  132. ExpandableListView mExpandableListView = (ExpandableListView) parent;
  133. mExpandableListView.expandGroup(groupPosition);
  134.  
  135. if (convertView == null) {
  136. LayoutInflater infalInflater = (LayoutInflater) context
  137. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  138. convertView = infalInflater.inflate(R.layout.message_left,
  139. null);
  140. }
  141.  
  142. SoundbiteWallResponse soundbiteWallResponse = getGroup(groupPosition);
  143.  
  144. TextView txtMessage = (TextView) convertView
  145. .findViewById(R.id.txtMessage);
  146.  
  147. NetworkImageViewWithSpinner photo = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.photo);
  148.  
  149. NetworkImageViewWithSpinner avatar = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.avatar);
  150. View clickableLayout = convertView.findViewById(R.id.clickableLayout);
  151.  
  152. View messageLayout = convertView.findViewById(R.id.messageLayout);
  153.  
  154. if (!TextUtils.isEmpty(soundbiteWallResponse.getComment())) {
  155. txtMessage.setText(soundbiteWallResponse.getComment());
  156. }
  157. if (!TextUtils.isEmpty(soundbiteWallResponse.getPhoto())) {
  158. photo.setVisibility(View.VISIBLE);
  159. photo.setImageUrl(soundbiteWallResponse.getPhoto());
  160. } else {
  161. photo.setVisibility(View.GONE);
  162. }
  163.  
  164. Poster poster = soundbiteWallResponse.getPoster();
  165. if (!TextUtils.isEmpty(poster.getPhoto())) {
  166. avatar.setVisibility(View.VISIBLE);
  167. avatar.setImageUrl(poster.getPhoto());
  168. } else {
  169. avatar.setVisibility(View.GONE);
  170. }
  171. /*
  172.  
  173. if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && isPostPicked){
  174. clickableLayout.setVisibility(View.GONE);
  175. if (txtMessage != null) {
  176. txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  177. }
  178. Logger.debug(getClass(), "onClick setSelected FALSE because of lastSelected");
  179. } else if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && !isPostPicked){
  180. clickableLayout.setVisibility(View.VISIBLE);
  181. if (txtMessage != null) {
  182. txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
  183. }
  184. Logger.debug(getClass(), "onClick setSelected TRUE because of lastSelected");
  185. } else if (selectedIndex == groupPosition) {
  186. clickableLayout.setVisibility(View.VISIBLE);
  187. if (txtMessage != null) {
  188. txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
  189. }
  190. Logger.debug(getClass(), "onClick setSelected TRUE");
  191. } else {
  192. Logger.debug(getClass(), "onClick setSelected FALSE");
  193. clickableLayout.setVisibility(View.GONE);
  194. if (txtMessage != null) {
  195. txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  196. }
  197. }
  198.  
  199. */
  200.  
  201. /*
  202.  
  203. if(isPostPicked){
  204. Logger.debug(getClass(), "getGroupView isPostPicked " + isPostPicked);
  205.  
  206. clickableLayout.setVisibility(View.VISIBLE);
  207. if (txtMessage != null) {
  208. txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
  209. }
  210. // isPostPicked = true;
  211. Logger.debug(getClass(), "setSelected true");
  212.  
  213.  
  214. if(lastSelectedIndex == groupPosition && isPostPicked){
  215. // clickableLayout.setVisibility(View.GONE);
  216. // if (txtMessage != null) {
  217. // txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  218. // }
  219. // Logger.debug(getClass(), "setSelected isPostPicked false");
  220. // isPostPicked = false;
  221. } else if (selectedIndex == groupPosition) {
  222. clickableLayout.setVisibility(View.VISIBLE);
  223. if (txtMessage != null) {
  224. txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
  225. }
  226. // isPostPicked = true;
  227. Logger.debug(getClass(), "setSelected true");
  228. }
  229. } else {
  230. clickableLayout.setVisibility(View.GONE);
  231. if (txtMessage != null) {
  232. txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  233. }
  234. Logger.debug(getClass(), "setSelected false");
  235. // isPostPicked = false;
  236. }
  237.  
  238. */
  239.  
  240. /*if (lastSelectedIndex == groupPosition && isPostPicked) {
  241. clickableLayout.setVisibility(View.GONE);
  242. if (txtMessage != null) {
  243. txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  244. }
  245. Logger.debug(getClass(), "setSelected isPostPicked false");
  246. } else */
  247. if (selectedIndex == groupPosition && isPostPicked) {
  248. clickableLayout.setVisibility(View.VISIBLE);
  249. if (txtMessage != null) {
  250. txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
  251. }
  252. Logger.debug(getClass(), "setSelected true");
  253. } else {
  254. clickableLayout.setVisibility(View.GONE);
  255. if (txtMessage != null) {
  256. txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
  257. }
  258. Logger.debug(getClass(), "setSelected false");
  259. }
  260.  
  261. messageLayout.setOnClickListener(new OnPostClickListener(soundbiteWallResponse.getId(), soundbiteWallResponse.getPoster().getAccountId(), groupPosition));
  262.  
  263.  
  264. return convertView;
  265. }
  266.  
  267. @Override
  268. public boolean hasStableIds() {
  269. return false;
  270. }
  271.  
  272. @Override
  273. public boolean isChildSelectable(int groupPosition, int childPosition) {
  274. return true;
  275. }
  276.  
  277. public String getSelectedMainPostId() {
  278. return selectedMainPostId;
  279. }
  280.  
  281. public void setSelectedMainPostId(String selectedMainPostId) {
  282. this.selectedMainPostId = selectedMainPostId;
  283. }
  284.  
  285. public String getSelectedMainAccountId() {
  286. return selectedMainAccountId;
  287. }
  288.  
  289. public void setSelectedMainAccountId(String selectedMainAccountId) {
  290. this.selectedMainAccountId = selectedMainAccountId;
  291. }
  292.  
  293. public boolean isPostPicked() {
  294. return isPostPicked;
  295. }
  296.  
  297. public void setIsPostPicked(boolean isPostPicked) {
  298. this.isPostPicked = isPostPicked;
  299. }
  300.  
  301. private class OnPostClickListener implements View.OnClickListener {
  302.  
  303. private String id;
  304. private String accountId;
  305. private int groupPosition;
  306.  
  307. private OnPostClickListener(String id, String accountId, int groupPosition) {
  308. this.id = id;
  309. this.accountId = accountId;
  310. this.groupPosition = groupPosition;
  311. }
  312.  
  313. @Override
  314. public void onClick(View v) {
  315. Logger.debug(getClass(), "adapter onClick ");
  316. setSelectedIndex(groupPosition);
  317. /*
  318.  
  319. // for(int i = 0; i<=1; i ++){
  320. if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && !isPostPicked){
  321.  
  322. isPostPicked = true;
  323. Logger.debug(getClass(), "onClick setSelected TRUE because of lastSelected");
  324. } else if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && isPostPicked){
  325.  
  326. isPostPicked = true;
  327. Logger.debug(getClass(), "onClick setSelected FALSE because of lastSelected");
  328. } else if (selectedIndex == groupPosition && !isPostPicked) {
  329.  
  330. isPostPicked = true;
  331. Logger.debug(getClass(), "onClick setSelected TRUE");
  332. } else {
  333. isPostPicked = false;
  334. Logger.debug(getClass(), "onClick setSelected FALSE");
  335. }
  336. // }
  337. Logger.debug(getClass(), "onClick final: setSelected " + isPostPicked);
  338. */
  339.  
  340.  
  341. SelectedObject selectedObject = new SelectedObject(lastSelectedIndex);
  342. selectedObject.setLastSelectedIndex(lastSelectedIndex);
  343. isPostPicked = calculateIfPostClicked(groupPosition, selectedObject);
  344.  
  345. // lastSelectedIndex = groupPosition;
  346. lastSelectedIndex = selectedObject.getLastSelectedIndex();
  347.  
  348. selectedMainPostId = id;
  349. selectedMainAccountId = accountId;
  350. if (onMainPostClickListener != null) {
  351. onMainPostClickListener.onMainPostClickListener(id);
  352. }
  353.  
  354. notifyDataSetChanged();
  355.  
  356. Toast.makeText(context, "isPicked?? " + isPostPicked, Toast.LENGTH_LONG).show();
  357.  
  358. Logger.debug(getClass(), "OnPostClickListener: OK");
  359. }
  360. }
  361.  
  362. private boolean calculateIfPostClicked(int groupPosition, SelectedObject selectedObject) {
  363. if (lastSelectedIndex == -2 && !isPostPicked) {
  364. return true;
  365. } else if (lastSelectedIndex == groupPosition && isPostPicked) {
  366. selectedObject.setLastSelectedIndex(-2);
  367. return false;
  368. } else {
  369. return false;
  370. }
  371.  
  372. }
  373.  
  374. private class SelectedObject {
  375. int lastSelectedIndex = -2;
  376.  
  377. public SelectedObject(int lastSelectedIndex) {
  378. this.lastSelectedIndex = lastSelectedIndex;
  379. }
  380.  
  381. public int getLastSelectedIndex() {
  382. return lastSelectedIndex;
  383. }
  384.  
  385. public void setLastSelectedIndex(int lastSelectedIndex) {
  386. this.lastSelectedIndex = lastSelectedIndex;
  387. }
  388. }
  389.  
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement