mhnds

Untitled

Oct 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. package com.example.innovare.scrollhorizontal;
  2.  
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.widget.LinearLayout;
  9. import android.widget.ListView;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import com.android.volley.Request;
  14. import com.android.volley.Response;
  15. import com.android.volley.VolleyError;
  16. import com.android.volley.toolbox.JsonArrayRequest;
  17. import com.android.volley.toolbox.JsonObjectRequest;
  18.  
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22.  
  23. import java.util.HashMap;
  24.  
  25. public class RelativeLayoutTouchListener implements View.OnTouchListener {
  26.  
  27. static final String logTag = "ActivitySwipeDetector";
  28. private MainActivity activity;
  29. static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
  30. private float downX, downY, upX, upY;
  31. String URL = "http://jsonplaceholder.typicode.com/users";
  32. HashMap<Integer,JSONObject> map = new HashMap();
  33. int count=1;
  34. int length=0;
  35. ListView listView;
  36. LinearLayout linear;
  37. boolean firsttime = true;
  38.  
  39. // private MainActivity mMainActivity;
  40.  
  41. public RelativeLayoutTouchListener(MainActivity mainActivity) {
  42. activity = mainActivity;
  43. downloadData();
  44.  
  45.  
  46. }
  47.  
  48. private View parseJSONObjectAt(int i) {
  49. JSONObject object = map.get(i);
  50. try {
  51. Log.d("CONTEXT",object.toString());
  52. int id = object.getInt("id");
  53. String name = object.getString("name");
  54. String username = object.getString("username");
  55. String mail = object.getString("email");
  56. // JSONObject address = object.getJSONObject("address");
  57.  
  58. LayoutInflater inflater = LayoutInflater.from(activity);
  59. View v = inflater.inflate(R.layout.item_swipe, null);
  60. TextView idt = (TextView) v.findViewById(R.id.id);
  61. TextView namet = (TextView) v.findViewById(R.id.name);
  62. TextView usernamet = (TextView) v.findViewById(R.id.username);
  63. TextView emailt = (TextView) v.findViewById(R.id.email);
  64. idt.setText(id+"");
  65. namet.setText(name);
  66. usernamet.setText(username);
  67. emailt.setText(mail);
  68. Log.d("CONTEXT", "Trying to ADD");
  69. return v;
  70.  
  71.  
  72. }catch (Exception e){
  73. Log.d("HEY HEY", "SOME ERROR DOWNLOADING DATA");
  74. e.printStackTrace();
  75. }
  76. return null;
  77.  
  78. }
  79.  
  80. private void downloadData() {
  81. Log.d("JSONDOWNLOAD", "Success Downloading DATA STARTING HEY");
  82. JsonArrayRequest request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
  83.  
  84. @Override
  85. public void onResponse(JSONArray response) {
  86. Log.d("JSONDOWNLOAD","Downloading DATA.............");
  87. length = response.length();
  88. for(int i=0;i<response.length();i++){
  89. try {
  90. JSONObject object = (JSONObject) response.get(i);
  91. Log.d("DOwnloaded Data", object.toString());
  92. map.put(i + 1, object);
  93. JSONObject object1 = map.get(i+1);
  94. Log.d("DOwnloaded Data(COPY)",object1.toString());
  95. } catch (JSONException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. Log.d("JSONDOWNLOAD","Success Downloading DATA");
  100. if(firsttime){
  101. View v = parseJSONObjectAt(1);
  102. activity.addViewToLinearfromLeft(v);
  103. firsttime = false;
  104. }
  105. }
  106. }, new Response.ErrorListener() {
  107. @Override
  108. public void onErrorResponse(VolleyError error) {
  109. Log.d("JSONDOWNLOAD","Erro Downloading DATA");
  110. }
  111. });
  112. //AppController.getInstance().addToRequestQueue(request);
  113.  
  114. }
  115.  
  116. public void onRightToLeftSwipe() {
  117. Log.i(logTag, "RightToLeftSwipe! :: "+length);
  118. if(count>=1) {
  119. if(count<length) {
  120. count++;
  121. View v = parseJSONObjectAt(count);
  122. activity.addViewToLinearfromRight(v);
  123. Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show();
  124. }
  125.  
  126. }
  127.  
  128.  
  129. // activity.doSomething();
  130. }
  131.  
  132. public void onLeftToRightSwipe() {
  133. Log.i(logTag, "LeftToRightSwipe!"+length);
  134. if(count<=length) {
  135. if(count>1) {
  136. count--;
  137. View v = parseJSONObjectAt(count);
  138. activity.addViewToLinearfromLeft(v);
  139. Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show();
  140. }
  141. }
  142.  
  143. // activity.doSomething();
  144. }
  145.  
  146. public void onTopToBottomSwipe() {
  147. Log.i(logTag, "onTopToBottomSwipe!");
  148. Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show();
  149. // activity.doSomething();
  150. }
  151.  
  152. public void onBottomToTopSwipe() {
  153. Log.i(logTag, "onBottomToTopSwipe!");
  154. Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show();
  155. // activity.doSomething();
  156. }
  157.  
  158. public boolean onTouch(View v, MotionEvent event) {
  159. switch (event.getAction()) {
  160. case MotionEvent.ACTION_DOWN: {
  161. downX = event.getX();
  162. downY = event.getY();
  163. return true;
  164. }
  165. case MotionEvent.ACTION_UP: {
  166. upX = event.getX();
  167. upY = event.getY();
  168.  
  169. float deltaX = downX - upX;
  170. float deltaY = downY - upY;
  171.  
  172. // swipe horizontal?
  173. if (Math.abs(deltaX) > MIN_DISTANCE) {
  174. // left or right
  175. if (deltaX < 0) {
  176. this.onLeftToRightSwipe();
  177. return true;
  178. }
  179. if (deltaX > 0) {
  180. this.onRightToLeftSwipe();
  181. return true;
  182. }
  183. } else {
  184. Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
  185. // return false; // We don't consume the event
  186. }
  187.  
  188. // swipe vertical?
  189. if (Math.abs(deltaY) > MIN_DISTANCE) {
  190. // top or down
  191. if (deltaY < 0) {
  192. this.onTopToBottomSwipe();
  193. return true;
  194. }
  195. if (deltaY > 0) {
  196. this.onBottomToTopSwipe();
  197. return true;
  198. }
  199. } else {
  200. Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE);
  201. // return false; // We don't consume the event
  202. }
  203.  
  204. return false; // no swipe horizontally and no swipe vertically
  205. }// case MotionEvent.ACTION_UP:
  206. }
  207. return false;
  208. }
  209.  
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment