notifyDataSetChanged()不适合我

时间:2014-12-22 05:10:59

标签: android notifydatasetchanged

我试图加速ListView。 在tab_3中我想点击项目,在标签2中我想通过这个项目获得查询。 但是当我从选项卡3滑动到选项卡2时,选项卡2中的ListView不会更新。我认为标签2的视图之前创建了,但是如何通过点击标签3中的listvew更新标签2上的列表视图?

我的来源:

public void onViewCreated(View view, Bundle savedInstanceState) {
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
    mViewPager.setAdapter(new SamplePagerAdapter());
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setViewPager(mViewPager);
}

class SamplePagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return 3;
    }


    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    public Object instantiateItem(ViewGroup container, int position) {
        switch (position) {
            case 0:
                View view0 = getActivity().getLayoutInflater().inflate(R.layout.tab_1, container, false);
                Log.d(TAG, "Tab case 0");
                container.addView(view0);
                return view0;

            case 1:
                View view1 = getActivity().getLayoutInflater().inflate(R.layout.tab_2, container, false);
                Log.d(TAG, "Tab case 1 ");
                ListView lvLibList = (ListView)view1.findViewById(R.id.lvLibList);
                Database database = new Database(getActivity());
                if (NAME != null) {
                    mNames = database.getListByName(NAME);

                } else {
                    mNames = database.getListByName(" ");
                }
                NameListAdapter namelistAdapter = new NameListAdapter(mMedias);
                lvNameList.setAdapter(namelistAdapter);
                namelistAdapter.notifyDataSetChanged();
                container.addView(view1);
                return view1;
            default:
                View view2 = getActivity().getLayoutInflater().inflate(R.layout.tab_3, container, false);
                Log.d(TAG, "Tab 3 ");
                ListView lvSourceList = (ListView)view2.findViewById(R.id.lvSourceList);
                Database mb = new Database(getActivity());
                mFull = mb.getFull();
                FullAdapter fullAdapter = new FullAdapter(mFull);
                lvFullList.setAdapter(fullAdapter);

                lvFullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view1,
                                            int position, long id) {

                        Full f = (Full)parent.getAdapter().getItem(position);
                        NAME = f.getName(); 
                        Log.d(TAG, NAME);
                    }
                });
                container.addView(view2);
                return view2;
        }
    } 

1 个答案:

答案 0 :(得分:0)

您应该使用setUserVisibleHint方法编写它。像这样覆盖它。适当重新加载适配器并调用notifyDataSetChanged

  public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {

    }
    else {

    }
  }