RecyclerView with BottomNavigationView won't show changes in showing items - android

I've got problem with my "RecyclerView" and "BottomNavigationView" when I've tried to refactor my app and use Dependency Injection. The problem is when i click on items in Navigation View, "RecyclerView" won't change items in list. I have a code status for my HTTP request and base on that request I'm getting items. I've checked API and worked fine, also in "LOGCAT" got 200 Code. here's the code for the parts. when activity open app will show data with 0 status code, then when we change status code with bottom, nothing gonna change and still I'm getting same list.
This is not a duplicate since I'm using dependency injection and MVP, and I've also added dependency injection tag because this is happening when i use dependency injection and i'm beginner.
Activity: I'm calling methods in onCreate for data and View Here's the data.
private void setupData() {
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.new_request) {
mPresenter.setupLoadOrders("0");
} else if (item.getItemId() == R.id.canceled_request) {
mPresenter.setupLoadOrders("-1");
} else if (item.getItemId() == R.id.submited_request) {
mPresenter.setupLoadOrders("1");
} else if (item.getItemId() == R.id.added_request) {
mPresenter.setupLoadOrders("2");
}
return true;
}
});
mPresenter.setupLoadOrders("0");
}
private void setupView() {
recyclerRequests.setLayoutManager(new LinearLayoutManager(this));
recyclerRequests.setHasFixedSize(true);
}
#Override
public void displayOrders(List<Requests> requests) {
requestAdapter.setData(requests);
recyclerRequests.setAdapter(requestAdapter);
}
And Presenter :
#Override
public void setupLoadOrders(String statusReq) {
compositeDisposable.add(atlasApiReference.getRequest(Common.currentUser.getPhone(), statusReq)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Requests>>() {
#Override
public void accept(List<Requests> requests) throws Exception {
mView.displayOrders(requests);
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
Log.e("Log",throwable.getMessage());
}
}));
}
public void unSubscribe()
{
AtlasRxJavaUtil.unsubscribeClear(compositeDisposable);
}
And Adapter :
private List<Requests> requestsList;
#Inject
public RequestAdapter() {
requestsList = new ArrayList<>();
}
#NonNull
#Override
public RequestAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RequestAdapterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_detail_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull RequestAdapterViewHolder holder, final int position) {
holder.request_number.setText(new StringBuilder(" ").append(requestsList.get(position).getId()));
holder.request_name.setText(new StringBuilder(" ").append(requestsList.get(position).getNameReq()));
}
public class RequestAdapterViewHolder extends RecyclerView.ViewHolder {
public PsTextView request_number,request_name;
CardView request_detail_layout;
public RequestAdapterViewHolder(final View itemView) {
super(itemView);
request_number = (PsTextView)itemView.findViewById(R.id.request_number);
request_name = (PsTextView)itemView.findViewById(R.id.request_name);
request_detail_layout = (CardView)itemView.findViewById(R.id.request_detail_layout);
request_detail_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Common.currentRequest = requestsList.get(getAdapterPosition());
itemView.getContext().startActivity(new Intent(itemView.getContext(), ShowDetail.class));
}
});
}
}
#Override
public int getItemCount() {
return requestsList.size();
}
public void setData(List<Requests> requestsList){
this.requestsList.addAll(requestsList);
notifyDataSetChanged();
}

Try removing the following line:
recyclerRequests.setHasFixedSize(true);
More info on this post.

Related

How to implement select all button to select all items in recyclerview using actionmode and selectiontracker?

I have this code in my MainActivity
recyclerView.setAdapter(customAdapter);
customAdapter.submitList(path_list);
selectionTracker = new SelectionTracker.Builder<>(
"my-selection-id",
recyclerView,
new ScrollKeyProvider(1, path_list),
new ScrollItemDetailsLookup(recyclerView),
StorageStrategy.createLongStorage()
)
.withOnItemActivatedListener(new OnItemActivatedListener<Long>() {
#Override
public boolean onItemActivated(#NonNull ItemDetailsLookup.ItemDetails<Long> item, #NonNull MotionEvent e) {
Log.d("TAG", "Selected ItemId: " + item.toString());
return true;
}
})
.withOnDragInitiatedListener(new OnDragInitiatedListener() {
#Override
public boolean onDragInitiated(#NonNull MotionEvent e) {
Log.d("TAG", "onDragInitiated");
return true;
}
}).build();
customAdapter.setSelectionTracker(selectionTracker);
selectionTracker.addObserver(new SelectionTracker.SelectionObserver() {
#Override
public void onItemStateChanged(#NonNull Object key, boolean selected) {
super.onItemStateChanged(key, selected);
}
#Override
public void onSelectionRefresh() {
super.onSelectionRefresh();
}
#Override
public void onSelectionChanged() {
super.onSelectionChanged();
if (selectionTracker.hasSelection() && actionMode == null) {
actionMode = startSupportActionMode(new ActionModeController(ScrollActivity.this, selectionTracker));
actionMode.getMenu().findItem(R.id.action_item_count).setTitle("" + selectionTracker.getSelection().size());
} else if (!selectionTracker.hasSelection() && actionMode != null) {
actionMode.finish();
actionMode = null;
} else {
actionMode.getMenu().findItem(R.id.action_item_count).setTitle("" + selectionTracker.getSelection().size());
}
Iterator<String> itemIterable = selectionTracker.getSelection().iterator();
while (itemIterable.hasNext()) {
Log.i("TAG", itemIterable.next());
}
}
#Override
public void onSelectionRestored() {
super.onSelectionRestored();
}
});
This is my ActionMode callback code
public class ActionModeController implements ActionMode.Callback {
private final Context context;
private final SelectionTracker selectionTracker;
public ActionModeController(Context context, SelectionTracker selectionTracker) {
this.context = context;
this.selectionTracker = selectionTracker;
}
#Override
public boolean onCreateActionMode(androidx.appcompat.view.ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(androidx.appcompat.view.ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(androidx.appcompat.view.ActionMode mode, MenuItem item) {
if(item.getItemId()==R.id.action_clear){
if (selectionTracker.hasSelection()){
selectionTracker.clearSelection();
}
}
else if(item.getItemId()==R.id.action_select_all){
// **THIS IS PLACE WHERE I NEED HELP TO ENTER CODE FOR SELECT ALL FUNCTIONALITY**
}
return false;
}
#Override
public void onDestroyActionMode(androidx.appcompat.view.ActionMode mode) {
selectionTracker.clearSelection();
}
}
What should I do in select all section in onActionItemClicked for selecting all items in Recyclerview using SelectionTracker?
You can see for the clear option in onActionItemClicked, I have used functions of selectionTracker to clear all the selected items. I am expecting similar solution for select all option too.
Kotlin version using PagingDataAdapter - can be reworked replacing the snapshot to items in your implementation.
var itemsArray = arrayListOf<Long>()
adapter?.snapshot()?.items?.forEach {
if (!selectionTracker.isSelected(it.id.toLong()))
itemsArray.add(it.id.toLong())
}
selectionTracker?.setItemsSelected(itemsArray.asIterable(), true)

remove duplicates from List?

I am getting duplicate items from an API in my adapter but I want to remove those duplicates from my side means from my adapter and print it once any idea how? Thanks in advance.
got: duplicate rows in my cardview but through an API.
want: just want to print it once and remove those duplicates.
MyAdapter:
#Override
public void onBindViewHolder(#NonNull catView holder, int position) {
holder.id.setText(oilResponses.get(position).getId());
}
#Override
public int getItemCount() {
if (oilResponses.size() != 0) {
return oilResponses.size();
}
return 0;
}
MainActivity:
private void oilList() {
Api.getClient().do_oil(CAR_ID)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<List<OilResponse>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(#NotNull List<OilResponse> responses) {
oilAdapter = new OilAdapter(responses);
oilRecycler.setAdapter(oilAdapter);
}
#Override
public void onError(#NotNull Throwable e) {
}
#Override
public void onComplete() {
}
});
}
you can try to save them in a Set instead of a List, it is basically the same thing but in a set each object can be saved one time, so you can't have duplicates even if you'd like to.

MVP with Moxy. Presenter's callback methods in Fragment not called (getViewState() is not null)

I have a difficulties with MVP realization using Moxy library. I read Moxy's github pages and checked examples, but any solution doesn't helps me.
In MyFragment the callbacks of MyFragmentView methods not called, but in MyFragmentPresenter getViewState() returns not null.
I mean that
getViewState().showProgressDialog();
getViewState().setAccounts(accountsResponse);
getViewState().hideProgressDialog();
are called in MyPresenter, but in MyFragment
#Override
public void showProgressDialog() {
// some code
}
#Override
public void hideProgressDialog() {
// some code
}
#Override
public void setAccounts(AccountsResponse accounts) {
// some code
}
doesn't called.
Please help, what's wrong?
My code below.
Gradle
compile 'com.arello-mobile:moxy:1.5.5'
compile 'com.arello-mobile:moxy-app-compat:1.5.5'
compile 'com.arello-mobile:moxy-android:1.5.5'
annotationProcessor 'com.arello-mobile:moxy-compiler:1.5.5'
MyFragmentView
#StateStrategyType(AddToEndSingleStrategy.class)
public interface MyFragmentView extends MvpView {
void showProgressDialog();
void hideProgressDialog();
void showTextTestMessage(String s);
void showCouldNotRetrieveAccounts();
}
MyFragmentPresenter
#PerFragment
#InjectViewState
public class MyFragmentPresenter extends MvpPresenter<MyFragmentView> {
private ApiService apiService;
#Inject
public MyFragmentPresenter(ApiService apiService) {
this.apiService = apiService;
}
public void getAccounts() {
getViewState().showProgressDialog();
getCompositeDisposable().add(apiService.getAccounts()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(accountsResponse -> {
if (accountsResponse.err_code != 0) {
throw new LogicException(accountsResponse.message);
}
return accountsResponse;
})
.subscribe(
accountsResponse -> {
getViewState().setAccounts(accountsResponse);
getViewState().hideProgressDialog();
},
throwable -> {
getViewState().hideProgressDialog();
getViewState().showCouldNotRetrieveAccounts();
}
)
);
}
}
MyFragment
public class MyFragment extends MvpAppCompatFragment implements MyFragmentView {
#Inject
#InjectPresenter
public MyFragmentPresenter mPresenter;
#ProvidePresenter
public MyFragmentPresenter providePresenter() {
return mPresenter;
}
#Override
public void onActivityCreated (#Nullable Bundle savedInstanceState){
ComponentManager.getInstance().getActivityComponent(getActivity()).inject((MyActivity) getActivity());
super.onActivityCreated(savedInstanceState);
mPresenter.getAccounts();
}
#Override
public void showProgressDialog() {
// some code
}
#Override
public void hideProgressDialog() {
// some code
}
#Override
public void setAccounts(AccountsResponse accounts) {
// some code
}
#Override
public void showCouldNotRetrieveBankAccounts() {
// some code
}
}
**MyFragmentPagerAdapter **
public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
#Override
public Fragment getItem (int position){
switch (position) {
case 0:
//
case 1:
//
case 2:
return new MyFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
I think something went wrong with connecting dagger with moxy.
Check the following issue on GitHub:
https://github.com/Arello-Mobile/Moxy/issues/100
Guys solve the same problem there

Espresso's IdlingResource with Retrofit 2

I am new to Espresso, and trying to write a test on a fragment that makes a Retrofit call when it is instantiated. When the call is received, I want to check the fragment to see if a view exists. I'm using an IdlingResource for the fragment and setting up a listener to be called that transitions the ResourceCallback to idle when the response is received (followed some steps in the implementation here: https://stackoverflow.com/a/30820189/4102823).
My fragment is instantiated when the user logs in and starts up MainActivity. The problem is that I just don't think my IdlingResource is set up correctly, and I don't know what's wrong with it. It is not even constructed until after the fragment is initiated and the call is made, even though I'm registering the IdlingResource in the test's setUp() method before everything else. So, I think the main issue here is how I get the IdlingResource instantiated alongside the fragment when I run the test, not after it. Could the problem be in the #Rule? Does it start MainActivity (which creates a NewsfeedFragment instance) before the test can run on the fragment? If so, how would I use a rule with my fragment instead?
Here is my fragment:
public ProgressListener mProgressListener;
public boolean mIsProgressShown = true;
public interface ProgressListener {
void onProgressShown();
void onProgressDismissed();
}
public void setProgressListener(ProgressListener progressListener) {
mProgressListener = progressListener;
}
public NewsfeedFragment() {
}
public static NewsfeedFragment newInstance() {
return new NewsfeedFragment();
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OSUtil.setTranslucentStatusBar(getActivity().getWindow(), this.getContext());
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mRootView = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_newsfeed, container, false);
mNewsfeedFramelayout = (FrameLayout) mRootView.findViewById(R.id.newsfeed_framelayout);
mProgressView = (ProgressBar) mRootView.findViewById(R.id.newsfeed_progress);
mPageIndictorView = (FrameLayout) mRootView.findViewById(R.id.page_indicator);
mPageIndicator = (TextView) mRootView.findViewById(R.id.page_indicator_text);
mRootView.setOnRefreshListener(this);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) mRootView.findViewById(R.id.newsfeed_viewpager);
mPageChangeListener = this;
if (savedInstanceState != null) {
mTabPosition = savedInstanceState.getInt(EXTRA_TAB_POSITION);
mViewPager.setCurrentItem(mTabPosition);
}
fetchNewsFeed();
return mRootView;
}
private void fetchNewsFeed() {
if (NetworkUtils.isConnectedToInternet(getActivity())) {
if (NetworkUtils.getService() != null) {
if (mRootView.isRefreshing()) {
dismissProgress();
} else {
showProgress();
}
showProgress();
Call<Newsfeed> call = NetworkUtils.getService().getNewsfeed();
call.enqueue(new Callback<Newsfeed>() {
#Override
public void onResponse(Call<Newsfeed> call, Response<Newsfeed> response) {
if (response.isSuccessful()) {
dismissProgress();
mNewsfeed = response.body();
FragmentManager fragmentManager = getChildFragmentManager();
mNewsfeedPagerAdapter = new NewsfeedPagerAdapter(fragmentManager, mNewsfeed.getNewsfeedItems());
}
...
private void showProgress() {
// show the progress and notify the listener
if (mProgressListener != null){
setProgressIndicatorVisible(true);
notifyListener(mProgressListener);
}
}
public void dismissProgress() {
// hide the progress and notify the listener
if (mProgressListener != null){
setProgressIndicatorVisible(false);
mIsProgressShown = false;
notifyListener(mProgressListener);
}
}
public boolean isInProgress() {
return mIsProgressShown;
}
private void notifyListener(ProgressListener listener) {
if (listener == null){
return;
}
if (isInProgress()){
listener.onProgressShown();
}
else {
listener.onProgressDismissed();
}
}
Here is the IdlingResource:
public class ProgressIdlingResource implements IdlingResource, NewsfeedFragment.ProgressListener {
private ResourceCallback mResourceCallback;
private NewsfeedFragment mNewsfeedFragment;
public ProgressIdlingResource(NewsfeedFragment fragment) {
mNewsfeedFragment = fragment;
mNewsfeedFragment.setProgressListener(this);
}
#Override
public String getName() {
return "ProgressIdlingResource";
}
#Override
public boolean isIdleNow() {
return !mNewsfeedFragment.mIsProgressShown;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
mResourceCallback = callback;
}
#Override
public void onProgressShown() {
}
#Override
public void onProgressDismissed() {
if (mResourceCallback == null) {
return;
}
//Called when the resource goes from busy to idle.
mResourceCallback.onTransitionToIdle();
}
}
The fragment test:
public class NewsfeedFragmentTest {
#Before
public void setUp() throws Exception {
Espresso.registerIdlingResources(new ProgressIdlingResource((NewsfeedFragment) MainActivity.getCurrentFragment()));
}
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void getViewPager() throws Exception {
onView(allOf(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
withId(R.id.newsfeed_viewpager))).check(matches(isDisplayed()));
}
#Test
public void getNewsfeedItems() throws Exception {
onView(withId(R.id.page_indicator)).check(matches(isDisplayed()));
}
}
Retrofit is using OkHttp, and there is a standard way to setup IdlingResource for that matter. Refer to OkHttp IdlingResource

RecyclerView onBindViewHolder is called too many times for one cell

I am using RecyclerView with GridLayoutManager. For each item in the grid, I need to call a REST api to retrieve data. Then, after get data asynchronously from remote,I use UIL to load/display image.
Everything seems to be fine. But i find onBindViewHolder is called too too too many times for specific items.
logcat log
Talk is cheap, let me show you the code:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
EditCourseImageAttachmentHolder holder = (EditCourseImageAttachmentHolder) viewHolder;
holder.itemView.setLayoutParams(getLayoutParams(position));
holder.attachmentView.initData(dataSource.get(headerView == null ? position : position - 1), urlMap);
}
public static class EditCourseImageAttachmentHolder extends RecyclerView.ViewHolder {
public final AttachmentView attachmentView;
public EditCourseImageAttachmentHolder(AttachmentView itemView) {
super(itemView);
this.attachmentView = itemView;
}
}
-----------AttachmentView.Java-----------------
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
subscription = tryLoadLocalImage(data)
.subscribeOn(Schedulers.io())
.flatMap(new Func1<String, Observable<String>>() {
#Override
public Observable<String> call(String localImageUrl) {
if (StringUtils.isNotBlank(localImageUrl)) {
return Observable.just(localImageUrl);
}
if (urlMap.containsKey(data.getUid())) {
return Observable.just(urlMap.get(data.getUid()));
} else {
//Remote API call
String qiNiuUrl = getQiNiuUrl(data);
urlMap.put(data.getUid(), qiNiuUrl);
return Observable.just(qiNiuUrl);
}
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
#Override
public void call(String localImageUrl) {
showImage(localImageUrl);
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
}
});
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (subscription != null && subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
Root cause is I changed the LayoutParam of the ItemView....
It works well after i remove those code.
But, it's quite strange that only the item at specific position would re-bind.
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
your code should be this...

Categories

Resources