Created
July 25, 2014 10:32
-
-
Save omarmiatello/19eb7b57032e9f91a893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.neosperience.egea; | |
| import android.os.Bundle; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.view.GestureDetectorCompat; | |
| import android.text.Html; | |
| import android.util.Log; | |
| import android.view.GestureDetector; | |
| import android.view.LayoutInflater; | |
| import android.view.MotionEvent; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.LinearLayout; | |
| import android.widget.ScrollView; | |
| import android.widget.TextView; | |
| import com.android.volley.toolbox.NetworkImageView; | |
| import com.neosperience.egea.customview.ViewAnimation; | |
| import com.neosperience.egea.utils.concurrency.ObservableRegisterer; | |
| import com.neosperience.egea.utils.concurrency.Observer; | |
| import com.neosperience.egea.utils.volleytoolbox.ImageCacheManager; | |
| public class NewsDetailFragment extends Fragment implements Observer { | |
| private NetworkImageView news_image; | |
| private int news_image_height; | |
| private ScrollView scrollView; | |
| public NewsDetailFragment() { | |
| // Empty constructor required for fragment subclasses | |
| } | |
| private void checkImageHeight() { | |
| if (news_image_height == 0) | |
| news_image_height = news_image.getHeight(); | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| View rootView = inflater.inflate(R.layout.fragment_news_detail, container, false); | |
| Bundle args = getArguments(); | |
| String title = args.getString("title"); | |
| TextView news_title = (TextView) rootView.findViewById(R.id.news_title); | |
| news_title.setText(title); | |
| EgeaApplication.easyTrackerSendScreen(getActivity(), String.format("News Reader Details Screen [%s]", title)); | |
| String description = args.getString("description"); | |
| TextView news_description = (TextView) rootView.findViewById(R.id.news_description); | |
| if (description != null) { | |
| news_description.setText(Html.fromHtml(description)); | |
| } | |
| String imageUrl = args.getString("imageUrl"); | |
| news_image = (NetworkImageView) rootView.findViewById(R.id.news_image); | |
| if (imageUrl != null) { | |
| news_image.setImageUrl(imageUrl, ImageCacheManager.getInstance().getImageLoader()); | |
| } else { | |
| news_image.setVisibility(View.GONE); | |
| } | |
| news_image.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| checkImageHeight(); | |
| ViewAnimation.collapse(v); | |
| // animate(v).setDuration(2000).scaleY(0); | |
| } | |
| }); | |
| final GestureDetectorCompat mGestureDetector = new GestureDetectorCompat(getActivity(), new GestureDetector.SimpleOnGestureListener() { | |
| public int initHeight; | |
| public int initY; | |
| @Override | |
| public boolean onDown(MotionEvent e) { | |
| checkImageHeight(); | |
| initHeight = news_image.getHeight(); | |
| initY = (int) e.getRawY(); | |
| return false; | |
| } | |
| @Override | |
| public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
| int diffY = (int) (e2.getRawY() - initY); | |
| int currentHeight = news_image.getHeight(); | |
| boolean canExpand = diffY > 0 && scrollView.getScrollY() == 0 && currentHeight < news_image_height; | |
| boolean canCollapse = diffY < 0 && currentHeight > 0; | |
| if (canExpand || canCollapse) { // | |
| int calcHeight = initHeight + diffY; | |
| if (calcHeight > news_image_height) { | |
| calcHeight = news_image_height; | |
| initHeight = calcHeight; | |
| } else if (calcHeight < 0) { | |
| calcHeight = 0; | |
| initHeight = calcHeight; | |
| } | |
| if (scrollView.getScrollY() != 0) { | |
| scrollView.scrollTo(0, 0); | |
| } | |
| news_image.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, calcHeight)); | |
| return true; | |
| } else { | |
| initY = (int) e2.getRawY(); | |
| initHeight = currentHeight; | |
| } | |
| return false; | |
| } | |
| @Override | |
| public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
| float diffY = e2.getRawY() - e1.getRawY(); | |
| int currentHeight = news_image.getHeight(); | |
| if (currentHeight > 0 && currentHeight < news_image_height) { | |
| if (diffY > 0) { | |
| ViewAnimation.expand(news_image, news_image_height); | |
| } else { | |
| ViewAnimation.collapse(news_image); | |
| } | |
| return true; | |
| } | |
| if (velocityY > 0) { | |
| boolean canExpand = velocityY * 0.2 > scrollView.getScrollY(); | |
| Log.w("", "onFling " + (canExpand ? "[ON] expand" : "[OFF] stay close") + " image - Y: " +velocityY +" "+ scrollView.getScrollY()); | |
| if (canExpand) ViewAnimation.expand(news_image, news_image_height); | |
| } | |
| return false; | |
| } | |
| }); | |
| View.OnTouchListener onTouchView = new View.OnTouchListener() { | |
| @Override | |
| public boolean onTouch(View v, MotionEvent event) { | |
| boolean onTouch = mGestureDetector.onTouchEvent(event); | |
| if (!onTouch) { | |
| if (event.getAction() == MotionEvent.ACTION_UP) { | |
| int currentHeight = news_image.getHeight(); | |
| if (currentHeight > 0 && currentHeight < news_image_height) { | |
| if (news_image_height / 2 > currentHeight) { | |
| ViewAnimation.collapse(news_image); | |
| } else { | |
| ViewAnimation.expand(news_image, news_image_height); | |
| } | |
| onTouch = true; | |
| } | |
| } | |
| } | |
| return onTouch; | |
| } | |
| }; | |
| scrollView = (ScrollView) rootView.findViewById(R.id.scrollView); | |
| scrollView.setOnTouchListener(onTouchView); | |
| return rootView; | |
| } | |
| @Override | |
| public void onResume() { | |
| super.onResume(); | |
| news_image.setMaxHeight(getResources().getDisplayMetrics().heightPixels / 5 * 2); | |
| } | |
| @Override | |
| public void update(ObservableRegisterer observable, int code, Bundle args) { | |
| //TODO complete | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment