Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.innovare.scrollhorizontal;
- import android.annotation.TargetApi;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.PorterDuffXfermode;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.provider.Contacts;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.view.animation.TranslateAnimation;
- import android.widget.Button;
- import android.widget.Gallery;
- import android.widget.HorizontalScrollView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListAdapter;
- import android.widget.RelativeLayout;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.w3c.dom.Text;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.logging.Handler;
- public class MainActivity extends AppCompatActivity implements ScrollViewListener{
- ObservableScrollView soriscroll;
- JSONObject jsonobject;
- JSONArray jsonarray;
- TextView text;
- LinearLayout linear_view;
- TextView scroll_point;
- LinearLayout linear3;
- //private static String url = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
- //private static String url = "http://www.mocky.io/v2/59a94ceb100000200c3e0a78";
- //private static String url = "https://pastebin.com/raw/bPzqq2Kd";
- //private static String url = "https://pastebin.com/raw/e4ZZmwuC";
- private static String url = "https://pastebin.com/raw/MWTj6rzZ";
- boolean count=true;
- RelativeLayout relative = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- new downloadData().execute();
- relative = (RelativeLayout) findViewById(R.id.relative);
- linear_view = (LinearLayout) findViewById(R.id.linear_view);
- }
- @Override
- public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
- int a = linear_view.getWidth();
- int b = soriscroll.getWidth();
- if ((a - b) == x) {
- new downloadData().execute();
- }
- scroll_point.setText("linear_view" + a + "\nscroll_view" + b + "\n" + x + " : " + y);
- }
- private class downloadData extends AsyncTask<Void,Void,Void>{
- ProgressDialog pDialog = null;
- String text1="";
- ArrayList<HashMap<String,String>> arrayList = new ArrayList<>();
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- // Showing progress dialog
- if(count) {
- pDialog = new ProgressDialog(MainActivity.this);
- pDialog.setMessage("Please wait...");
- pDialog.setCancelable(false);
- pDialog.show();
- }
- }
- @Override
- protected Void doInBackground(Void... params) {
- ServiceHandler sh = new ServiceHandler();
- String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
- Log.d("Response: ", "> " + jsonStr);
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- jsonarray = jsonObj.getJSONArray("worldpopulation");
- for (int i = 0; i < jsonarray.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- jsonobject = jsonarray.getJSONObject(i);
- String author = jsonobject.getString("author");
- String photo = jsonobject.getString("photo");
- addToLinearLayout("",author,"",photo);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- Log.e("ServiceHandler", "Couldn't get any data from the url");
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- // Dismiss the progress dialog
- if(count) {
- if (pDialog.isShowing())
- pDialog.dismiss();
- count= false;
- }
- }
- }
- private void addToLinearLayout(String text1, final String text2,String text3,String text4) {
- LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
- final View v = inflater.inflate(R.layout.scroll_element, linear_view, false);
- TextView tv = (TextView) v.findViewById(R.id.rank);
- final TextView tv2 = (TextView) v.findViewById(R.id.name);
- TextView tv3 = (TextView) v.findViewById(R.id.population);
- ImageView iv = (ImageView) v.findViewById(R.id.imagelink);
- InputStream input = null;
- Bitmap bitmap = null;
- try {
- input = new java.net.URL(text4).openStream();
- bitmap = BitmapFactory.decodeStream(input);
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Decode Bitmap
- tv.setText(text1);
- tv2.setText(text2);
- tv3.setText(text3);
- iv.setImageBitmap(bitmap);
- v.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- scroll_point.setText(text2);
- // Toast.makeText(getApplicationContext(),text2,Toast.LENGTH_SHORT).show();
- }
- });
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- linear_view.addView(v);
- }
- });
- }
- private void doAddText(String rank) {
- text.setText(rank);
- }
- public void addViewToLinearfromRight(View v){
- Log.d("CONTEXT", "View is Adding");
- Animation animation;
- animation = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
- );
- animation.setDuration(400);
- Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
- linear3.setAnimation(anim);
- linear3.removeAllViews();
- linear3.addView(v);
- v.setAnimation(animation);
- }
- public void addViewToLinearfromLeft(View v){
- Animation animation2 = new TranslateAnimation(
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
- Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
- );
- animation2.setDuration(400);
- linear3.startAnimation(animation2);
- new Thread(new Task(v)).start();
- }
- public void addNewView(View v){
- }
- private class Task implements Runnable{
- View v;
- Task(View v){
- this.v = v;
- }
- @Override
- public void run() {
- try {
- Thread.sleep(400);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- linear3.post(new Runnable() {
- @Override
- public void run() {
- Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_fade_in);
- linear3.removeAllViews();
- v.setAnimation(anim);
- linear3.addView(v);
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment