GridView分页但GridView内部的GridView未在Android

时间:2015-07-10 08:54:42

标签: android pagination android-gridview

片段调用时,我有问题显示gridview第一项。在布局文件

RelaltiveLayout
 then Imageview
    then gridview
      and bottom view.

当片段调用imageview向上移动时,只能从gridview查看。为什么? 我想从imageview显示视图。它意味着自动滚动到gridview。

gridview代码:

                    <co.utils.ExpandableHeightGridView
                        android:id="@+id/listProductDeals"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="15dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:horizontalSpacing="10dp"
                        android:isScrollContainer="false"
                        android:numColumns="2"
                        android:stretchMode="columnWidth"
                        android:verticalSpacing="10dp" >
                    </co.utils.utilsExpandableHeightGridView>

完整的Xml代码:http://pastebin.com/dvb9Ne6a

我在代码中做了什么:

public class ProductsSectionFragment extends Fragment implements OnClickListener {

   ScrollView scrollView;
    SwipeRefreshLayout swipeView;

    private ExpandableHeightGridView gridviewProductDeals;

    public ProductsSectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_main_products, container, false);


    scrollView = (ScrollView) rootView.findViewById(R.id.scrollview);

    swipeView = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe);

    swipeView.setEnabled(false);


    gridviewProductDeals = (ExpandableHeightGridView) rootView.findViewById(R.id.gridProductDeals);

    // gridviewProductDeals.setOnTouchListener(new View.OnTouchListener() {
    //
    // @Override
    // public boolean onTouch(View arg0, MotionEvent arg1) {
    // // TODO Auto-generated method stub
    // arg0.getParent().requestDisallowInterceptTouchEvent(true);
    // return false;
    // }
    // });

    myLastVisiblePos = gridviewProductDeals.getFirstVisiblePosition();
    Log.i("myLastVisiblePos ", "myLastVisiblePos=" + myLastVisiblePos);

    // gridviewProductDeals.setPadding(0, 0, 0, 100);
    // gridviewProductDeals.setClipToPadding(false);

    loadMoreView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.progress, null, false);
    // mLayoutParams.setMargins(0, 0, 0, 60);
    // gridviewProductDeals.addView(loadMoreView);
    loadData();

    swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
        swipeView.setRefreshing(true);
        (new Handler()).postDelayed(new Runnable() {
            @Override
            public void run() {
            swipeView.setRefreshing(false);

            }
        }, 3000);
        }
    });

    gridviewProductDeals.setOnScrollListener(new AbsListView.OnScrollListener() {
        int pageNumber = 2;

        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        final int lastInScreen = firstVisibleItem + visibleItemCount;
        int currentFirstVisPos = absListView.getFirstVisiblePosition();
        if (load(currentFirstVisPos, myLastVisiblePos, pageNumber)) {
            loading = true;
            // gridviewProductDeals.addFooterView(loadMoreView, null, false);
            (new LoadNextPage()).execute(String.valueOf(pageNumber));
            pageNumber++;
            Log.i("LOG", "After  LoadNextPage  " + pageNumber);
        }

        myLastVisiblePos = currentFirstVisPos;

        if (firstVisibleItem == 0) {
            swipeView.setEnabled(true);
            // Toast.makeText(getActivity(), "Swip to refresh Call", Toast.LENGTH_SHORT).show();
        } else
            swipeView.setEnabled(false);
        }
    });



    return rootView;
    }

    protected class LoadNextPage extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        getNewData(params[0].toString());
            return params[0].toString();
    }

    @Override
    protected void onPostExecute(String result) {


    }
    }

    protected boolean load(int currentFirstVisPos, int myLastVisiblePos, int pageNumber) {

    // boolean lastItem = firstVisibleItem + visibleItemCount == totalItemCount && gridviewProductDeals.getChildAt(visibleItemCount - 1) != null;
    // gridviewProductDeals.getChildAt(visibleItemCount - 1).getBottom() <= gridviewProductDeals.getHeight();
    // 2 >=2
    if (Variables.prouductsPageCount == null || Variables.prouductsPageCount.isEmpty()) {
        Variables.prouductsPageCount = "0";
    }

    boolean moreRows = Integer.parseInt(Variables.prouductsPageCount) >= pageNumber;

    return moreRows && currentFirstVisPos > myLastVisiblePos && !loading;
    }

    public void getNewData(final String pageNumber) {

    AQuery aQuery = new AQuery(getActivity());
    String url = Url.paginationProducts + String.valueOf(pageNumber) + ".json";

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("category", String.valueOf(Variables.categoryTwo));
    if (Common.isNetworkAvailable(getActivity())) {
        aQuery.ajax(url, params, JSONObject.class, new AjaxCallback<JSONObject>() {

        @Override
        public void callback(String url, JSONObject jsonObject, AjaxStatus status) {

            if (jsonObject != null) {
            try {
                JSONObject output_obj = jsonObject.getJSONObject(Variables.TAG_OUTPUT);
                String outputStatus = output_obj.getString("status");
                if (outputStatus.equalsIgnoreCase("1")) {

                Variables.ProductsData.addAll(Parser.parsePaginationProdcutsJson(jsonObject));
                CustomProductGridViewAdapter productAdapter = new CustomProductGridViewAdapter(getActivity(), R.layout.product_list_items, Variables.ProductsData);
                gridviewProductDeals.setAdapter(productAdapter);
                // gridviewProductDeals.setSelectionFromTop(top, 0);
                // gridviewProductDeals.removeFooterView(loadMoreView);
                productAdapter.notifyDataSetChanged();
                gridviewProductDeals.setExpanded(true);
                loading = false;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            } else {
            Log.e("ProductsData", "Couldn't get any data from the url");

            }

        }
        });
    } else {
        Log.e("ProductsData", "No Interent Connection");
    }

    }


}

当gridview在这里骂时,我没有检测到滚动。这个

public void onScroll(AbsListView absListView,int firstVisibleItem,int visibleItemCount,int totalItemCount){} 无法执行。这段代码有什么问题。

任何想法,如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

android:layout_width="wrap_content"
android:layout_height="wrap_content"

像列表视图一样的网格视图无法计算它的高度。你应该使用这样的东西:

<RelativeLayout>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"/>

<ExpandableHeightGridView
    android:layout_width="match_parrent"
    android:layout_height="match_parrent"
    android:layout_below="@+id/img_view"/>    
</RelativeLayout>

在这种情况下,图像视图将位于片段顶部,网格视图将位于图像视图的下方