Coder Social home page Coder Social logo

Comments (9)

suvansh-dev avatar suvansh-dev commented on July 3, 2024 2

yes thanks it worked.

from blog-android-clean-coding-approach.

suvansh-dev avatar suvansh-dev commented on July 3, 2024 2

Thanks but it worked without adding a listener in ViewHolder as i said.

from blog-android-clean-coding-approach.

grasmussen-phunware avatar grasmussen-phunware commented on July 3, 2024

Hi @suvansh-dev 👋 ,

It is hard to tell with only having the one fragment class pasted here to see what issue might exist.

With the provided issue, I would not call this an open issue with the code in this repo, as it has been modified separately and it could be another issue.

Do you have a fork of this repo, with your commits, and that you could reference here in this conversation? Providing that public fork information would allow for me to look at the implementation to see where I can help.

Are there any binding errors printed out to LogCat?

With MVVM, where the View component can also include Activities and Fragments, whether the code is in an Activity or Fragment, it really does not matter.

Could double check through the implementation to see if anything else was changed?

Best,

Gregory

from blog-android-clean-coding-approach.

suvansh-dev avatar suvansh-dev commented on July 3, 2024

Hi @grasmussen-phunware ,
Here is the forked project https://github.com/suvansh-dev/blog-android-clean-coding-approach , I have set the recycler view background to black to check whether recycler view is visible or not and the background turned out to be black of the fragment but still the items are not visible plus there is no error which i am receiving in the logcat, its like the problem is with the setup()

from blog-android-clean-coding-approach.

suvansh-dev avatar suvansh-dev commented on July 3, 2024

@grasmussen-phunware This worked by this way:

FragmentBlankBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_blank, container, false);

       RecyclerView recyclerView = binding.getRoot().findViewById(R.id.data_recycler_view);
       recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
       recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), VERTICAL));

       dataViewModel = new DataViewModel();
       binding.setViewModel(dataViewModel);

       binding.executePendingBindings();
      
       recyclerView.setAdapter(dataViewModel.getAdapter());
       dataViewModel.setUp();
       
       return binding.getRoot();

But i don't know the reason why this need to be done and why the above code is working in activity but not in fragment.

from blog-android-clean-coding-approach.

grasmussen-phunware avatar grasmussen-phunware commented on July 3, 2024

Hi @suvansh-dev,

I see the issue with the original code, which creates a MyFragmentViewModel object twice and doesn't reuse the object reference. The class level scoped object reference, dataViewModel, should be set to a new instance of MyFragmentViewModel in method onCreate. Then, in method onViewCreated, the object instance should not be set again. It should be reused.

This will get you back on track.

I've attached a screenshot of the fragment in a working state from the forked repo as well as a screenshot of the project running with those changes on a Pixel 2 this morning.

Best,

Gregory

Screen Shot 2019-08-01 at 9 11 12 AM

device-2019-08-01-091712

I'm closing this issue, as it's not an issue with the project and the guidance will get the developer back on track.

from blog-android-clean-coding-approach.

grasmussen-phunware avatar grasmussen-phunware commented on July 3, 2024

You're welcome @suvansh-dev .

from blog-android-clean-coding-approach.

suvansh-dev avatar suvansh-dev commented on July 3, 2024

Hi, Where can we attach a click listener for RecyclerView items(for textview) as you have specified setUp(..) method for adding listeners but what we actually do is specifying up a method name in onClick attribute of xml and then use that method in the ViewModel class. So how does setUp(...) is useful in this case?

from blog-android-clean-coding-approach.

grasmussen-phunware avatar grasmussen-phunware commented on July 3, 2024

Hi @suvansh-dev,

With future questions, please open up a new issue and label it with the question label. Doing so will help keep issues relevant as well as helping others to better find what they are looking for when searching the issues.

When approaching making an MVVM implementation your own or creating your own, I recommend looking at it in terms of:

  1. the functional requirements
  2. how changes/additions adhere to the project's architecture (MVVM)
  3. keeping in mind best practices, programming principles, and more

To answer your question, you don't need to use method setUp(). It's just a possibility for listeners in general. One could simply add a click listener in a ViewHolder, and the click listener would then call a function on the corresponding ViewModel. Doing this does follow the MVVM Architecture pattern, where views can call on viewmodel's functions, which essentially provide commands for the View.

Best,

Gregory

from blog-android-clean-coding-approach.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.