Gridview的AdapterView并不令人耳目一新,它显示出奇怪的响应(有时候有些时候不刷新)

时间:2016-02-15 07:33:27

标签: android android-gridview

我正在片段上设置gridview的适配器。此片段附加在视图寻呼机的布局上,并且viewpager附加在活动中。 我检查过我的arraylist数据是刷新但视图不令人耳目一新。

我的代码是:

在onCreate of Activity ::

中设置适配器
for(ArrayList<Item> newLst : updateList){
  newPropertySubList.add(newLst);
}
Log.e("newPropertySubList","newPropertySubList"+newPropertySubList.size());
adapter = new PropertyPagerAdapetr(getSupportFragmentManager());
//propertyViewPager.setAdapter(adapter);
adapter.notifyChangeInPosition(1);
propertyViewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
mIndicator.setViewPager(propertyViewPager);

PropertyPagerAdapetr是:在此代码部分中,GridviewFragment在设置适配器时创建新实例。

public class PropertyPagerAdapetr extends FragmentPagerAdapter {          
  private long baseId = 0;
  public PropertyPagerAdapetr(FragmentManager fm) {
    super(fm);
  }

  @Override
  public Fragment getItem(int arg0) {
    return GridViewFragment.newInstance(newPropertySubList.get(arg0));
  }

  @Override
  public long getItemId(int position) {
    return baseId + position;   
  }

  public void destroyAllItem(){
    getSupportFragmentManager().getFragments().clear();
  }

  @Override
  public int getItemPosition(Object object) {
    return POSITION_NONE;
  }

  @Override
  public int getCount() {
    if (newPropertySubList != null && newPropertySubList.size() > 0)
      return newPropertySubList.size();
    else
      return 0;
    }    

  public void notifyChangeInPosition(int n) {
    // shift the ID returned by getItemId outside the range of all previous fragments
    baseId += getCount() + n;
  }
}        

GridViewFragment:Gridview适配器正在此片段中设置

public class GridViewFragment extends Fragment {

    public static final String PROPERTY_TYPE = "property_type";
    private GridView mImageGrid;
    private View view;
    ArrayList<Item> itemList;
    String forRentOrSale;
    double belowheight;

    private GridLayout gLayout;
    //int count;
    //static Context mContext;

    public static GridViewFragment newInstance(ArrayList<Item> propertyList) {

        GridViewFragment frag = new GridViewFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("Property_item", propertyList);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.grid_view_fragmnet, container, false);
        mImageGrid = (GridView) rootView.findViewById(R.id.gridview);
        itemList = (ArrayList<Item>) getArguments().getSerializable(
                "Property_item");

        LoadPropertyAdapter adapter = new LoadPropertyAdapter(getActivity());
        mImageGrid.setAdapter(adapter);

        //mImageGrid.setAdapter(new MyAdapter());
        return rootView;

        //return (view = inflater.inflate(R.layout.grid_view_fragmnet, null));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        belowheight = (((SearchForm) getActivity()).belowHeight / 2);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        mImageGrid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // Toast.makeText(getActivity(),
                // ""+itemList.get(position).getPID()+" "+itemList.get(position).getA2(),
                // Toast.LENGTH_LONG).show();

                if (Constants.g_Setttings.getApp_google_analytics() != null
                        && !Constants.g_Setttings.getApp_google_analytics().equals("")) {
                    TVApplication dApp = (TVApplication) getActivity().getApplication();
                    Tracker tracker = dApp.getTracker(TrackerName.APP_TRACKER,
                            Constants.g_Setttings.getApp_google_analytics());
                    tracker.setScreenName("/Search Form");
                    tracker.set(Fields.SCREEN_NAME, "Search Form");
                    tracker.send(new HitBuilders.AppViewBuilder().build());
                }

                Intent intent = new Intent(getActivity(),
                        PropertyDetailActivity.class);
                Item item = itemList.get(position);

                intent.putExtra(Constants.ACCESS_DETAIL_PAGE_KEY,
                        item);
                if (((SearchForm) getActivity()).boolSale) {
                    intent.putExtra(PROPERTY_TYPE, "for sale");
                } else if (((SearchForm) getActivity()).boolRent) {
                    intent.putExtra(PROPERTY_TYPE, "to rent");
                } else if (((SearchForm) getActivity()).boolStudentRent) {
                    intent.putExtra(PROPERTY_TYPE, "to rent");
                }
                startActivity(intent);
            }
        });
    }

    private void initView() {
        //mImageGrid = (GridView) view.findViewById(R.id.gridview);
        // gLayout=(GridLayout) view.findViewById(R.id.gridlayout);
        /*itemList = (ArrayList<Item>) getArguments().getSerializable(
                "Property_item");

        LoadPropertyAdapter adapter = new LoadPropertyAdapter();
        mImageGrid.setAdapter(adapter);*/
    }

    private void initData() {

        if (itemList == null || itemList.size() == 0)
            return;

        // setUpGridLayout();
        /*
         * ViewGroup.LayoutParams layoutParams = mImageGrid.getLayoutParams();
         * layoutParams.height = (int)
         * (((SearchForm)getActivity()).belowHeight/2); //this is in pixels
         * mImageGrid.setLayoutParams(layoutParams);
         */
    }



    }

    public class LoadPropertyAdapter extends BaseAdapter {
        private Item propertyItem;
        Context mContext;
        Holder holder = null;
        DisplayImageOptions options;
        ImageLoader imageLoader;
        ImageLoaderConfiguration config;

        public LoadPropertyAdapter(FragmentActivity activity) {
            mContext = activity;
            imageLoader = ImageLoader.getInstance();
            config = new ImageLoaderConfiguration.Builder(mContext)
            .memoryCache(new WeakMemoryCache())
            .denyCacheImageMultipleSizesInMemory()
            .threadPoolSize(1)
            .build();

            options = new DisplayImageOptions.Builder()
            .cacheOnDisc()
            .resetViewBeforeLoading()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .build();

            imageLoader.init(config);
        }

        @Override
        public int getCount() {
            if (itemList != null && itemList.size() > 0)
                return itemList.size();
            else
                return 0;
        }

        @Override
        public Object getItem(int arg0) {
            return itemList.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        public String imageUrl() {
            String imageUrl = "";
            /*
             * if (nm_item.m_bIsAdvert) { imageUrl =
             * Constants.advertContainer.getImages_url() + "&file=" +
             * Constants.advertContainer.getGen_advert()
             * .get(nm_item.m_nIndex).getsca_image(); } else {
             */
            imageUrl = Constants.getRequestUrl(propertyItem.getPID(), 1,
                    Constants.FLOORPLANS_IMAGE_BIG_URL, "");
            // }
            return imageUrl;
        }


        public class Holder
        {
            TextView priceText,content, feesApply, cornerLabel;
            ImageView imageview;
            RelativeLayout propertyStatusBG, propertyInfoView, layout;
            ProgressBar progressbar ;
        }

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

            View rowView = null;
            propertyItem = itemList.get(position);

            if (rowView == null) {
                // If convertView is null then inflate the appropriate layout file
                rowView = LayoutInflater.from(mContext).inflate( R.layout.grid_view_imageviewitem , null);
                holder = new Holder();
                rowView.setTag(holder);

            }else{
                holder = (Holder) rowView.getTag();
            }

            //rowView = new View(getActivity());
            holder.imageview = (ImageView) rowView
                    .findViewById(R.id.img_property);
            holder.priceText = (TextView) rowView
                    .findViewById(R.id.pricetext);
            holder.content = (TextView) rowView
                    .findViewById(R.id.contenttext);
            holder.feesApply = (TextView) rowView
                    .findViewById(R.id.txt_fees_rent);
            holder.propertyStatusBG = (RelativeLayout) rowView
                    .findViewById(R.id.rel_status_bg);

            holder.cornerLabel = (TextView) rowView
                    .findViewById(R.id.txt_property_offer);

            holder.progressbar = (ProgressBar) rowView
                    .findViewById(R.id.progressbar);

            holder.propertyInfoView = (RelativeLayout) rowView
                    .findViewById(R.id.property_info_view);

            //holder.imageview.setTag(imageUrl());


            if (Constants.filter_Parameter != null
                    && (Constants.filter_Parameter.getGoal().equals("2")
                            || Constants.filter_Parameter.getGoal().equals(
                                    "3") || Constants.filter_Parameter
                                    .getGoal().equals("5"))) {
                if (Constants.g_Setttings != null
                        && Constants.g_Setttings.getIncl_tenantfeesinfo() != null
                        && Constants.g_Setttings.getIncl_tenantfeesinfo()
                        .equals("1")) {
                    if (propertyItem.getTenant_fee_exempt() != null
                            && propertyItem.getTenant_fee_exempt().equals("0")) {
                        holder.feesApply.setVisibility(View.VISIBLE);
                    } else {
                        holder.feesApply.setVisibility(View.GONE);
                    }
                } else {
                    holder.feesApply.setVisibility(View.GONE);
                }
            } else {
                holder.feesApply.setVisibility(View.GONE);
            }

            if (holder.feesApply != null) {
                holder.feesApply.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(),
                                TenentFeeDialogActivity.class);
                        startActivity(intent);
                    }
                });
            }


            Util.setButtonBackgroundColor(holder.propertyStatusBG);
            // propertyStatusBG.getBackground().setAlpha(88);
            Util.setButtonBackgroundColor(holder.cornerLabel);
            if (propertyItem.getStatus() != null && !propertyItem.getStatus().equals("")) {
                updatePropertyStatus(propertyItem.getStatus(), holder.cornerLabel,
                        holder.propertyStatusBG);
            } else {
                holder.propertyStatusBG.setVisibility(View.GONE);
            }

            if (propertyItem.getPR() != null
                    && !propertyItem.getPR().equals("")) {
                double price = Math.round(Double.parseDouble(propertyItem
                        .getPR()));

                NumberFormat format = NumberFormat.getCurrencyInstance();
                format.setMinimumFractionDigits(0);
                format.setCurrency(Currency.getInstance(Locale.UK));

                if (propertyItem.getLetRentFrequency() != null
                        && !propertyItem.getLetRentFrequency().equals("")
                        && (Constants.filter_Parameter.getGoal().equals("2") || Constants.filter_Parameter
                                .getGoal().equals("3"))) {


                    if (Constants.OPTIONS_FREQUENCY.equals("")) {
                        holder.priceText.setText(format.format(price) + " "
                                + Constants.OPTIONS_FREQUENCY
                                );
                    } else {
                        holder.priceText.setText(format.format(price) + " "
                                + propertyItem.getLetRentFrequency());
                    }
                }

                else if (Constants.g_Setttings.getApp_property_commercial() != null
                        && Constants.g_Setttings.getApp_property_commercial()
                        .equals("1")) {
                    if ((Constants.g_Setttings
                            .getIncl_property_commercial_sales() != null && Constants.g_Setttings
                            .getIncl_property_commercial_sales().equals("1"))
                            || Constants.g_Setttings
                            .getIncl_property_commercial_rent() != null
                            && Constants.g_Setttings
                            .getIncl_property_commercial_rent().equals(
                                    "1")) {

                        if (Constants.OPTIONS_FREQUENCY.equals("")) {
                            holder.priceText.setText(format.format(price) + " "
                                    + Constants.OPTIONS_FREQUENCY
                                    );
                        } else {
                            holder.priceText.setText(format.format(price) + " "
                                    + propertyItem.getLetRentFrequency());
                        }
                    }
                } else {

                    holder.priceText.setText(format.format(price));
                }

            }

            String contentText = "";
            String propertyTypeCode = propertyItem.getPT();
            String propertyType = "";
            if (propertyTypeCode != null && !propertyTypeCode.equals("")
                    && !propertyTypeCode.equals("null")
                    && !propertyTypeCode.equals("0")) {
                if (Constants.propertyTypeList != null
                        && Constants.propertyTypeList.size() > 0) {
                    for (int i = 0; i < Constants.propertyTypeList.size(); i++) {
                        if (Constants.propertyTypeList.get(i).getId() == Integer
                                .parseInt(propertyTypeCode)) {
                            propertyType = Constants.propertyTypeList.get(i)
                                    .getLabel();
                            break;
                        }
                    }
                } else {
                    propertyType = "";
                }
            } else {
                propertyType = "";
            }

            initRentOrSale();

            if (propertyItem.getBD() != null
                    && !propertyItem.getBD().equals("")) {
                if (propertyItem.getBD().equals("0")) {
                    if (!propertyType.equals("")) {
                        contentText = propertyType + " " + forRentOrSale + " ";
                    } else {
                        contentText = forRentOrSale + " ";
                    }
                } else {
                    if (!propertyType.equals("")) {
                        contentText += propertyItem.getBD() + " bed "
                                + propertyType + " " + forRentOrSale + " ";
                    } else {
                        contentText += propertyItem.getBD() + " bed "
                                + forRentOrSale + " ";
                    }
                }
            }
            if (propertyItem.getA2() != null
                    && !propertyItem.getA2().equals("")) {
                contentText += "in "
                        + Html.fromHtml(StringEscapeUtils
                                .unescapeHtml(propertyItem.getA2())) + " ";
            }

            if (contentText != null && !contentText.equals("")) {
                holder.content.setText(contentText);
            }

            imageLoader.displayImage(imageUrl(), holder.imageview,options, new ImageLoadingListener(){

                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    holder.progressbar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onLoadingFailed(String imageUri, View view,
                        FailReason failReason) {
                    holder.progressbar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingComplete(String imageUri, View view,
                        Bitmap loadedImage) {
                    holder.progressbar.setVisibility(View.GONE);
                }

                @Override
                public void onLoadingCancelled(String imageUri, View view) {
                    holder.progressbar.setVisibility(View.GONE);
                }

            });

            holder.layout = new RelativeLayout(getActivity());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    (int) (belowheight / 1.1));
            holder.layout.addView(rowView, params);
            holder.propertyInfoView.getLayoutParams().height = (int) (belowheight / 4.60);

            return holder.layout;
        }

    }




}

2 个答案:

答案 0 :(得分:0)

您可能正在混合GridLayoutGridView。它们是不同的实体。 在setUpGridLayout()中,您要为grid_view_imageviewitem充气并将其添加到GridLayout,而不是GridView

答案 1 :(得分:0)

*

i have solve the issue using           

Handler disconnectHandler = new Handler() {     
            public void handleMessage(Message msg) {     
            }    
        };    
        Runnable disconnectCallback = new Runnable() {    
            @Override    
            public void run() {     
                /////////// load data from here ////   
                }    
            }    

    };
disconnectHandler.postDelayed(disconnectCallback, 1000);*

*