StaggeredGridView只能正确刷新顶部

时间:2013-10-11 20:46:01

标签: android baseadapter

我使用https://github.com/bulletnoid/StaggeredGridView这个库来制作一个pinterest样式布局,并在此布局上使用pulltorefresh。此外,还有一个幻灯片菜单,可以选择不同的类别,并根据用户选择的类别,刷新并再次填充。

Pulltorefresh工作正常。

如果用户位于布局顶部并在幻灯片菜单上选择一个类别,则它可以正常工作。但是,如果用户在布局底部并在幻灯片菜单上选择一个类别,则其工作不正确。

场景,布局顶部并在slidemenu上选择类别并重新填充交错布局。它正常工作

Top of the layout enter image description here

场景,布局底部并在slidemenu上选择类别并重新填充交错布局。它无法正常工作

bottom of the layout enter image description here

- > listviewAdapter

public void onItemClick(AdapterView<?> parent, View view, int position,
            long arg3) {
        // TODO Auto-generated method stub

        switch (parent.getId()) {
        case R.id.listView_sliding_menu:

            smenu.toggle();

            slidingMenuControl = true;
            String categoryId = ((TextView) view.findViewById(R.id.categoryID))
                    .getText().toString();

            parameters[0] = categoryId;
            Toast.makeText(getApplicationContext(), categoryId,
                    Toast.LENGTH_LONG).show();
            new PARSEJSONCATEGORYCONTENT().execute(parameters);

            break;

        default:
            break;

        }

    }

- &gt;解析器

private class PARSEJSONCATEGORYCONTENT extends
                AsyncTask<String[], Void, ArrayList<Utils>> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                processDialoge();
            }

            protected ArrayList<Utils> doInBackground(String[]... params) {

                String catId = params[0][0];
                String startCount = params[0][5];
                String count = params[0][6];
                String urlCatContent = "http://212.58.8.109/webservice/api/content/cat/";

                jArray = jsonParser.getJSONFromUrltoCategoryContent(urlCatContent,
                        Token, tokenValue, catId, startCount, count);

                if (utilsArray == null) {
                    utilsArray = new ArrayList<Utils>();

                } else if (slidingMenuControl == true) {

                    utilsArray.clear();

                } else if (contentItemSelection != null) {
                    utilsArray.clear();
                }

                try {

                    // looping through All Contacts
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject k = jArray.getJSONObject(i);
                        utils = new Utils();

                        // Storing each json item in variable
                        utils.imageUrl = k.getString("ipad_URL");
                        utils.imageWidth = k.getInt("ipad_width");
                        utils.imageHeight = k.getInt("ipad_height");
                        utils.categoryHeader = k.getString("contentHeader");
                        utils.contentDesc = k.getString("contentDesc");
                        utils.categoryContentId = k.getInt("id");
                        utils.contentTxt = k.getString("contentTxt");

                        Log.d("ipad_URL", utils.imageUrl);

                        utilsArray.add(utils);

                    }

                    String arrayLenght = Integer.toString(utilsArray.size());
                    Log.d("arrayLenght", arrayLenght);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return utilsArray;
            }

            protected void onPostExecute(ArrayList<Utils> utilsArray) {

                staggeredAdapter.getMoreItemm(utilsArray);

                // staggeredAdapter.setRefreshListener(false);
                super.onPostExecute(utilsArray);
                slidingMenuControl = false;
                dialog.cancel();
            }

        }

- &GT; BaseAdapter.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
public class StaggeredAdapter extends BaseAdapter implements OnClickListener {

    Typeface tf;
    boolean refreshListener = false;
    Utils utils;

    private Context mContext;
    private Application mAppContext;
    private ArrayList<Utils> mUtilsArraylist = new ArrayList<Utils>();

    public StaggeredAdapter(Context context, Application application) {

        mContext = context;
        mAppContext = application;
        tf = Typeface.createFromAsset(mContext.getAssets(),
                "font/Klavika-Medium.otf");
        notifyDataSetChanged();

    }

    public void getMoreItemm(ArrayList<Utils> arrayList) {
        mUtilsArraylist.clear();
        mUtilsArraylist.addAll(arrayList);
        this.notifyDataSetChanged();
    }

    public int getCount() {

        return mUtilsArraylist == null ? 0 : mUtilsArraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return mUtilsArraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mUtilsArraylist.indexOf(getItem(position));
    }

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

        View view = null;
        utils = mUtilsArraylist.get(position);

        if (convertView == null) {
            Holder holder = new Holder();
            view = View.inflate(mContext, R.layout.staggered_item, null);

            holder.imgUrl_content = (STGVImageView) view
                    .findViewById(R.id.imgUrl_content);

            holder.tv_info = (TextView) view.findViewById(R.id.contentHeader);
            holder.tv_info.setTypeface(tf);

            holder.tv_info2 = (TextView) view.findViewById(R.id.contentDesc);
            holder.tv_info2.setTypeface(tf);

            view.setTag(holder);
        } else {

            return convertView;
        }

        final Holder holder = (Holder) view.getTag();

        holder.imgUrl_content.mHeight = utils.imageHeight;
        holder.imgUrl_content.mWidth = utils.imageWidth;
        holder.imgUrl_content.setOnClickListener(this);

        ImageLoader imgLoader = new ImageLoader(mAppContext);
        imgLoader.DisplayImage(utils.imageUrl, holder.imgUrl_content);

        holder.tv_info.setText(utils.categoryHeader);
        holder.tv_info.setOnClickListener(this);
        holder.tv_info2.setText(utils.contentDesc);
        holder.tv_info2.setOnClickListener(this);

        return view;
    }

    class Holder {
        public STGVImageView imgUrl_content;
        public TextView tv_info;
        public TextView tv_info2;

    }

    public boolean isRefreshListener() {
        return refreshListener;
    }

    public void setRefreshListener(boolean refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub

        switch (view.getId()) {
        case R.id.imgUrl_content:
            sendDataItemContentActivity();
            break;

        case R.id.contentHeader:
            sendDataItemContentActivity();
            break;

        case R.id.contentDesc:
            sendDataItemContentActivity();
            break;

        default:
            break;
        }

    }

    public void sendDataItemContentActivity() {

        Intent intent = new Intent(mContext, ItemContent.class)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("contentTxt", utils.contentTxt);
        intent.putExtra("contentHeader", utils.categoryHeader);
        intent.putExtra("contentİmageUrl", utils.imageUrl);
        intent.putExtra("contentCategoryName", utils.categoryName);

        Bundle animBundle = ActivityOptions.makeCustomAnimation(mContext,
                R.anim.anim, R.anim.anim2).toBundle();

        mContext.startActivity(intent, animBundle);

    }

}

1 个答案:

答案 0 :(得分:0)

我想我和你一样使用相同的pinterest样式库也有同样的问题。我把这个问题解决了我的问题

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mAdapter = new SearchSTGVAdapter(getActivity(), adsList, (Fragment)this);       
    ptrstgv.setAdapter(mAdapter);

}

之前我把它放在onCreate方法中 为了您的信息,我在视图分页器中实现了这种pinterest样式而不是滑动菜单