我有一个项目在片段中有一个gridview,显示一个游戏列表,点击每个游戏将打开一个显示游戏指南的新片段。
如何从服务器管理此阵列,当我上传更多图像时,它会使用新图像更新应用网格视图?
实际上,我需要的是:
public class GridGameListFragment extends Fragment {
private Context context;
private LayoutInflater inflater;
GridView gridView;
public GridListaJogosFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.grid_lista_jogos_fragment, container, false);
gridView = (GridView) view.findViewById(R.id.gridview_xml);
CustomGridAdapterGameList adapter = new CustomGridAdapterGameList(getActivity());
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GuideFragment guideFragment = new GuideFragment();
fragmentTransaction.add(R.id.rl_lista_jogos_activity_xml, guideFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}
}
========================
public class CustomGridAdapterGameList extends BaseAdapter {
private String[] titles = { "Final Fantasy 7",
"Final Fantasy 8",
"Final Fantasy 9"
};
private int[] covers = { R.drawable.ff7_ps1,
R.drawable.ff8_ps1,
R.drawable.ff9_ps1,
};
private Context context;
private LayoutInflater inflater;
public CustomGridAdapterGameList(Context context){
this.context = context;
}
@Override
public int getCount() {
return titles.length;
}
@Override
public Object getItem(int position) {
return titles.length;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridview = convertView;
if(convertView == null){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridview = inflater.inflate(R.layout.custom_gridview__lista_jogos_layout, null);
}else{
gridview= convertView;
}
TextView textView = gridview.findViewById(R.id.textview_custom_grid_xml);
ImageView imageView = gridview.findViewById(R.id.imageview_custom_grid_xml);
textView.setText(titles[position]);
imageView.setImageResource(covers[position]);
return gridview;
}
}
答案 0 :(得分:0)
getview()。 你可以使用AsyncTask下载游戏图像,然后使用setImageResourse
private class DownloadImageTask extends AsyncTask<String, Void,Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
如果您可以缓存游戏图像,而不是每次滚动下载它们,那么这是一个很好的做法
第二个问题可以通过在添加新图像时从服务器发送(消息)来解决。实现一个监听器来监听这条消息。收到后,更新您的GridView 您还可以为更简单的解决方案实施下拉刷新