如何在onclick事件中切换片段?

13

我有一个项目需要完成。我遇到了一个小问题,不确定如何解决。下面是应用程序的图像。

我希望的是,当用户点击列表项中的某一项时,“Hello!It's Fragment2” 部分会更改为我在应用程序中声明的新的 xml。因此,如果我点击 ATO 列表项,则右侧的片段应更改为类似“Hello!It's ATO Fragment”的内容。

enter image description here

以下是我的代码:

AndroidListFragmentActivity:

package com.exercise.AndroidListFragment;

import android.app.Activity;
import android.os.Bundle;

public class AndroidListFragmentActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

片段2:

package com.exercise.AndroidListFragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment2, container, false);
    }

}  

我的列表碎片1:

package com.exercise.AndroidListFragment;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListFragment1 extends ListFragment {

    String[] options ={
            "ATO", 
            "BETA", 
            "DELT", 
            "PHI DELT",
            "SAE", 
            "SIG NU", 
            "FIJI", 
            "SIG CHI",
            "PHI PSI" 
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListAdapter myListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, options);
        setListAdapter(myListAdapter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.listfragment1, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }
}

Fragment2.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/fragment2text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello! It's Fragment2" />
</LinearLayout>

listfragment1.xml

<?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"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No data"/>
 </LinearLayout>

主界面.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:name="com.exercise.AndroidListFragment.MyListFragment1"
      android:id="@+id/fragment1"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="match_parent" />
  <fragment
      android:name="com.exercise.AndroidListFragment.Fragment2"
      android:id="@+id/fragment2"
      android:layout_weight="2"
      android:layout_width="0px"
      android:layout_height="match_parent" />

</LinearLayout>
2个回答

20

不确定修复您的代码所需的最小更改是什么,但是否考虑使用导航抽屉在片段之间进行切换?在我的看法中,官方文档中的示例看起来几乎完全符合您想要实现的目标。

关键在于拥有某种容器用于当前显示的片段(而不是像在您的 XML 中使用<fragment>)。例如:

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

然后,切换片段大概是这样的:

Fragment fragment = new Fragment2();
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.content_frame, fragment)
               .commit();

更多阅读:Android开发者文档中的将Fragment在运行时添加到Activity


@Jonik,你能帮我吗?这是关于片段和活动之间的事务。 - Noufal

1
private void openFragment (int number) {

        Fragment fragment = new Fragment();

        switch (number) {
            case FRAGMENT_1:
                fragment = new *Fragment1*();
                break;

            case FRAGMENT_2:
                fragment = new *Fragment2*();
                break;
        }

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_frame, fragment)
                .commit();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接