为什么更改搜索数据时我的片段不更新?

时间:2019-01-12 20:57:37

标签: java android android-fragments searchview android-cardview

视图仅刷新一次并显示新卡片,我不知道为什么,当我更改为Tab3(谷歌地图)时。

移至tab3(谷歌地图)并再次移至tab1后(仅刷新Tab1,但不刷新tab2,启动后tab2在tab1上显示相同,但​​不刷新)[3]

问题是,如何实时刷新或代码发生什么变化?

一些MainActivity Java代码

private void filtrar() {
    List<Locales> data = AllLocales;
    List<Locales> output = new ArrayList<>();
    for (Locales item : data) {
        if (item.getNombre().toLowerCase().startsWith(getQuer().toLowerCase())) {
            output.add(item);
        } else {
            if (item.getRubro().toLowerCase().startsWith(getQuer().toLowerCase())) {
                output.add(item);
            }
        }
    }
    AllLocales = output;
}

public static class MapFragment extends Fragment implements OnMapReadyCallback{
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public MapFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static MapFragment newInstance(int sectionNumber) {
        MapFragment fragment = new MapFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);
        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

    }
}

public static class LocalesFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public LocalesFragment() {
    }

    public static LocalesFragment newInstance(int sectionNumber) {
        LocalesFragment fragment = new LocalesFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

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

        RecyclerView rv = rootView.findViewById(R.id.rv_recycler_view);
        rv.setHasFixedSize(true);
        Adaptador adapter = new Adaptador(AllLocales);
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }

}
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        if (position==2) {return  MapFragment.newInstance(position+1);}else{
            return LocalesFragment.newInstance(position + 1);}
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }
}

Adaptador.java

public class Adaptador extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private List<Locales> data;
    int total_types;

    class ViewHolder0 extends RecyclerView.ViewHolder {
        //Some code
        }
    }

    class ViewHolder1 extends RecyclerView.ViewHolder {
        //Some code
        }
    }

    class ViewHolder2 extends RecyclerView.ViewHolder {
        //Some code
        }
    }

    @Override
    public int getItemViewType(int position) {
        switch (parseInt(data.get(position).getNivelMiembro())) {
            case 0:
                return parseInt(data.get(position).getNivelMiembro());
            case 1:
                return parseInt(data.get(position).getNivelMiembro());
            case 2:
                return parseInt(data.get(position).getNivelMiembro());
            default:
                return -1;
        }
    }
    public Adaptador(List<Locales> data) {
        this.data = data;
        total_types = data.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //Some code
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder,final int position) {
       //Some code
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}

0 个答案:

没有答案