簡體   English   中英

片段在應用程序中不起作用

[英]Fragments don't work in app

我有問題,因為我不知道出什么問題了。 我不顯示activity_main_fragment.xml的活動,我也不知道為什么。 您有什么建議要更改嗎? 我創建了兩個fragment_a和fragment_b片段,當您在列表視圖中單擊某個項目時,我想顯示它們與fragment_a連接的信息。

Communicator.java

public interface Communicator {
    public void respond(int i);

}

strings.xml中

 <string-array name="titles">
        <item>Zmiany</item>
        <item>Ilę</item>
        <item>Trudne pytania</item>
    </string-array>

    <string-array name="descriptions">
        <item>Zmianysadas</item>
        <item>Ilęvcxcvcx</item>
        <item>Trudne pytaxcvcxvxcnia</item>
    </string-array>

fragment_a.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="vertical"
    android:background="#FFBB00" >

    <ListView
        android:id="@+id/listView22"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

fragment_b.xml

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

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

</LinearLayout>

activity_main_fragment.xml

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


    <fragment
        android:id="@+id/fragment1"
        android:name="com.odpad.odpadygdansk.FragmentA"
        android:layout_width="150dp"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.odpad.odpadygdansk.FragmentB"
        android:layout_width="470dp"
        android:layout_height="match_parent" />

</LinearLayout>

FragmentA.java

public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{

    ListView list;
    Communicator communicator;
    @Override
    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        communicator = (Communicator) getActivity();
        list = (ListView) getActivity().findViewById(R.id.listView22);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.titles, android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
        return inflater.inflate(
                R.layout.fragment_a, container, false);     
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
        communicator.respond(i);
    }

}

FragmentB.java

public class FragmentB extends Fragment{
    TextView text;

    @Override
    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        text = (TextView) getActivity().findViewById(R.id.textView);
        return inflater.inflate(
                R.layout.fragment_b, container, false);
    }

    public void changeData(int i)
    {
        Resources res = getResources();
        String[] descriptions = res.getStringArray(R.array.descriptions);
        text.setText(descriptions[i]);
    }
}

EcologicalInformationActivity.java

public class EcologicalInformationActivity extends FragmentActivity implements Communicator{ 
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_fragment);
    }

    @Override
    public void respond(int i){
        FragmentManager manager = getFragmentManager();
        FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
        f2.changeData(i);
    }
}

EcologicalInformationActivity擴展了FragmentActivity因此您應該調用getSupportFragmentManager()代替getFragmentManager() 請參閱此處的文檔。 如果您的片段還不是支持庫版本的實例, 也需要更改它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM