当图像来自json时,我必须制作一个图像滑块。 我用过View Flipper。 以前它作为列表视图出现,但我想图像应该在水平而不是列表视图中显示为滑块。 当我做出一些改变时,它不会像图像的滑动一样。
为什么它不是滑块? 我没有得到任何东西。我得到了空白屏幕。请帮助我如何通过点击json url来获得图像的滑动。
MainActivty.java
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
private static final String url = "http://f6aa763b.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;
ViewFlipper viewFlipper;
//private SliderLayout imageSlider;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
//listView = (ListView) findViewById(R.id.list);
//imageSlider = (SliderLayout)findViewById(R.id.slider);
adapter = new CustomListAdapter(this, movieList);
//listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
// handler to set duration and to upate animation
final Handler mHandler = new Handler() {
@Override
public void close() {
}
@Override
public void flush() {
}
@Override
public void publish(LogRecord record) {
}
};
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
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();
setFlipperImage(movieList);
}
}, 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;
}*/
private void setFlipperImage(List<Movie> movieList) {
for(int i=0;i<movieList.size();i++){
Log.i("Set Filpper Called", movieList.get(i).toString()+"");
ImageView org_image_url = new ImageView(getApplicationContext());
//com.android.volley.toolbox.NetworkImageView thumbnail = (NetworkImageView) new ImageView(getApplicationContext());
// image.setBackgroundResource(res);
Picasso.with(MainActivity.this)
.load(movieList.get(i).toString())
.placeholder(R.drawable.bc)
.error(R.drawable.bc)
.into(org_image_url);
viewFlipper.addView(org_image_url);
}
viewFlipper.setAutoStart(true);
}
// method to show slide show
private void AnimateandSlideShow() {
viewFlipper.showNext();
}
}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="6dp" >
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="260dp"
android:scaleType="fitXY"
android:adjustViewBounds="false"
android:layout_centerHorizontal="true" />
</ViewFlipper>
</LinearLayout>
Customlist Adapter.java
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView _ImageView = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
_ImageView.setDefaultImageResId(R.drawable.bc);
Movie m = movieItems.get(position);
_ImageView.setImageUrl(m.getThumbnailUrl(), imageLoader);
return convertView;
}
}
Logcat
08-02 16:12:36.100 4782-4782/info.androidhive.customlistviewvolley W/System: ClassLoader referenced unknown path: /data/app/info.androidhive.customlistviewvolley-2/lib/x86
08-02 16:12:37.785 4782-4782/info.androidhive.customlistviewvolley W/System: ClassLoader referenced unknown path: /data/app/info.androidhive.customlistviewvolley-2/lib/x86
08-02 16:12:37.884 4782-4863/info.androidhive.customlistviewvolley D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 08-02 16:12:37.893 4782: 4782 D/ ]
HostConnection::get() New Host Connection established 0xb4018fc0, tid 4782
[ 08-02 16:12:37.943 4782: 4863 D/ ]
HostConnection::get() New Host Connection established 0xae507570, tid 4863
08-02 16:12:37.945 4782-4863/info.androidhive.customlistviewvolley I/OpenGLRenderer: Initialized EGL, version 1.4
08-02 16:12:39.306 4782-4782/info.androidhive.customlistviewvolley D/MainActivity: [{"id":1980,"name":"Yuuka - The St. Regis Mumbai","description":"Super Sushi","restaurant_type":null,"restaurant_url":null,"restaurant_facebook_url":null,"restaurant_google_url":null,"restaurant_zomato_url":"https:\/\/www.zomato.com\/mumbai\/yuuka-the-st-regis-mumbai-lower-parel","average_ratings":"4.1","profile_image_file_name":"1980.jpg","profile_image_content_type":"image\/jpeg","cover_image_file_name":null,"cover_image_content_type":null,"establishment_type":"Fine Dining","cost":"8000","cuisine":"Japanese","timing":"7:30 PM to 12 Midnight","extras":null,"phone_numbers":[{"id":2869,"restaurant_id":1980,"number":"022 30151995","created_at":"2016-07-26T06:38:29.016-04:00","updated_at":"2016-07-26T06:38:29.016-04:00"}],"org_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_images\/1980.jpg","thumb_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_thumb_images\/1980.jpg","address":{"area":"The St. Regis, Lower Parel","full_address":"The St. Regis, 462, Senapati Bapat Marg, Lower Parel, Mumbai,The St. Regis, Lower Parel,mumbai,maharashtra,india,"},"menus":[],"restaurant_offers":[{"id":751,"text":"\"A free glass of domestic red\/white\/sparkling wine per person\"","offer_link":"https:\/\/www.eazydiner.com\/\/mumbai\/yuuka-the-st-regis-lower-parel-223235","resource":"eazydiner"}]},{"id":1174,"name":"EXO - The St. Regis Mumbai","description":"Where's The Party?","restaurant_type":null,"restaurant_url":null,"restaurant_facebook_url":null,"restaurant_google_url":null,"restaurant_zomato_url":"https:\/\/www.zomato.com\/mumbai\/exo-the-st-regis-mumbai-lower-parel","average_ratings":"4.1","profile_image_file_name":"1174.jpg","profile_image_content_type":"image\/jpeg","cover_image_file_name":null,"cover_image_content_type":null,"establishment_type":"Lounge","cost":"5000","cuisine":"Finger Food","timing":"7 PM to 3 AM (Wed-Sun), Mon & Tue Closed","extras":null,"phone_numbers":[{"id":1642,"restaurant_id":1174,"number":"022 61628422","created_at":"2016-07-26T06:38:19.743-04:00","updated_at":"2016-07-26T06:38:19.743-04:00"},{"id":1643,"restaurant_id":1174,"number":" 022 61628000","created_at":"2016-07-26T06:38:19.744-04:00","updated_at":"2016-07-26T06:38:19.744-04:00"}],"org_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_images\/1174.jpg","thumb_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_thumb_images\/1174.jpg","address":{"area":"The St. Regis, Lower Parel","full_address":"The St. Regis, Senapati Bapat Marg, Lower Parel, Mumbai,The St. Regis, Lower Parel,mumbai,maharashtra,india,"},"menus":[],"restaurant_offers":[]},{"id":353,"name":"Lotus Cafe - JW Marriott Mumbai Juhu","description":"Sunday Brunches, Book a Table Online on Zomato","restaurant_type":null,"restaurant_url":null,"restaurant_facebook_url":null,"restaurant_google_url":null,"restaurant_zomato_url":"https:\/\/www.zomato.com\/mumbai\/lotus-cafe-jw-marriott-mumbai-juhu-juhu","average_ratings":"4.1","profile_image_file_name":"353.jpg","profile_image_content_type":"image\/jpeg","cover_image_file_name":null,"cover_image_content_type":null,"establishment_type":"Fine Dining","cost":"3500","cuisine":"Italian, Continental, North Indian, Asian","timing":"24 Hours","extras":null,"phone_numbers":[{"id":479,"restaurant_id":353,"number":"022 30151027","created_at":"2016-07-26T06:38:09.566-04:00","updated_at":"2016-07-26T06:38:09.566-04:00"}],"org_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_images\/353.jpg","thumb_image_url":"http:\/\/comida-dev.s3.amazonaws.com\/res_pro_thumb_images\/353.jpg","address":{"area":"JW Marriott Mumbai Juhu, Juhu","full_address":"JW Marriott, Juhu Tara Road, Juhu, Mumbai,JW Marriott Mumbai Juhu, Juhu,mumbai,maharashtra,india,"},"menus":[],"restaurant_offers":[{"id":235,"text":"\"A free pint of beer per person\"","offer_link":"https:\/\/www.eazydiner.com\/\/mumbai\/lotus-cafe-juhu-223153","resource":"eazydiner"}]},{"id":209,"name":"Aer - Four Seasons","description":"Romantic, Happy Hours, Rooftops, Summer Cocktails","restaurant_type":null,"restaurant_url":null,"restaurant_f
08-02 16:12:39.309 4782-4863/info.androidhive.customlistviewvolley E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa3d3b90
08-02 16:12:39.335 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@bc7b903
08-02 16:12:39.343 4782-4782/info.androidhive.customlistviewvolley W/Settings: Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
08-02 16:12:39.525 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@ed61575
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@9d457b
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@dff93f1
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@130be57
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@4687e2d
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@118e8f3
08-02 16:12:39.526 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@2fbd029
08-02 16:12:39.527 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@44f214f
08-02 16:12:39.567 4782-4792/info.androidhive.customlistviewvolley I/art: Background partial concurrent mark sweep GC freed 10269(920KB) AllocSpace objects, 9(288KB) LOS objects, 40% free, 2MB/4MB, paused 1.626ms total 140.282ms
08-02 16:12:39.586 4782-4782/info.androidhive.customlistviewvolley I/Set Filpper Called: info.androidhive.customlistviewvolley.model.Movie@45845e5