适配器中的原始列表一旦过滤后就不会更新

时间:2018-07-27 06:46:26

标签: android android-fragments android-recyclerview android-adapter notifydatasetchanged

当我滚动列表时,它会按预期跟随pagination

但是,当我打开搜索视图并键入内容并重置editText并尝试加载下一个列表时,除非我滑动adapter或隐藏{ {1}}。

在两种情况下,我都将我的fragments设置为searchview,以便在我认为需要但您需要帮助的那次电话会议上对其进行更新。

昨天我进行了大量搜索,但不幸的是没有找到任何东西。希望我能在这里找到帮助。谢谢

MainActivity

edittext

}

Received_Fragment

null

Given_Fragment

public class MainActivity extends AppCompatActivity {
@BindView(R.id.vp_horizontal_ntb)
ViewPager viewPager;
@BindView(R.id.ntb_horizontal)
TabLayout topNavigationBar;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.appbar)
AppBarLayout mAppBar;
@BindView(R.id.searchIcon)
ImageView search;
@BindView(R.id.searchBoxLinOut)
LinearLayout searchBox;
@BindView(R.id.deleteSearchText)
ImageView deleteText;
@BindView(R.id.searchtext)
EditText et_search;
@BindView(R.id.imageView)
ImageView backButton;
String target = "nothing";
Context context;
Received_Fragment rf = new Received_Fragment();
Given_Fragment gf = new Given_Fragment();
InputMethodManager imm;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //StatusBarCompat.setStatusBarColor(this, 
   getResources().getColor(R.color.md_grey_300));
    setContentView(R.layout.layouot_all_reviews);
    ButterKnife.bind(this);
    context = this;
    viewPager.setOffscreenPageLimit(2);
    viewPager.setCurrentItem(0);
    viewPager.postDelayed(new Runnable() {

        @Override
        public void run() {
            setPosition();
        }
    }, 100);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
      WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    imm = (InputMethodManager) 
    context.getSystemService(Context.INPUT_METHOD_SERVICE);
    setUpTabs();
    handleClicks();

}

private void handleClicks() {

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int 
         positionOffsetPixels) {
            et_search.setText(null);
        }

        @Override
            public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
            et_search.setText(null);
        }
    });

    backButton.setOnClickListener(v -> {
        onBackPressed();
    });
    search.setOnClickListener(v -> {
        searchBox.bringToFront();
        if (searchBox.getVisibility() == View.VISIBLE) {
            hideSearch(searchBox);
        } else {
            showSearch(searchBox);
        }

    });
    deleteText.setOnClickListener(v -> {
        if (et_search.getText().length() > 1) {
            et_search.setText(null);
        } else hideSearch(searchBox);
    });

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

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, 
            int count) {
                if (s.toString().trim().length() == 0) {
                    rf.update();
                    gf.update();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (viewPager.getCurrentItem() == 0) {
                    rf.filter(s.toString());
                } else
                    gf.filter(s.toString());

            }
        });
    }


private void showSearch(View view) {
    TranslateAnimation animate = new TranslateAnimation(
            0,                 // fromXDelta
            0,                 // toXDelta
            -view.getHeight() * 2,              // fromYDelta
            0);                // toYDelta
    animate.setDuration(500);
    animate.setFillAfter(false);
    view.startAnimation(animate);
    view.requestFocus();
    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    view.setVisibility(View.VISIBLE);
    backButton.setEnabled(false);
}

private void hideSearch(View view) {
    TranslateAnimation animate = new TranslateAnimation(
            0,                 // fromXDelta
            0,                 // toXDelta
            0,                 // fromYDelta
            -view.getHeight() * 3); // toYDelta
    animate.setDuration(500);
    animate.setFillAfter(false);
    view.startAnimation(animate);
    view.setVisibility(View.INVISIBLE);
    backButton.setEnabled(true);
    et_search.setText(null);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
     finish();
}

private void setPosition() {
    target = getIntent().getStringExtra("WHERE");
    if (target.equals("given")) {
        switchFragment(1);
    } else {
        viewPager.setCurrentItem(0);
    }
}

public void switchFragment(int target) {
    viewPager.setCurrentItem(target);
}

private void setUpTabs() {
    viewPager.setAdapter(new 
   FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return rrf;
                case 1:
                    return rgf;
                default:
                    return new Default_Fragment();
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Received";
                case 1:
                    return "Given";
                default:
                    return "Error";
            }
        }
    });
    topNavigationBar.setupWithViewPager(viewPager);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

两个适配器都只附加一个适配器

AllRecievedAdapter

public class Received_Fragment extends Fragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.shimmer_view_container)
    ShimmerFrameLayout shimmerLayout;
    @BindView(R.id.msg)
    TextView message;
    //Initialization of Classes
    SessionManager session;
    ApiClient apiClient;
    List<ClassName> List = new ArrayList<>();

    Boolean isScrolling = false;
    boolean isDataLoaded = false;
    int currentItem, totalItem, scrollOutItem;
    //Properties to access
    Context context;
    String email;
    String userId;
    String access_token;
    RecievedAdapter adapter;
    BottomSheetBehavior sheetBehavior;
    LinearLayoutManager linearLayoutManager;
    private boolean isLastPage = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
    ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recycler_view_layout, 
        container, false);
        ButterKnife.bind(this, view);
        context = getContext();
        session = new SessionManager(context);
        HashMap<String, String> user = session.getUserDetails();
        email = user.get(SessionManager.KEY_EMAIL);
        access_token = user.get(SessionManager.KEY_ACCESS_TOKEN);
        userId = user.get(SessionManager.KEY_USER_ID);
        apiClient = ((Common) 
        getActivity().getApplication()).getClient().create(ApiClient.class);
        shimmerLayout.startShimmerAnimation();
        linearLayoutManager = new LinearLayoutManager(getContext());
        adapter = new RecievedAdapter(List, context, 
        recyclerView);
        adapter.setHasStableIds(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(feedsAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int 
             newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                currentItem = linearLayoutManager.getChildCount();
                totalItem = linearLayoutManager.getItemCount();
                scrollOutItem = linearLayoutManager.findFirstVisibleItemPosition();

                if (!isScrolling && !isLastPage) {
                    if ((currentItem + scrollOutItem) >= totalItem && scrollOutItem >= 0) {
                        LoadMoreData();
                    }


                }
            }


        });
        getData();
        return view;



    }




    public void filter(String text) {

        feedsAdapter.notifyDataSetChanged();
        List<Rating> temp = new ArrayList();
        for (Rating d : feedsList) {
            String tempF = "";
            for (int i = 0; i < d.getFeatures().size(); i++) {
                if (d.getFeatures().get(i).getName()!=null) {
                    tempF += d.getFeatures().get(i).getName()+",";
                    Log.d("This is name ", " Feature " + tempF);
                    if (tempF.toLowerCase().contains(text.toLowerCase())){
                        if (!temp.contains(d)){
                            temp.add(d);
                        }
                    }
                }
            }
            if (d.getName().toLowerCase().contains(text.toLowerCase())&& !d.getAnonymous()) {
                if (!temp.contains(d)){
                    temp.add(d);
                }

            }

        }
        //update recyclerview
        feedsAdapter.updateList(temp);

    }

    private void getData() {


                    List.addAll(allRecieved);
                    List.remove(0);
                    adapter = new RecievedAdapter(List, context, recyclerView);
                    shimmerLayout.setVisibility(View.GONE);
                    recyclerView.setAdapter(feedsAdapter);
                    recyclerView.invalidate();


    }

    private void LoadMoreData() {
        //Log.d("LoadMoreData", "Executing");
        isScrolling = true;

                        if (allRecieved.get(0).getSize() == 0) {
                            isLastPage = true;
                            Toast.makeText(context, "That's All", Toast.LENGTH_SHORT).show();
                        } else {
                            isScrolling = false;
                            allGiven.remove(0);
                            if (!List.contains(allRecieved)) {
                               List.addAll(allRecieved);
                                adapter.notifyDataSetChanged();
                            } else {
                                Toast.makeText(context, "Call Repeated", Toast.LENGTH_SHORT).show();
                            }

                        }
                    }

    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (adapter != null) {
            adapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void update() {
        adapter.updateOriginalList();
    }
}

如果您需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

更新列表后,呼叫feedsAdapter.notifyDataSetChanged();。重置notifyDataSetChanged()时,在列表更新之前,您正在呼叫edittext。您的filter方法更新了适配器中的列表,但是您正在notifyDataSetChanged进行更新。您需要在之后完成