1

我正在使用交错网格视图来显示我通过网络服务获得的信息。当我以格式纵向显示它时没有问题。(我有 2 个网格)。在横向模式下,我有 3 个网格。问题就在这里,文本视图改变了大小并变得更大,当我滚动到最后时,我将有 2 或 1 个网格(不是 3 个),有时没有网格。

这是适配器代码:

 public class StaggeredAdapterVraiCategorie extends ArrayAdapter<VraiCategorie> {
ArrayList<VraiCategorie> categorieList;
LayoutInflater vi;
int Resource;
private ImageLoader mLoader;

Context context;
public StaggeredAdapterVraiCategorie(Context context, int resource,ArrayList<VraiCategorie> objects) {
    super(context, resource, objects);
    categorieList = objects;
    Resource = resource;
    this.context=context;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    mLoader = new ImageLoader(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (convertView == null) {

        LayoutInflater layoutInflator = LayoutInflater.from(getContext());
        convertView = layoutInflator.inflate(R.layout.vrai_list_item_sample,null);
        holder = new ViewHolder();
        holder.imageView = (DynamicHeightImageView)convertView.findViewById(R.id.image);
        holder.namecateg = (TextView)convertView.findViewById(R.id.namecateg);

        holder.reftype = (TextView)convertView.findViewById(R.id.reftype);
        holder.myid = (TextView)convertView.findViewById(R.id.myid);


        convertView.setTag(holder);
    }
   // else {
        holder =(ViewHolder)convertView.getTag();
   // }

    holder.namecateg.setText(categorieList.get(position).getNamecateg());
    holder.reftype.setText(categorieList.get(position).getReftype());
    holder.myid.setText(categorieList.get(position).getMyid());

    String imgget=categorieList.get(position).getImagecateg();
    if(!imgget.equals("null")){
        String mylink="http://myserver/images/";
        String imgexe=mylink+imgget;
        new DownloadImageTask(holder.imageView).execute(imgexe);
    }
    else if (imgget.equals("null")){
        holder.imageView.setHeightRatio(1.0); //ta3mel prob fel staggered
        holder.imageView.setImageResource(R.drawable.nophoto);
    }


    if(!categorieList.get(position).getImagecateg().equals("null")) {

        String mylink = "http://myserver/images/";
       // holder = (ViewHolder) convertView.getTag();

        mLoader.DisplayImage(mylink + categorieList.get(position).getImagecateg(), holder.imageView);
    }
    return convertView;
}

static class ViewHolder {
    public  DynamicHeightImageView imageView;
    public  TextView namecateg;
    public  TextView reftype;
    public  TextView myid;


}
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 mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

        bmImage.setImageBitmap(result);
    }
  }
}

这是主要课程:

public class VraiCateg extends BaseFragment implements StaggeredGridView.OnItemClickListener {
ArrayList<VraiCategorie> categorieList;
StaggeredAdapterVraiCategorie adapter;
StaggeredGridView staggeredGridView;
String passtitle="";
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES="My profile info";
public static final String keyidhotel="idH";

private String myidhotel="";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.vrai_main, container, false);

       Main activity = (Main) getActivity();
       String myDataFromActivity = activity.getData();
       myidhotel = myDataFromActivity;
    final String url =  "http://myserver/api/type_etablissement/categories/id/"+myidhotel;
    //url = url + myidhotel;


    Toast.makeText(getActivity().getApplicationContext(), "Adresse categ URL "+url, Toast.LENGTH_LONG).show();


    staggeredGridView = (StaggeredGridView) view.findViewById(R.id.grid_view);
   // myidhotel = getArguments().getString("idH");


    int margin = getResources().getDimensionPixelSize(R.dimen.margin);
    staggeredGridView.setPadding(margin, 0, margin, 0);
    if(!(MySuperScaler.scaled))
        MySuperScaler.scaleViewAndChildren(view, MySuperScaler.scale);
    categorieList = new ArrayList<VraiCategorie>();
    new CategorieAsyncTask().execute(url);
    staggeredGridView.setOnItemClickListener(this);
    return view;
}


class CategorieAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        dialog.cancel();
       if (result == false){
            //show a msg to user that data not parsed
            Toast.makeText(getActivity().getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
        else {
            //call etab adapter and create now adapter
            StaggeredAdapterVraiCategorie adapter = new StaggeredAdapterVraiCategorie(getActivity().getApplicationContext(),R.layout.vrai_list_item_sample,categorieList);
            staggeredGridView.setAdapter(adapter);
            adapter.notifyDataSetChanged();


        }
    }

    @Override
    protected Boolean doInBackground(String... params) {

        try {
            HttpClient client = new DefaultHttpClient();
            //HttpPost post = new HttpPost(uri); //uri in oncreate method
            HttpGet get = new HttpGet(params[0]);
            HttpResponse response = client.execute(get);

            int status = response.getStatusLine().getStatusCode();
            if(status ==200){
                //get the response and pars it
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);
                JSONObject jObj = new JSONObject(data);
                JSONArray jArray = jObj.getJSONArray("categories");
                for(int i=0;i<jArray.length();i++ ){
                    VraiCategorie categ = new VraiCategorie();
                    JSONObject jRealObject = jArray.getJSONObject(i);

                    categ.setImagecateg(jRealObject.getString("image_categorie"));

                   categ.setNamecateg(jRealObject.getString("description_type"));
                    //ref_type=jRealObject.getString("ref_type");
                    categ.setReftype(jRealObject.getString("ref_type"));
                    categ.setMyid(jRealObject.getString("my_id"));

                    categorieList.add(categ);
                }

                return true; //verifie the parse
            }

        }catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
        //http call
        return false;
    }
}

@Override
public void onItemClick(StaggeredGridView parent, View view, int position, long id) {

   final String ref_type= categorieList.get(position).getReftype();
    Fragment newFragment = new VraiRela();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
   Bundle args=new Bundle();
    args.putString("ref_type",ref_type);
    args.putString("idH",myidhotel);
   // Toast.makeText(getActivity().getApplicationContext(), "ref_type"+ref_type+"idH"+myidhotel, Toast.LENGTH_LONG).show();
    newFragment.setArguments(args);
    transaction.replace(R.id.frame_container, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
  /*  myidhotel="";
    sharedpreferences =  getActivity().getApplicationContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    myidhotel="";
    editor.putString(keyidhotel, myidhotel);
    editor.commit();*/
  }

}
4

0 回答 0