来自相同活动中同一类的两个片段不起作用

时间:2014-05-15 17:56:02

标签: android android-layout android-fragments android-activity

我正在测试专门用于平板电脑的布局,我希望放置两个列表视图,一个在右侧,一个在左侧,&在中间,细节将显示在选择上。

右侧和左侧的片段将包含列表视图。两者都完全相同而没有任何区别,所以当我将android:name设置为相似时,只有一个显示,只有正确,任何建议?

<LinearLayout 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="horizontal"
    tools:context="com.example.MainActivity">

    <fragment
        android:id="@+id/photo_list1"
        android:name="com.example.fragments.PhotoListFragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/photo_details_fragment"
        android:name="com.example.fragments.PhotoDetailsFragment"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent"/>
    <fragment
        android:id="@+id/photo_list2"
        android:name="com.example.fragments.PhotoListFragment"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

我正在为您提供您的要求的演示

DetailFrag.java 请尽可能使用支持库来降低api

public class DetailFrag extends Fragment{
@Override


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override


public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}


@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}


public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.captain);


view.setText(item);


}


}

ListFrag.java

public class ListFrag extends ListFragment{
@Override


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override


public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Enterprise", "Star Trek", "Next Generation", "Deep Space 9", "Voyager"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);


}


@Override


public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);


DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_capt);


if (frag != null && frag.isInLayout()) {
frag.setText(getCapt(item));


}


}


private String getCapt(String ship) {
if (ship.toLowerCase().contains("enterprise")) {
return "Johnathan Archer";
}


if (ship.toLowerCase().contains("star trek")) {
return "James T. Kirk";
}


if (ship.toLowerCase().contains("next generation")) {
return "Jean-Luc Picard";
}


if (ship.toLowerCase().contains("deep space 9")) {
return "Benjamin Sisko";
}


if (ship.toLowerCase().contains("voyager")) {
return "Kathryn Janeway";
}


return "???";
}


}

MainActivity.java

public class MainActivity extends FragmentActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/frag_series"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.fragmentdemo.ListFrag" />
<fragment
android:id="@+id/frag_capt"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
class="com.example.fragmentdemo.DetailFrag" />
<fragment
android:id="@+id/frag_series1"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.fragmentdemo.ListFrag" />
</LinearLayout>

<强> detail_fragment.xml

<?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" >
<TextView
 android:id="@+id/captain"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="center_horizontal|center_vertical"
 android:layout_marginTop="20dip"
 android:text="Johnathan Archer"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:textSize="30dip" />
</LinearLayout>

将输出cbove代码 enter image description here 希望它会对你有所帮助。