Tag: android fragments

Kotlin-android-extension类之间的通信类似于与其他片段进行通信

我有3个部分到我的项目:一个模型,做计算,一些片段,显示用户界面,并发送触发到我的第三部分,主要活动。 我做了所有我的片段与一些接口,如与其他片段通信 。 但是现在我需要模型的一部分来触发一些用户界面的变化。 我不知道该怎么做。 因为我的目标是让我的模型的一部分发送或触发一些函数,以便GUI得到更新,但它本身并不知道GUI。 (它是完全独立的)在主要活动中,我覆盖了所有的功能 class MainActivity : AppCompatActivity(), MimaFragment.elementSelectedListener, InstructionFragment.instructionSaveButtonClickedCallback , OptionFragment.optionSaveButtonClickedCallback, MimaFragment.UITrigger{ override fun abortOptions() { extendNormal() } override fun updateMima() { mimaFragment.updateView() } override fun normal() { mimaFragment.drawArrows() }} 片段exapmle: class OptionFragment : Fragment() { var optionCallback : optionSaveButtonClickedCallback? = null interface optionSaveButtonClickedCallback{ fun updateMima() fun abortOptions() } override fun […]

Android支持库27,片段更新?

由于我更新了我的项目到SDK版本27和支持库的gradle插件到版本27.0.0我需要更改我的代码。 26.1.0我可以在我的Fragment ( android.support.v4.app )中使用getContext() (与Kotlin context ),我没有可空性的问题,但是因为我使用Kotlin我有一个版本27.0.0的问题,我所有的context调用都不再工作,我需要一个安全操作员,就像context!!一样context!! ,但是因为我个人认为这是一个忙碌的做法,每次我只是让自己的解决方案功能 override fun getContext() = super.getContext()!! 另一个改变(突然,这就是为什么我问)是onCreateView()和onViewCreated() 。 在onCreateView的inflater不可能是null了,所以我需要改变我的函数签名正确覆盖从onCreateView(inflater: LayoutInflater?…) onCreateView(inflater: LayoutInflater…)和createdView参数相同onViewCreated 。 所以现在我想知道为什么,特别是(对于Kotlin)非常丑陋的getContext()更改,并转向https://developer.android.com/sdk/support_api_diff/27.0.0/changes.html 。 但是等一下,显然他们没有改变呢? 所以现在我的问题是如果我做错了什么,或者如果他们真的改变了,如果是的话,我可能会问他们为什么? 顺便说一下,同样适用于getActivity() ,我认为mHost == null检查被添加,并且getActivity方法甚至是最终的,所以我不能在那里使用我的解决方法,这使得它非常非常难看。 实际上在源文件中的方法看起来是一样的,但26.1.0有Kotlin返回类型的Context! 和27.0.0返回类型的Context? 。

ListView可以出现在Activity中,但不能出现在Fragment中

ListView可以出现在Activity但不能出现在Fragment但是我在Android Studio 3.0中使用Kotlin中的相同代码? 这是我的片段代码: class testFrag : Fragment() { var adapter : mo3dAdapter?=null var listOfMkabala = ArrayList<MeetingDetails>() override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater!!.inflate(R.layout.fragment_test, container, false) listOfMkabala .add ( MeetingDetails(" nour1", "ahmed1" , "aya1")) listOfMkabala .add ( MeetingDetails(" nour2", "ahmed2" , "aya2")) listOfMkabala .add ( MeetingDetails(" nour3", "ahmed3" , "aya3")) […]

在添加/替换片段中的任何代码改进

我已经开始研究Kotlin,不知道这门语言的所有功能。 该函数用于在FrameLayout中显示片段。 逻辑是这样的,它第一次应该总是add()片段,下次它会replace() 。 另外在某些情况下,我需要使用addToBackStack() ,并在相同的情况下禁用左侧菜单。 fun showFragment(fragment : Fragment, isReplace: Boolean = true, backStackTag: String? = null, isEnabled: Boolean = true) { /* Defining fragment transaction */ val fragmentTransaction = supportFragmentManager .beginTransaction() /* Select if to replace or add a fragment */ if(isReplace) fragmentTransaction.replace(R.id.frameLayoutContent, fragment, backStackTag) else fragmentTransaction.add(R.id.frameLayoutContent, fragment) /* Select if to add […]

片段:RelativeLayout可见性在OnCreateView中不可更改

(即时通讯使用Kotlin)所以这里是我的OnCreateView在片段。 override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view: View = inflater!!.inflate(R.layout.fragment_bots, container, false) BotDiv2.visibility = View.VISIBLE startUp() return view } 这里是相关布局的xml: <RelativeLayout android:id="@+id/BotDiv2" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="33.3" android:visibility="invisible"> <ImageButton android:id="@+id/BotBtn1" android:layout_width="90dp" android:layout_height="90dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@null" android:scaleType="fitCenter" android:src="@android:drawable/btn_star_big_on" /> <TextView android:id="@+id/uselessLevel1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/BotBtn1" android:layout_alignStart="@+id/BotBtn1" android:text="Level:" /> <TextView android:id="@+id/BotWorth1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/BotBtn1" […]

使用Android数据绑定在Kotlin中初始化RecyclerView会引发错误。 我究竟做错了什么?

描述 我正尝试在Kotlin中使用Android数据绑定的RecyclerView设置Android屏幕。 我有一个活动,其中包含一个片段和片段包含一个RecyclerView,我初始化与数据绑定。 问题是,当我尝试设置我的回收视图的布局管理器应用程序抛出一个IllegalStateException抱怨指定的孩子(我假设它意味着无论是RecyclerView或LayoutManager)已经有一个父母,我需要运行removeView )。 我试图从RecyclerView中删除所有视图,但无济于事。 这是我的活动: class HomeActivity : BaseActivity() { private lateinit var mBinding: ActivityHomeBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mBinding = DataBindingUtil.setContentView(this, R.layout.activity_home) if(savedInstanceState != null){ return } val fragment = HomeFragment.newInstance() supportFragmentManager.beginTransaction().add(R.id.home_fragment_container, fragment).commit() } } 这是我的片段: class HomeFragment : BaseFragment() { private lateinit var mBinding: FragmentHomeBinding private lateinit var mRecyclerView: […]

Getpreferences不能在片段中工作

像findViewById , getSharedPreferences命令不工作内部Fragment我正在使用kotlin,我的代码如下 fun update (v:View){ Val sharedpref = getSharedPreferences("logindata",Context.MODE_PRIVATE)} LOG E/AndroidRuntime: FATAL EXCEPTION: main Process: com.techno.app, PID: 25691 java.lang.IllegalStateException: Could not find method update(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:5721) at android.widget.TextView.performClick(TextView.java:10936) at android.view.View$PerformClick.run(View.java:22620) at android.os.Handler.handleCallback(Handler.java:739)

如何在Android上用Kotlin替换一个FrameLayout

我试图用Kotlin开发一个Android应用程序,但是当试图动态地移动碎片时,我遇到了一些麻烦。 我想要做的是用一个片段替换活动布局中的一个FrameLayout。 目前,每当我尝试运行我的应用程序,它只是在工具栏下显示一个白色的屏幕,导致我相信这个片段没有以我期望的方式被添加到FrameLayout。 这是我做第一笔交易的主要活动: package net.ma.ttrobinson.kchan import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.Button import android.widget.EditText import com.androidquery.callback.AjaxStatus import net.ma.ttrobinson.kchan.api.ChanThread import net.ma.ttrobinson.kchan.api.Request import org.jdeferred.DoneCallback import org.jdeferred.FailCallback import org.json.JSONArray import org.json.JSONObject class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolbar = […]

Kotlin:如何在android studio中用新片段替换已经打开的片段?

我只是学习在android studio中使用片段。 我试图用PickPowerFragement片段替换一个MainFragment片段,但是当我按下MainFragment中的按钮来替换MainPragment与PickPowerFragment应用程序崩溃。 我能够成功地加载在教程中给出的java中的PickPowerFragment,但是当我在Kotlin中尝试这样做时,应用程序崩溃(仅用于练习)。 那么如何使用Kotlin语法来替换片段 activity_hero_me.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mandar.herome.Activities.HeroMe" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="25dp"> <FrameLayout android:id="@+id/ReplaceFrame" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"> </FrameLayout> </android.support.constraint.ConstraintLayout> HeroMe.kt package com.example.mandar.herome.Activities import android.app.Activity import android.app.Fragment import android.app.FragmentManager import android.net.Uri import android.os.Bundle import com.example.mandar.herome.Fragments.MainFragment import com.example.mandar.herome.Fragments.PickPowerFragment import com.example.mandar.herome.R class HeroMe : Activity() , MainFragment.OnMainFragmentInteractionListener […]

Kotlin扩展/合成属性:多个调用中的相同对象Fragment.onViewCreated()

我正在使用kotlin-android-extensions从我的布局导入合成视图属性到我的片段。 在我的onViewCreated()片段方法中,我正在EditText视图上调用addTextChangedListener()是一个合成的导入。 这在第一次实例化Fragment的时候工作的很好,但是下一次,我的新侦听器被添加到最后一次调用的同一个视图对象中。 所以,现在TextChangedListener对象正在触发文本更改,结果可想而知。 据我所知,片段首次出现的观点从来没有被取消过。 这里是我输出的日志记录视图对象,显示它是同样的事情。 首次亮相: AmountTextWatcher (com.redacted.util.AmountTextWatcher@36d1ccc) added to amount_edit_text: android.support.v7.widget.AppCompatEditText{79e2a VFED..CL. ……I. 0,0-0,0 #7f0f013a app:id/amount_edit_text} 第二次出现: AmountTextWatcher (com.redacted.util.AmountTextWatcher@5812584) added to amount_edit_text: android.support.v7.widget.AppCompatEditText{79e2a VFED..CL. ……ID 0,0-434,200 #7f0f013a app:id/amount_edit_text} 当然,我可以通过使用findViewById()来访问我的视图,而不是合成参考,但我想知道是否有其他人正在运行这个问题,如果有更好的方法来处理它(其他比不使用综合视图参考。) FYI:使用Kotlin版本1.0.6 。