一个活动中的三个片段

时间:2016-11-05 23:35:01

标签: android android-fragments fragment

Android的新手,我正在尝试创建一个显示名称列表的应用程序,然后右侧显示右侧的主题标题,然后是第三个显示该主题标题详细信息的片段。我希望所有三个片段能够显示在我的主要活动上。所以我希望它看起来像这样:

Paint description

到目前为止,这是主要的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/companyListFrag_LL"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment
        class= "name.financialnews.CompaniesListFragment"
        android:id="@+id/list_companies"
        android:layout_width="10dp"
        android:layout_weight="20"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/list_headings"
        android:layout_width="20dp"
        android:layout_weight="80"
        android:layout_marginLeft="20dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/detail_fragment"
        android:layout_width="0dp"
        android:layout_below="@id/list_headings"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    </RelativeLayout>

当我运行它时,我能够点击名称以获得右侧的主题标题,当我单击主题标题时,没有任何内容显示细节片段。任何人都可以帮我弄清楚我做错了什么?

谢谢

这里是细节片段xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
 >

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_marginTop="30dp"
    android:id="@+id/textHeading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium" />

/>


<TextView
    android:id="@+id/textDescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="" />

</FrameLayout>

添加了主要活动java:

import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;

public class MainActivity extends AppCompatActivity
    implements CompaniesListFragment.OnCompaniesSelectedListener,
    HeadingsListFragment.OnHeadingsSelectedListener

{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void onCompanySelected(long id){


    HeadingsListFragment headingsList = new HeadingsListFragment();

    FragmentTransaction transaction= 
getFragmentManager().beginTransaction();
    headingsList.updateCompanyId(id);

    transaction.replace(R.id.list_headings,headingsList);

    transaction.addToBackStack(null);

    transaction.setTransition(FragmentTransaction.TRANSIT_NONE);

    transaction.commit();


}


@Override
public void onHeadingSelected(long id) {


    Detail_Fragment items = new Detail_Fragment();
    FragmentTransaction transaction=            
getFragmentManager().beginTransaction();
    items.updateHeadingId(id);


    transaction.replace(R.id.detail_fragment, items);

    transaction.addToBackStack(null);

    transaction.setTransition(FragmentTransaction.TRANSIT_NONE);

    transaction.commit();

}
}

1 个答案:

答案 0 :(得分:0)

将根元素从RelativeLayout更改为LinearLayout,并将其方向更改为vertical。我还会在内部LinearLayout上加权,以便根元素的两个直接子元素具有相等的权重。你给公司名单和标题的空间也很小(20dp不是很大)。

当我将您的代码粘贴到Android Studio中时,会有警告说明您的布局存在一些问题,这些问题会指向正确的方向。总的来说,我建议一次开始简单并添加一个片段。可以帮助的一件事是打开设置&#34;显示布局边界&#34;如果您在视图布局方面遇到问题,请在开发人员选项中使用。