amggg / YXing

483 stars 37 forks source link

很棒的代码不能在片段中使用它,请您帮忙 (很棒的代碼不能在片段中使用它,請您幫忙) #3

Open blazingdevil44 opened 3 years ago

blazingdevil44 commented 3 years ago

我的母语是英语,但是我发现这段代码很棒,我通过翻译阅读了所有文档,但是我似乎无法在片段中使用它,我正在使用底部导航视图,并且有5个片段,但是我可以 使其无法正常工作,请您帮我一下,或者给我一些如何在片段中使用的示例代码,请先谢谢

我的母語是英語,但是我發現這段代碼很棒,我通過翻譯閱讀了所有文檔,但是我似乎無法在片段中使用它,我正在使用底部導航視圖,並且有5個片段,但是我可以 使其無法正常工作,請您幫我一下,或者給我一些如何在片段中使用的示例代碼,請先謝謝

My native language is English, but I found this code is great, I read all the documents through translation, but I can't seem to use it in fragments, I am using the bottom navigation view, and there are 5 fragments but I can’t make it work properly, can you please help me or give me some sample code on how i can use this inside a fragment thank you in advance

amggg commented 3 years ago

In fragment you can use like this:

ScanCodeConfig.create(context, mFragment) //style ScanStyle.NONE:non ScanStyle.QQ :QQ ScanStyle.WECHAT :Wechat ScanStyle.CUSTOMIZE : CUSTOMIZE .setStyle(style) .buidler() .start(ScanCodeActivity.class);

context is the activity to which the fragment is attached, and mFragment is the current fragment ,Receive the scan code result in the onActivityResult of the fragment ,thanks.

blazingdevil44 commented 3 years ago

i did this but the problem is it goes to another ScanCodeActivity instead on opening it it inside the fragment i want it to show in inside the fragment

e.g it should be seen inside this fragment

Screenshot_20210130-162352

but instead staying here it opens new activity

Screenshot_20210130-162544

when i back press it goes to picture 1 my code is below

in Main Activity //Code Below

private lateinit var bottomNavigationView:BottomNavigationView

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)

    loadLocale()

    setContentView(R.layout.activity_main)
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

    checkPermission()

    bottomNavigationView =
        findViewById<BottomNavigationView>(R.id.bottom_navigation)
    bottomNavigationView.setOnNavigationItemSelectedListener(bottomNavMethod)
    supportFragmentManager.beginTransaction()
        .replace(R.id.fragment_container, ScannerFragment(), "MainFrag").commit()

}

private val bottomNavMethod = BottomNavigationView.OnNavigationItemSelectedListener { item -> var fragment: Fragment = ScannerFragment() when (item.itemId) { R.id.navigation_scanner -> fragment = ScannerFragment() R.id.navigation_generate -> fragment = CreateFragment() R.id.navigation_history -> fragment = HistoryFragment() R.id.navigation_imgToPDf -> fragment = ImgToPdfFragment() R.id.navigation_setting -> fragment = SettingFragment() } supportFragmentManager.beginTransaction() .replace(R.id.fragment_container, fragment).commit() true }

And inside my Fragment

//Code below

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_scanner, container, false) return view }

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val myFragment: Fragment = activity!!.getSupportFragmentManager().findFragmentByTag("MainFrag") as Fragment
    ScanCodeConfig.create(activity, myFragment)
        //style ScanStyle.NONE:non ScanStyle.QQ :QQ ScanStyle.WECHAT :Wechat ScanStyle.CUSTOMIZE : CUSTOMIZE
        .setStyle(ScanStyle.WECHAT)
        .buidler()
        .start(ScanCodeActivity::class.java)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    //Receive scan result
    if (resultCode == Activity.RESULT_OK && requestCode == ScanCodeConfig.QUESTCODE && data != null) {
        val extras: Bundle? = data.extras
        if (extras != null) {
            val code = extras.getString(ScanCodeConfig.CODE_KEY)
            //tvCode.setText(String.format("%s%s", "结果: ", code))
        }
    }
}

override fun onStart() { super.onStart() val view = view if (view != null) { //Loading some ads stuff.... } }

//Code End

//Code End 

instead of opening new activity i want to open it inside my fragment i know that i have to use Customize style and make pass it instead of .start(ScanCodeActivity.class);

but i tried but it with different things but i cant make it work can u please help me with this thanks in advance