如何替换片段运行时

时间:2013-02-14 10:53:15

标签: java android android-layout android-fragments android-widget

我想在Grid和List项目的项目点击上添加和替换帧布局与fragment-1和fragment-2上的片段。

我创建了main.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="horizontal" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/fragment1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.example.fragment.Fragment1" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <fragment
        android:id="@+id/fragment2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.example.fragment.Fragment2" />
</LinearLayout>

<View
    android:layout_width="1dp"
    android:layout_height="fill_parent"
    android:background="#000000" />

<FrameLayout
    android:id="@+id/fragment3"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="2"/>

这是我的fragment_1.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"
android:paddingLeft="8dp"
android:paddingRight="8dp" >

<GridView
    android:id="@+id/Grid1"
    android:layout_width="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:drawSelectorOnTop="false" />

这是我的row_fragment1_list.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:layout_margin="15dp"
android:orientation="vertical" >

<ImageView
    android:id="@+id/img_view"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#CCCCCC"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp" />

和fragment_3.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:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_fragment3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:gravity="center"
    android:text="This is 3rd Fragment" />

这是我的Main.xml

public class Main extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

这里是Fragment1.class

public class Fragment1 extends Fragment implements OnItemClickListener
{
    Activity myActivity;
    GridView mGridView; 
    private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7"};
    private int imgID[] = {R.drawable.admin_access_rule,
            R.drawable.admin_backup,R.drawable.admin_browsesite,
            R.drawable.admin_comment_post,R.drawable.admin_content,
            R.drawable.admin_content_type,R.drawable.admin_logout,};

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
        this.myActivity = activity;
        Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();

        View view = inflater.inflate(R.layout.fragment_1,container, false);
        mGridView = (GridView)view.findViewById(R.id.Grid1);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() 
    {
        super.onStart();
        mGridView.setAdapter(new GridBaseAdapter(myActivity));
        mGridView.setOnItemClickListener(this);
    }

    private class GridBaseAdapter extends BaseAdapter 
    {
        LayoutInflater mLayoutInflater = null;
        public GridBaseAdapter(Context mContext) 
        {
            mContext = myActivity;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() 
    {
        return ListItem.length;
    }

    @Override
    public Object getItem(int position) 
    {
        return null;
    }

    @Override
    public long getItemId(int position) 
    {
        return ListItem.length;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {

        if(convertView == null) 
        {
            convertView = mLayoutInflater.inflate(R.layout.row_fragment_list, null);
        }

        ImageView mImageView = (ImageView) convertView.findViewById(R.id.img_view);
        mImageView.setImageResource(imgID[position]);

        TextView tvUserEmail = (TextView) convertView.findViewById(R.id.text2);
        tvUserEmail.setText("Sub " +ListItem[position]);            

        return convertView;
    }
}

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) 
{
    TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3);
    txt3.setText("1st Fragment's : " + position +" Item Clicked");
}
}

My Fragment2.class

public class Fragment2 extends ListFragment
{
    Activity myActivity;
    private String ListItem[] = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 6","Item 7"};

@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    this.myActivity = activity;
    Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();

     /** Creating an array adapter to store the list of countries **/
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,ListItem);
    /** Setting the list adapter for the ListFragment */
    setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) 
{
    Toast.makeText(myActivity.getApplicationContext(), position +" Item Clicked", Toast.LENGTH_SHORT).show();
    TextView txt3 = (TextView)myActivity.findViewById(R.id.tv_fragment3);
    txt3.setText("2nd Fragment's : " + position +" Item Clicked");
    super.onListItemClick(l, v, position, id);
}
}

最后我的Fragment3.class

public class Fragment3 extends Fragment
{
Activity myActivity;
TextView txt_view;  
@Override
public void onAttach(Activity activity) 
{
    super.onAttach(activity);
    this.myActivity = activity;
    Toast.makeText(activity.getApplicationContext(), "On Attach", Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    Toast.makeText(myActivity.getApplicationContext(), "On Create", Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Create View", Toast.LENGTH_SHORT).show();
    View view = inflater.inflate(R.layout.fragment_3,container, false);
    txt_view = (TextView)view.findViewById(R.id.tv_fragment3);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) 
{
    Toast.makeText(myActivity.getApplicationContext(), "On Activity Created", Toast.LENGTH_SHORT).show();
    super.onActivityCreated(savedInstanceState);
}
} 

1 个答案:

答案 0 :(得分:4)

使片段暴露您的活动可以附加到的接口。事件应在需要时发生(例如,单击列表项)。然后,活动应该将片段的实例放在应该的位置,例如:

// fragment is an instance of the fragment you want to show
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment3, fragment);
transaction.commit();