Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.innovare.scrollhorizontal;
- import android.app.Activity;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.android.volley.Request;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.JsonArrayRequest;
- import com.android.volley.toolbox.JsonObjectRequest;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- public class RelativeLayoutTouchListener implements View.OnTouchListener {
- static final String logTag = "ActivitySwipeDetector";
- private MainActivity activity;
- static final int MIN_DISTANCE = 100;// TODO change this runtime based on screen resolution. for 1920x1080 is to small the 100 distance
- private float downX, downY, upX, upY;
- String URL = "http://jsonplaceholder.typicode.com/users";
- HashMap<Integer,JSONObject> map = new HashMap();
- int count=1;
- int length=0;
- ListView listView;
- LinearLayout linear;
- boolean firsttime = true;
- // private MainActivity mMainActivity;
- public RelativeLayoutTouchListener(MainActivity mainActivity) {
- activity = mainActivity;
- downloadData();
- }
- private View parseJSONObjectAt(int i) {
- JSONObject object = map.get(i);
- try {
- Log.d("CONTEXT",object.toString());
- int id = object.getInt("id");
- String name = object.getString("name");
- String username = object.getString("username");
- String mail = object.getString("email");
- // JSONObject address = object.getJSONObject("address");
- LayoutInflater inflater = LayoutInflater.from(activity);
- View v = inflater.inflate(R.layout.item_swipe, null);
- TextView idt = (TextView) v.findViewById(R.id.id);
- TextView namet = (TextView) v.findViewById(R.id.name);
- TextView usernamet = (TextView) v.findViewById(R.id.username);
- TextView emailt = (TextView) v.findViewById(R.id.email);
- idt.setText(id+"");
- namet.setText(name);
- usernamet.setText(username);
- emailt.setText(mail);
- Log.d("CONTEXT", "Trying to ADD");
- return v;
- }catch (Exception e){
- Log.d("HEY HEY", "SOME ERROR DOWNLOADING DATA");
- e.printStackTrace();
- }
- return null;
- }
- private void downloadData() {
- Log.d("JSONDOWNLOAD", "Success Downloading DATA STARTING HEY");
- JsonArrayRequest request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
- @Override
- public void onResponse(JSONArray response) {
- Log.d("JSONDOWNLOAD","Downloading DATA.............");
- length = response.length();
- for(int i=0;i<response.length();i++){
- try {
- JSONObject object = (JSONObject) response.get(i);
- Log.d("DOwnloaded Data", object.toString());
- map.put(i + 1, object);
- JSONObject object1 = map.get(i+1);
- Log.d("DOwnloaded Data(COPY)",object1.toString());
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- Log.d("JSONDOWNLOAD","Success Downloading DATA");
- if(firsttime){
- View v = parseJSONObjectAt(1);
- activity.addViewToLinearfromLeft(v);
- firsttime = false;
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.d("JSONDOWNLOAD","Erro Downloading DATA");
- }
- });
- //AppController.getInstance().addToRequestQueue(request);
- }
- public void onRightToLeftSwipe() {
- Log.i(logTag, "RightToLeftSwipe! :: "+length);
- if(count>=1) {
- if(count<length) {
- count++;
- View v = parseJSONObjectAt(count);
- activity.addViewToLinearfromRight(v);
- Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show();
- }
- }
- // activity.doSomething();
- }
- public void onLeftToRightSwipe() {
- Log.i(logTag, "LeftToRightSwipe!"+length);
- if(count<=length) {
- if(count>1) {
- count--;
- View v = parseJSONObjectAt(count);
- activity.addViewToLinearfromLeft(v);
- Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show();
- }
- }
- // activity.doSomething();
- }
- public void onTopToBottomSwipe() {
- Log.i(logTag, "onTopToBottomSwipe!");
- Toast.makeText(activity, "onTopToBottomSwipe", Toast.LENGTH_SHORT).show();
- // activity.doSomething();
- }
- public void onBottomToTopSwipe() {
- Log.i(logTag, "onBottomToTopSwipe!");
- Toast.makeText(activity, "onBottomToTopSwipe", Toast.LENGTH_SHORT).show();
- // activity.doSomething();
- }
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN: {
- downX = event.getX();
- downY = event.getY();
- return true;
- }
- case MotionEvent.ACTION_UP: {
- upX = event.getX();
- upY = event.getY();
- float deltaX = downX - upX;
- float deltaY = downY - upY;
- // swipe horizontal?
- if (Math.abs(deltaX) > MIN_DISTANCE) {
- // left or right
- if (deltaX < 0) {
- this.onLeftToRightSwipe();
- return true;
- }
- if (deltaX > 0) {
- this.onRightToLeftSwipe();
- return true;
- }
- } else {
- Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
- // return false; // We don't consume the event
- }
- // swipe vertical?
- if (Math.abs(deltaY) > MIN_DISTANCE) {
- // top or down
- if (deltaY < 0) {
- this.onTopToBottomSwipe();
- return true;
- }
- if (deltaY > 0) {
- this.onBottomToTopSwipe();
- return true;
- }
- } else {
- Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long vertically, need at least " + MIN_DISTANCE);
- // return false; // We don't consume the event
- }
- return false; // no swipe horizontally and no swipe vertically
- }// case MotionEvent.ACTION_UP:
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment