Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

huangapple go评论79阅读模式
英文:

Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

问题

我是一个完全新手的Android开发者,目前有些迷茫。我正在按照这个教程尝试在我的应用中实现绘图功能,这个应用是一个带有三个标签页(或片段)的“底部导航活动”项目。请参见下面的截图:

Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

问题是,该教程是针对一个只有一个视图和一个标准MainActivity的应用程序的。在这种情况下,以下代码用于项目的MainActivity.java类中的onCreate()方法:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PaintView paintView = new PaintView(this);
        setContentView(paintView);
    }
}

当我建立一个只有一个视图的应用程序时,代码运行得很好,没有问题。但是在我的“底部导航活动”项目中就无法工作,因为片段使用了ViewModel类型,该类型不提供View类所需的许多方法。我的应用程序包含3个片段,第一个片段命名为HomeFragment。我想要在这个片段中进行所有的绘图。这个片段的默认onCreateView()方法如下:

public class HomeFragment extends Fragment {
 
    private HomeViewModel homeViewModel;
    
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        return root;
    }
}

请查看下面的截图,了解我尝试过的内容。你会看到,我在我的项目中添加了一个名为PaintViewView类(就像在教程中创建的那样),其中包含所有的绘图代码。

Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

不幸的是,它生成了以下编译错误:

Inferred type com.example.mobile_testapp_android_2_ui.home.PaintView for type parameter T is not within its bound; should extend androidx.lifecycle.ViewModel

对于如何实现教程中的PaintView类以便我可以在“HomeFragment”上使用其方法进行绘图,是否有任何提示,我会非常感激。

非常感谢,

Wulf

英文:

I'm a completely new Android dev and am rather lost at the moment. I'm following this tutorial as I try to enable drawing to a view in my app which is a "Bottom Navigation Activity" project that features three tabs--or fragments. See screenshot below:

Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

The problem is that the tutorial is intended for an app with a single View and a single standard MainActivity. In this case, the following code is used in the onCreate()method in the project's MainActivity.java class:

public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState):
PaintView paintView = new PaintView(content: this):
setContentView(paintView);
  }

}

When I build a single view app, the code works great. No issues. But things can't work in my "Bottom Navigation Activity" project, because the fragments use a ViewModel type that doesn't offer the much needed methods of the View class. My app features 3 fragments, the first of which is named HomeFragment. It is on this fragment that I want all my drawing to take place. The default onCreateView() method for this fragment looks like so:

public class HomeFragment extends Fragment {
 
private HomeView Model homeViewModel;

public View onCreateView(@NonNull LayoutInflator inflater, ViewGroup container, Bundle savedInstanceState) {

homeViewModel = ViewModelProviders.of(fragment: this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, attachToRoot: false);

return root;
 }
}

See screenshot below to see what I've tried to do. You will see that I've added a View class to my project called PaintView (just as created in the tutorial), which contains all the drawing code.

Android drawing app intended for a single View needs to work in multi-view Bottom Navigation Activity app

Unfortunately it generates the following compilation error:

Inferred type com.example.mobile_testapp_android_2_ui.home.PaintView for type parameter T is not within its bound; should extend androidx.lifecycle.ViewModel

Any tips on how I can implement the tutorial's PaintView class so that I can use its methods to draw on the "HomeFragment" would be deeply appreciated.

Thank you and very cordially,

Wulf

答案1

得分: 1

你正在尝试使用 PaintView 类来创建一个 HomeViewModel 对象,但这是不可能的。如果你想将 PaintView 设置为 HomeFragment 的视图,你需要返回一个它的对象:

// 你可以返回任何你喜欢的视图对象
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    PaintView homeFragmentView = new PaintView(requireContext());

    // PaintView 类必须继承自 View 类
    return homeFragmentView;

}

希望对你有所帮助。

英文:

What you are doing is trying to create a HomeViewModel object with the PaintView class which is not possible. If you want to set PaintView as the view of your HomeFragment, you need to return an object of it as :

    //you can return any kind of view object as you like
    public View onCreateView(@NonNull LayoutInflator inflater, ViewGroup container, Bundle savedInstanceState) {
    
        PaintView homeFragmentView = new PaintView(requireContext());

        // PaintView class must extends View class
        return homeFragmentView;

    }

I hope, this helps.


Learn more about Fragment here

Learn more about ViewModel here

huangapple
  • 本文由 发表于 2020年5月29日 07:43:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/62076315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定