滚动到GridView上的位置(使用GridViewWithHeaderAndFooter)

时间:2015-07-03 13:05:45

标签: java android gridview gridviewheader

我为我的应用程序使用GridViewWithHeaderAndFooter库。 在方法onCreate()中,我创建了我的经理,当用户点击某个项目时,他会存储位置。

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    goodsManager = new GoodsManager(getActivity(), this);
}

onCreateView中我创建了标题和gridView,以及您如何看待我在onItemClickListener中指定位置

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

    scAdapter = new AdapterForGoods(context);
    View view = inflater.inflate(R.layout.gridview_layout, container, false);
    headerView = inflater.inflate(R.layout.header_for_goods_layout, container, false);

    gridView = (GridViewWithHeaderAndFooter) view.findViewById(R.id.grid_view_with_header_and_footer);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (position != 0) {
                View concretView = view.findViewById(R.id.txtNameGoods);
                String nameShop = ((TextView) concretView).getText().toString();
                goodsManager.setPosition(position);
                goodsManager.replaceFragment((int) id, nameShop, new UiDescription());
                Log.e("GoodsList set position = ", String.valueOf(position));

            }
        }
    });

    gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem + visibleItemCount >= totalItemCount) {
                goodsManager.updateList();
            }
        }

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

    gridView.addHeaderView(headerView);
    gridView.setAdapter(scAdapter);

    return view;
}

在此活动中,我有方法goOnPosition(),当我从我的数据库接收数据时,它从Manager调用

public void goOnPosition(int p) {        
    gridView.smoothScrollToPosition(p);
 }

我从经理

调用方法goOnPosition
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
       goodsList.scAdapter.swapCursor(data);          
       goodsList.goOnPosition(getPosition()); 
}

当我启动应用程序gridView时,只在Header和1行滚动,无论我在方法中传输什么位置。 我想:当用户点击该项目时,我正在开始一个新片段,当我从片段返回时,我想显示他上次按下的位置。

0 个答案:

没有答案