2015-06-27 26 views
2

我正在尝试用Kotlin开发Android应用程序,但在尝试动态移动片段时遇到了一些问题。我想要做的是用一个片段替换活动布局中的FrameLayout。目前,无论何时我尝试运行我的应用程序,它都会在工具栏下方显示一个白色屏幕,导致我相信该片段未按照我的预期方式添加到FrameLayout中。如何用Android上的Kotlin代码片段替换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 = findViewById(R.id.toolbar) as Toolbar 
     setSupportActionBar(toolbar) 

     getSupportFragmentManager().beginTransaction() 
      .replace(R.id.main_content_frame, MainFragment()) 
      .commit() 
    } 
} 

这里是我试图创建的片段。它只是膨胀期,同时增加在按下按钮的回调:

package net.ma.ttrobinson.kchan 

import android.os.Bundle 
import android.support.v4.app.Fragment 
import android.util.Log 
import android.view.LayoutInflater 
import android.view.View 
import android.view.ViewGroup 
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 

/** 
* Holds the main content 
*/ 
class MainFragment : Fragment() { 
    companion object { 
     val TAG = "MainFragment" 
    } 

    val debugBoard = "g" 
    val debugThread = "48667796" 

    override fun onCreateView(inflater : LayoutInflater, parent : ViewGroup?, savedInstanceState : Bundle?) : View { 
     val v = inflater.inflate(R.layout.fragment_main, parent, false) 
     val boardText = v.findViewById(R.id.board_text) as EditText 
     val threadText = v.findViewById(R.id.thread_text) as EditText 
     val request = Request(getActivity()) 

     boardText.setText(debugBoard) 
     threadText.setText(debugThread) 

     val button = v.findViewById(R.id.submit) as Button 
     button.setOnClickListener({ v -> 
      val board = boardText.getText().toString() 
      val thread = threadText.getText().toString().toInt() 

      val promise = request.getThread(board, thread) 
      promise.done({ thread -> 
       val fm = getActivity().getSupportFragmentManager() 
       fm.beginTransaction() 
        .replace(R.id.main_content_frame, ThreadFragment(thread), ThreadFragment.TAG) 
        .addToBackStack(null) 
        .commit() 
      }).fail({ result -> 
       Log.v(TAG, "Failed to get thread") 
      }) 
     }) 

     return v 
    } 
} 

下面是主要的活动与的setContentView使用的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <FrameLayout 
     android:id="@+id/main_content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

而且最后这里是该片段是布局充气:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/board_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/board_hint"/> 

    <EditText 
     android:id="@+id/thread_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:hint="@string/thread_hint"/> 

    <Button 
     android:id="@+id/submit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/submit"/> 
</LinearLayout> 

我觉得这是具有可与其它片段被换出一个的FrameLayout一个主活动的一个相当简单的设置。有谁知道我可能会做错什么?

在此期间,我想我会尝试做一个更简单的情况来尝试复制行为。

编辑:这样一个简单的解决方案。我忘了在我的LinearLayout中添加android:orientation="vertical"。下面是更新后的代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <FrameLayout 
     android:id="@+id/main_content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 
+1

不知道这是唯一的问题有,但'LinearLayout'在活动视图应该有'方向=“垂直” – rciovati

+0

哇,我不能相信它;修复它。我猜想当我从非片段布局切换到片段布局时,方向会丢失。 XY问题的经典案例。非常感谢。如果您将此作为答案提交,我可以接受。 – mrobinson7627

+0

在异步操作结束时,您不应执行FragmentTransaction:如果事务尝试在onStop()之后执行,您将得到IllegalStateException。 – BladeCoder

回答

2

的问题是,该活动的LinearLayout应该有orientation="vertical"

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <FrameLayout 
     android:id="@+id/main_content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout>