我想实现 Etsy 的交错网格视图。问题是,我不知道如何将 Volley 的 NetworkImage View 与它结合起来。我从 Json 文件中获取 Imageurl,因为我正在使用 volley。
这是我的 MainActivity.class
import com.etsy.android.grid.StaggeredGridView;
public class NeuImKino extends ActionBarActivity {
private Toolbar toolbar;
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://***json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
private static String Title = "title";
private static String movie = "movie";
private static StaggeredGridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_neu_im_kino);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mGridView = (StaggeredGridView) findViewById(R.id.grid_view);
//listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
//listView.setAdapter(adapter);
mGridView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// JSON PARSING
JsonObjectRequest movieReq = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
parseJSONResponse(response);
}
}, 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);
/**listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = ((TextView) view.findViewById(R.id.title)).getText().toString();
Intent intent = new Intent(NeuImKino.this, SingleItemView.class);
intent.putExtra(Title, name);
startActivity(intent);
}
}
); **/
}
public void parseJSONResponse(JSONObject response) {
try {
if (response.has("movie")) {
JSONArray arrayMovies = response.getJSONArray("movie");
for (int i = 0; i < arrayMovies.length(); i++) {
JSONObject currentMovie = arrayMovies.getJSONObject(i);
Movie movie = new Movie();
String title = null;
if (currentMovie.has("title")) {
title = currentMovie.getString("title");
} else {
title = "NA";
}
String fsk;
if (currentMovie.has("fsk")) {
fsk = currentMovie.getString("fsk");
} else {
fsk = "NA";
}
String country = null;
if (currentMovie.has("country")) {
country = currentMovie.getString("country");
} else {
country = "NA";
}
String start = null;
if (currentMovie.has("start")) {
start = currentMovie.getString("start");
} else {
start = "NA";
}
String longdesc = null;
if (currentMovie.has("longdesc")) {
longdesc = currentMovie.getString("longdesc");
} else {
longdesc = "NA";
}
String imgname = null;
if (currentMovie.has("imgname")) {
imgname = currentMovie.getString("imgname");
} else {
imgname = null;
}
movie.setTitle(title);
movie.setFsk(fsk);
movie.setStart(start);
movie.setCountry(country);
movie.setLongDesc(longdesc);
movie.setImgName(imgname);
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
@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.menu_main, menu);
return true;
}
}
我的自定义列表适配器:
public class CustomListAdapter extends BaseAdapter {
private static String CONFIG_URL = "http://****";
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// Constructor
public CustomListAdapter(Activity activity, List<Movie> movieItem) {
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;
}
// Create each list view row
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
// Layout Inflater to call external xml layout
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
// Inflate list_row_neuimkino file for each row
convertView = inflater.inflate(R.layout.list_row_neuimkino, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
DynamicHeightImageView poster = (DynamicHeightImageView) convertView.findViewById(R.id.image);
//TextView title = (TextView) convertView.findViewById(R.id.title);
//TextView fsk = (TextView) convertView.findViewById(R.id.fsk);
//TextView start = (TextView) convertView.findViewById(R.id.start);
// getting movie data for the row from Model object
Movie m = movieItems.get(position);
// Set Model Values in Holder Items
// thumbnail image
//poster.setImageUrl(CONFIG_URL+m.getImgName()+"_w300.jpg", imageLoader);
// title
// title.setText(m.getTitle());
// fsk
//fsk.setText("FSK: " + m.getFsk());
// Startdatum
//start.setText("Beginnt: " +m.getStart());
return convertView;
}
}
最后是我的一行布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.etsy.android.grid.util.DynamicHeightImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />