Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gfl.linewise.ui.adapters;
- import android.content.Context;
- import android.text.TextUtils;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.alshvets.core.structure.helpers.Logger;
- import com.alshvets.core.ui.widgets.NetworkImageViewWithSpinner;
- import com.gfl.linewise.R;
- import com.gfl.linewise.api.models.Poster;
- import com.gfl.linewise.api.models.responses.ReplyOn;
- import com.gfl.linewise.api.models.responses.SoundbiteWallResponse;
- import java.util.HashMap;
- import java.util.List;
- /**
- * Created by kvarivoda on 08.30.2015.
- */
- public class SoundBiteExpandableAdapter extends BaseExpandableListAdapter {
- private Context context;
- private List<SoundbiteWallResponse> mainPosts;
- private HashMap<String, List<ReplyOn>> replyToMainPostMap;
- private String selectedMainPostId;
- private String selectedMainAccountId;
- private boolean isPostPicked;
- private OnMainPostClickListener onMainPostClickListener;
- private int selectedIndex = -1;
- private int lastSelectedIndex = -2;
- public void setSelectedIndex(int selectedIndex) {
- this.selectedIndex = selectedIndex;
- }
- public interface OnMainPostClickListener {
- void onMainPostClickListener(String id);
- }
- public SoundBiteExpandableAdapter(Context context, List<SoundbiteWallResponse> mainPosts,
- HashMap<String, List<ReplyOn>> replyToMainPostMap, OnMainPostClickListener onMainPostClickListener) {
- this.context = context;
- this.mainPosts = mainPosts;
- this.replyToMainPostMap = replyToMainPostMap;
- this.onMainPostClickListener = onMainPostClickListener;
- }
- @Override
- public ReplyOn getChild(int groupPosition, int childPosition) {
- return replyToMainPostMap.get(mainPosts.get(groupPosition).getId()).get(childPosition);
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- if (convertView == null) {
- LayoutInflater infalInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = infalInflater.inflate(R.layout.message_right,
- null);
- }
- TextView textViewItem = (TextView) convertView.findViewById(R.id.txtMessage);
- NetworkImageViewWithSpinner photo = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.photo);
- NetworkImageViewWithSpinner avatar = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.avatar);
- ReplyOn replyOn = getChild(groupPosition, childPosition);
- if (!TextUtils.isEmpty(replyOn.getComment())) {
- textViewItem.setText(replyOn.getComment());
- }
- if (!TextUtils.isEmpty(replyOn.getPhoto())) {
- photo.setVisibility(View.VISIBLE);
- photo.setImageUrl(replyOn.getPhoto());
- } else {
- photo.setVisibility(View.GONE);
- }
- Poster poster = replyOn.getReplyOn();
- if (!TextUtils.isEmpty(poster.getPhoto())) {
- avatar.setVisibility(View.VISIBLE);
- avatar.setImageUrl(poster.getPhoto());
- } else {
- avatar.setVisibility(View.GONE);
- }
- return convertView;
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- return replyToMainPostMap.get(mainPosts.get(groupPosition).getId()).size();
- }
- @Override
- public SoundbiteWallResponse getGroup(int groupPosition) {
- return mainPosts.get(groupPosition);
- }
- @Override
- public int getGroupCount() {
- return mainPosts.size();
- }
- @Override
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- ExpandableListView mExpandableListView = (ExpandableListView) parent;
- mExpandableListView.expandGroup(groupPosition);
- if (convertView == null) {
- LayoutInflater infalInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- convertView = infalInflater.inflate(R.layout.message_left,
- null);
- }
- SoundbiteWallResponse soundbiteWallResponse = getGroup(groupPosition);
- TextView txtMessage = (TextView) convertView
- .findViewById(R.id.txtMessage);
- NetworkImageViewWithSpinner photo = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.photo);
- NetworkImageViewWithSpinner avatar = (NetworkImageViewWithSpinner) convertView.findViewById(R.id.avatar);
- View clickableLayout = convertView.findViewById(R.id.clickableLayout);
- View messageLayout = convertView.findViewById(R.id.messageLayout);
- if (!TextUtils.isEmpty(soundbiteWallResponse.getComment())) {
- txtMessage.setText(soundbiteWallResponse.getComment());
- }
- if (!TextUtils.isEmpty(soundbiteWallResponse.getPhoto())) {
- photo.setVisibility(View.VISIBLE);
- photo.setImageUrl(soundbiteWallResponse.getPhoto());
- } else {
- photo.setVisibility(View.GONE);
- }
- Poster poster = soundbiteWallResponse.getPoster();
- if (!TextUtils.isEmpty(poster.getPhoto())) {
- avatar.setVisibility(View.VISIBLE);
- avatar.setImageUrl(poster.getPhoto());
- } else {
- avatar.setVisibility(View.GONE);
- }
- /*
- if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && isPostPicked){
- clickableLayout.setVisibility(View.GONE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- }
- Logger.debug(getClass(), "onClick setSelected FALSE because of lastSelected");
- } else if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && !isPostPicked){
- clickableLayout.setVisibility(View.VISIBLE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
- }
- Logger.debug(getClass(), "onClick setSelected TRUE because of lastSelected");
- } else if (selectedIndex == groupPosition) {
- clickableLayout.setVisibility(View.VISIBLE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
- }
- Logger.debug(getClass(), "onClick setSelected TRUE");
- } else {
- Logger.debug(getClass(), "onClick setSelected FALSE");
- clickableLayout.setVisibility(View.GONE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- }
- }
- */
- /*
- if(isPostPicked){
- Logger.debug(getClass(), "getGroupView isPostPicked " + isPostPicked);
- clickableLayout.setVisibility(View.VISIBLE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
- }
- // isPostPicked = true;
- Logger.debug(getClass(), "setSelected true");
- if(lastSelectedIndex == groupPosition && isPostPicked){
- // clickableLayout.setVisibility(View.GONE);
- // if (txtMessage != null) {
- // txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- // }
- // Logger.debug(getClass(), "setSelected isPostPicked false");
- // isPostPicked = false;
- } else if (selectedIndex == groupPosition) {
- clickableLayout.setVisibility(View.VISIBLE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
- }
- // isPostPicked = true;
- Logger.debug(getClass(), "setSelected true");
- }
- } else {
- clickableLayout.setVisibility(View.GONE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- }
- Logger.debug(getClass(), "setSelected false");
- // isPostPicked = false;
- }
- */
- /*if (lastSelectedIndex == groupPosition && isPostPicked) {
- clickableLayout.setVisibility(View.GONE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- }
- Logger.debug(getClass(), "setSelected isPostPicked false");
- } else */
- if (selectedIndex == groupPosition && isPostPicked) {
- clickableLayout.setVisibility(View.VISIBLE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(R.color.greyText));
- }
- Logger.debug(getClass(), "setSelected true");
- } else {
- clickableLayout.setVisibility(View.GONE);
- if (txtMessage != null) {
- txtMessage.setTextColor(context.getResources().getColor(android.R.color.black));
- }
- Logger.debug(getClass(), "setSelected false");
- }
- messageLayout.setOnClickListener(new OnPostClickListener(soundbiteWallResponse.getId(), soundbiteWallResponse.getPoster().getAccountId(), groupPosition));
- return convertView;
- }
- @Override
- public boolean hasStableIds() {
- return false;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return true;
- }
- public String getSelectedMainPostId() {
- return selectedMainPostId;
- }
- public void setSelectedMainPostId(String selectedMainPostId) {
- this.selectedMainPostId = selectedMainPostId;
- }
- public String getSelectedMainAccountId() {
- return selectedMainAccountId;
- }
- public void setSelectedMainAccountId(String selectedMainAccountId) {
- this.selectedMainAccountId = selectedMainAccountId;
- }
- public boolean isPostPicked() {
- return isPostPicked;
- }
- public void setIsPostPicked(boolean isPostPicked) {
- this.isPostPicked = isPostPicked;
- }
- private class OnPostClickListener implements View.OnClickListener {
- private String id;
- private String accountId;
- private int groupPosition;
- private OnPostClickListener(String id, String accountId, int groupPosition) {
- this.id = id;
- this.accountId = accountId;
- this.groupPosition = groupPosition;
- }
- @Override
- public void onClick(View v) {
- Logger.debug(getClass(), "adapter onClick ");
- setSelectedIndex(groupPosition);
- /*
- // for(int i = 0; i<=1; i ++){
- if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && !isPostPicked){
- isPostPicked = true;
- Logger.debug(getClass(), "onClick setSelected TRUE because of lastSelected");
- } else if(lastSelectedIndex == groupPosition && selectedIndex == groupPosition && isPostPicked){
- isPostPicked = true;
- Logger.debug(getClass(), "onClick setSelected FALSE because of lastSelected");
- } else if (selectedIndex == groupPosition && !isPostPicked) {
- isPostPicked = true;
- Logger.debug(getClass(), "onClick setSelected TRUE");
- } else {
- isPostPicked = false;
- Logger.debug(getClass(), "onClick setSelected FALSE");
- }
- // }
- Logger.debug(getClass(), "onClick final: setSelected " + isPostPicked);
- */
- SelectedObject selectedObject = new SelectedObject(lastSelectedIndex);
- selectedObject.setLastSelectedIndex(lastSelectedIndex);
- isPostPicked = calculateIfPostClicked(groupPosition, selectedObject);
- // lastSelectedIndex = groupPosition;
- lastSelectedIndex = selectedObject.getLastSelectedIndex();
- selectedMainPostId = id;
- selectedMainAccountId = accountId;
- if (onMainPostClickListener != null) {
- onMainPostClickListener.onMainPostClickListener(id);
- }
- notifyDataSetChanged();
- Toast.makeText(context, "isPicked?? " + isPostPicked, Toast.LENGTH_LONG).show();
- Logger.debug(getClass(), "OnPostClickListener: OK");
- }
- }
- private boolean calculateIfPostClicked(int groupPosition, SelectedObject selectedObject) {
- if (lastSelectedIndex == -2 && !isPostPicked) {
- return true;
- } else if (lastSelectedIndex == groupPosition && isPostPicked) {
- selectedObject.setLastSelectedIndex(-2);
- return false;
- } else {
- return false;
- }
- }
- private class SelectedObject {
- int lastSelectedIndex = -2;
- public SelectedObject(int lastSelectedIndex) {
- this.lastSelectedIndex = lastSelectedIndex;
- }
- public int getLastSelectedIndex() {
- return lastSelectedIndex;
- }
- public void setLastSelectedIndex(int lastSelectedIndex) {
- this.lastSelectedIndex = lastSelectedIndex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement