在选择片段期间删除列表视图的重复项

时间:2016-04-11 04:43:51

标签: android

我创建了两个嵌套的片段选项卡。在两个片段选项卡之间滑动时添加了一个重复的列表项。只有在快速刷卡片段时才添加重复项。在android中删除listview的重复项的解决方案是什么?`

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

        final View rootview = inflater.inflate(R.layout.activity_explore_next, container, false);

        FontChangeCrawler fontChanger = new FontChangeCrawler(getContext().getAssets(), "arial.ttf");
        fontChanger.replaceFonts((ViewGroup) rootview);

        final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);

        hashSet.addAll(movieList);
        movieList.clear();
        movieList.addAll(hashSet);

        peoplelist = (ListView) rootview.findViewById(R.id.peoplelist);
        taglist = (ListView) rootview.findViewById(R.id.taglist);
        usermsg = (TextView) rootview.findViewById(R.id.usernotfound);
        search = (EditText) rootview.findViewById(R.id.searchbar);
        adapter = new CustomListAdapter2(getActivity(), movieList);
        peoplelist.setItemsCanFocus(true);
        peoplelist.setAdapter(null);
        peoplelist.setAdapter(adapter);
        peoplelist.setVisibility(View.GONE);
        adapter1 = new CustomListAdapter21(getActivity(), movieList1);
        taglist.setItemsCanFocus(false);

        taglist.setAdapter(adapter1);

        viewPager = (ViewPager) rootview.findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        viewPager.setOffscreenPageLimit(2);//Caching Fragment view

        PageListener pageListener = new PageListener();
        viewPager.setOnPageChangeListener(pageListener);

        search = (EditText) rootview.findViewById(R.id.searchbar);

        peoplelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                System.out.println("item clicked");
            }
        });

        movieList.clear();

        getPeople();
//        adapter.notifyDataSetChanged();

        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }





    private void setupViewPager(ViewPager viewPager) {

        ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
        adapter.addFragment(new PeopleFragment(), "PEOPLE");
        adapter.addFragment(new TagFragment(), "TAGS");
        viewPager.setAdapter(adapter);
        viewPager.requestTransparentRegion(viewPager);
    }

    public class PageListener extends ViewPager.SimpleOnPageChangeListener {
        public void onPageSelected(int position) {
            System.out.println("position in view pager" + position);
            movieList.clear();

            if (position == 0) {
                text = "people";
                search.setHint("Search people");
                usermsg.setText("No Username found");
                search.setText("");
                movieList.clear();
                getPeople();

            } else {
                text = "tag";
                search.setHint("Search hashtags");
                search.setText("");
                usermsg.setText("# No Hash tag found");
                movieList1.clear();
                getHashTag();

            }

        }
    }



    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
    @Override
    public void onDestroyView() {
        //mContainer.removeAllViews();
        ViewGroup mContainer = (ViewGroup) getActivity().findViewById(R.id.editprofilenext);
        mContainer.removeAllViews();

        mContainer.clearFocus();
        mContainer.removeAllViewsInLayout();

        ((View) movieList).clearFocus();


        adapter.notifyDataSetChanged();
        super.onDestroyView();
    }
}

0 个答案:

没有答案