2016-07-28 12 views
0

Ich möchte Bilder gleiten, Bilder kommen durch JSON. Mit diesem Code kommt er in die Listenansicht. Aber ich möchte, dass alle zehn Bilder als Schieberegler kommen. Wie mache ich das?Ich möchte Bilder schieben, Bilder kommen durch JSON

Ich möchte Bild eins nach dem anderen horizontal verschieben.

ich bin immer wie dieses Bild

im Bild zu sehen.

enter image description here

MainActivity

public class MainActivity extends Activity { 
    // Log tag 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final String url = "http://20dde605.ngrok.io/api/v1/restaurants/get_featured_restaurants"; 
    // Movies json url 
    //private static final String url = "http://api.androidhive.info/json/movies.json"; 
    private ProgressDialog pDialog; 
    private List<Movie> movieList = new ArrayList<Movie>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, movieList); 
     listView.setAdapter(adapter); 
     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 
     // changing action bar color 
     getActionBar().setBackgroundDrawable(
       new ColorDrawable(Color.parseColor("#1b1b1b"))); 
     // Creating volley request obj 
     JsonArrayRequest movieReq = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 
         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 
           JSONObject obj = response.getJSONObject(i); 
           Movie movie = new Movie(); 
           //movie.setTitle(obj.getString("title")); 
           //movie.setName(obj.getString("name")); 
           //movie.setThumbnailUrl(obj.getString("image")); 
           movie.setThumbnailUrl(obj.getString("org_image_url")); 
           //movie.setAverage_ratings(obj.getString("average_ratings")); 
           //movie.setCuisine(obj.getString("cuisine")); 
           //movie.setAddress(obj.getJSONObject("address").getString("area")); 
           //movie.setAddress(obj.getString("address")); 
           //movie.setYear(obj.getInt("releaseYear")); 
           // Genre is json array 
           /*JSONArray genreArry = obj.getJSONArray("genre"); 
           ArrayList<String> genre = new ArrayList<String>(); 
           for (int j = 0; j < genreArry.length(); j++) { 
            genre.add((String) genreArry.get(j)); 
           } 
           movie.setGenre(genre);*/ 
           // adding movie to movies array 
           movieList.add(movie); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d(TAG, "Error: " + error.getMessage()); 
         hidePDialog(); 

        } 
       }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(movieReq); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

Appcontroller.java

public class AppController extends Application { 

    public static final String TAG = AppController.class.getSimpleName(); 

    private RequestQueue mRequestQueue; 
    private ImageLoader mImageLoader; 

    private static AppController mInstance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 
    } 

    public static synchronized AppController getInstance() { 
     return mInstance; 
    } 

    public RequestQueue getRequestQueue() { 
     if (mRequestQueue == null) { 
      mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
     } 

     return mRequestQueue; 
    } 

    public ImageLoader getImageLoader() { 
     getRequestQueue(); 
     if (mImageLoader == null) { 
      mImageLoader = new ImageLoader(this.mRequestQueue, 
        new LruBitmapCache()); 
     } 
     return this.mImageLoader; 
    } 

    public <T> void addToRequestQueue(Request<T> req, String tag) { 
     // set the default tag if tag is empty 
     req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
     getRequestQueue().add(req); 
    } 

    public <T> void addToRequestQueue(Request<T> req) { 
     req.setTag(TAG); 
     getRequestQueue().add(req); 
    } 

    public void cancelPendingRequests(Object tag) { 
     if (mRequestQueue != null) { 
      mRequestQueue.cancelAll(tag); 
     } 
    } 
} 

Modell -Film

public class Movie { 
    private String name, thumbnailUrl; 
    //private int year; 
    private String average_ratings,area,cuisine,address; 
// private ArrayList<String> genre; 

    public Movie() { 
    } 

    public Movie(String name, String thumbnailUrl, String average_ratings,String area,String cuisine,String address 
      ) { 
     this.name = name; 
     this.thumbnailUrl = thumbnailUrl; 
     //this.year = year; 
     this.average_ratings = average_ratings; 
     this.area=area; 
     this.cuisine=cuisine; 
this.address=address; 
     //this.genre = genre; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getThumbnailUrl() { 
     return thumbnailUrl; 
    } 

    public void setThumbnailUrl(String thumbnailUrl) { 
     this.thumbnailUrl = thumbnailUrl; 
    } 

    /*public int getYear() { 
     return year; 
    }*/ 

    /*public void setYear(int year) { 
     this.year = year; 
    }*/ 

    public String getAverage_ratings() { 
     return average_ratings; 
    } 

    public void setAverage_ratings(String average_ratings) { 
     this.average_ratings = average_ratings; 
    } 
    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getCuisine() { 
     return cuisine; 
    } 

    public void setCuisine(String cuisine) { 
     this.cuisine = cuisine; 
    } 
    /*public ArrayList<String> getGenre() { 
     return genre; 
    } 

    public void setGenre(ArrayList<String> genre) { 
     this.genre = genre; 
    } 
*/ 
} 

LruBitmapcache

public class LruBitmapCache extends LruCache<String, Bitmap> implements 
     ImageCache { 
    public static int getDefaultLruCacheSize() { 
     final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024); 
     final int cacheSize = maxMemory/8; 

     return cacheSize; 
    } 

    public LruBitmapCache() { 
     this(getDefaultLruCacheSize()); 
    } 

    public LruBitmapCache(int sizeInKiloBytes) { 
     super(sizeInKiloBytes); 
    } 

    @Override 
    protected int sizeOf(String key, Bitmap value) { 
     return value.getRowBytes() * value.getHeight()/1024; 
    } 

    @Override 
    public Bitmap getBitmap(String url) { 
     return get(url); 
    } 

    @Override 
    public void putBitmap(String url, Bitmap bitmap) { 
     put(url, bitmap); 
    } 
} 

Antwort

0

Verwenden <ViewFlipper> oder <ViewPager> oder ImageSlider Bibliothek verwenden.

0

Ich empfehle besser Pager für die Diashow zu verwenden. Für Sie Hinweis: to use pager andere Sache können wir sogar Timer verwenden:

public void makescroll() { 
     final Handler mHandler= new Handler(); 

     // Create runnable for posting 
     final Runnable mUpdateResults = new Runnable() { 
      public void run() { 

       AnimateandSlideShow(); //display image of your arraylist 

      } 
     }; 
     int delay = 5000; // delay for 1 sec. 

     int period = 5000; // repeat every 4 sec. 

     Timer timer = new Timer(); 

     timer.scheduleAtFixedRate(new TimerTask() { 

      public void run() { 

       mHandler.post(mUpdateResults); 

      } 

     }, delay, period); 
} 
0

würde ich RecyclerView über normale ListView für eine bessere Leistung und mehr anpassbar empfehlen. RecyclerView Unterstützung horizontale Listen aber Einstellung LinearLayoutManager wie folgt:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false); 
recyclerView.setLayoutManager(linearLayoutManager);