Android中的Gridview分页

时间:2011-03-17 10:32:54

标签: android

我从DB中检索文本+图像并将其显示在gridview.ieeach gridview单元格中包含一个图像+一个文本。现在我要为它做分页。我必须允许它每个显示4个单元格我怎么能这样做?非常感谢示例链接或代码。

4 个答案:

答案 0 :(得分:2)

//尝试这个---

gridView.setOnScrollListener(new AbsListView.OnScrollListener(){

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

        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (totalItemCount > 0) {
                int lastVisibleItem = firstVisibleItem + visibleItemCount;
                if (!isLoading && hasMoreItems && (lastVisibleItem == totalItemCount)) {
                    isLoading = true;
                    //load more items--

                }
            }
        }
    });

答案 1 :(得分:0)

我希望这有帮助..

我在Android培训页面链接“http://developer.android.com/training/implementing-navigation/index.html”上找到了一个示例,您可以下载一个示例EffectiveNavigation.zip,对于寻呼机非常有用。从那里你将获得适配器的基础知识和你需要的东西。

我通过分页获得网格的方式是:

有2个布局1与寻呼机

<android.support.v4.view.ViewPager ...

和一个网格视图

<GridView  android:id="@+id/gridview" ... 

注意:网格布局if包含在我遇到问题的其他组件中。

我在一个Activity中加载了寻呼机,在这里你加载了paginator作为android训练的例子我不添加这里的信息seance在那里很好地解释。 在这里我也有我的逻辑来加载项目数组,在这种情况下你从json响应加载它我在AsyncTask上完成一次完成:

protected void onPostExecute(String params) {
    refresList();
    progressDialog.dismiss();
}

我调用了refreshItems方法,这会使寻呼机适配器无法改变某些内容“mGridPagerAdapter.notifyDataSetChanged();”我还调用了一个刷新选项卡方法来重新计算要显示的选项卡。

public void refrestTabs() {
    actionBar.removeAllTabs();
    for (int i = 0; i < mGridPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mGridPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }
}

我已经在getItem方法中创建了一个扩展FragmentPagerAdapter的GridAdapter加载片段

@Override
public Fragment getItem(int i) {
    Fragment fragment = new GridListFragment();
    Bundle args = new Bundle();
    args.putInt(GridListFragment.ARG_SECTION_NUMBER, i);
    fragment.setArguments(args);
    return fragment;
}

类GridListFragment扩展了片段,这里你的逻辑加载你想要的数组部分。 这是我使用的onCreateView方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState) {
    Bundle args = getArguments();
    position =  args.getInt(ARG_SECTION_NUMBER);
    View view = inflater.inflate(R.layout.comp_grid_view, container, false); 
    gridView = (GridView) view.findViewById(R.id.gridview);
    applicationItems = new ArrayList<ApplicationItem>();
    int resID = R.layout.comp_app_list_item;
    aa = new ApplicationListItemAdapter(view.getContext(), resID, applicationItems);
    gridView.setAdapter(aa);
    gridView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            mCallback.onApplicationSelected(applicationItems.get(position));
        }}
    );
    return view;
}

我在调用onStart方法时刷新网格,我这样做:

@Override
public void onStart() {
    applicationItems.clear();   
    AppShortcutApplication appState = ((AppShortcutApplication)getActivity().getApplicationContext());
    List<ApplicationItem> l = appState.getCurrentListApplications();
    if (l != null){
        applicationItems.clear();
        applicationItems.addAll(l);
        aa.notifyDataSetChanged();      
    }
    super.onStart();
}

我通过应用程序状态传递项目,您可以在其他问题链接“How do I pass data between Activities in Android application?”上找到有关该项目的信息

同样,这可能不是最好的方法,但是找不到别的东西。

答案 2 :(得分:0)

这就是我在其中一个应用中所做的事情。希望它有所帮助。

public static int currentPage = 0;
private boolean endOfAlbums = false;
private int lastItem = 0;

albumGrid.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                    // Pause fetcher to ensure smoother scrolling when flinging // You can ignore this part
                    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                        mImageFetcher.setPauseWork(true);
                    } else {
                        mImageFetcher.setPauseWork(false);
                    }
                }

                @Override
                public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                    final int _lastItem = firstVisibleItem + visibleItemCount;
                    if (_lastItem > 0 && totalItemCount > 0)
                        if (_lastItem == albums.size() && !endOfAlbums && lastItem != _lastItem) {
                            lastItem = _lastItem;
                            // Last item is fully visible.
                            loadAlbums();
                        }
                }
            });

            return view;
        }

        private void loadAlbums() {

            if (currentPage == 0) {
                albums.clear();
                endOfAlbums = false;
                lastItem = 0;
                // get new releases
                progressLayout.setVisibility(View.VISIBLE);
            } else
                progressLoadMore.setVisibility(View.VISIBLE);

            if (Utility.isNetworkAvailable(getActivity())) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        // get the serverAlbums
                        ArrayList<Album> serverAlbums = ServerAPI.getAllAlbums(session.getLanguage(), currentPage);
                        if (serverAlbums.size() > 0)
                            albums.addAll(serverAlbums);
                        else
                            endOfAlbums = true;

                        currentPage++;

                        myHandler.post(updateRunnable);
                    }
                }).start();
                updateRunnable = new Runnable() {
                    @Override
                    public void run() {

                        if (albums.size() > 0) {

                            imageAdapter.notifyDataSetChanged();
                            // get listview current position - used to maintain scroll position
                            int currentPosition = albumGrid.getFirstVisiblePosition();

                            // Setting new scroll position
                            albumGrid.smoothScrollToPosition(currentPosition + 1, 0);
                        } else
                            tvNoAlbums.setVisibility(View.VISIBLE);

                        progressLayout.setVisibility(View.GONE);
                        progressLoadMore.setVisibility(View.GONE);
                    }
                };
            } else {
                Toast.makeText(getActivity(), R.string.check_connectivity, Toast.LENGTH_SHORT).show();
                progressLayout.setVisibility(View.GONE);
                progressLoadMore.setVisibility(View.GONE);
            }

        }

答案 3 :(得分:-1)

我认为这可能会对你有所帮助。它介绍了使用代码示例在GridView上显示多个项目的基础知识。

Android GridView Develop Guide