我有一个名为 FragmentActivity 的 Activity 类。在 Activity 类中,我创建了三个名称为Button1、Button2 和 Button3的按钮。我有三个片段类,分别命名为Fragment1、Fragment2 和 Fragment3。
现在单击每个按钮,我在该 Activity 上设置一个特定的片段类。我想使用滑动来做同样的事情。我的意思是说在滑动时打开每个片段类。到目前为止我使用的代码如下所示。
Fragment_Activity.xml
<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="vertical" >
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
</LinearLayout>
<fragment
android:id="@+id/fragment_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="in.icebreaker.fragments.Fragment1" />
<fragment
android:id="@+id/fragment_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="in.icebreaker.fragments.Fragment2" />
<fragment
android:id="@+id/fragment_three"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="in.icebreaker.fragments.Fragment3" />
</LinearLayout>
FragmentActivity 类的代码
public class FragmentActivity extends Activity
{
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private static final int FRAGMENT_INDEX1 = 1001;
private static final int FRAGMENT_INDEX2 = 1001;
private static final int FRAGMENT_INDEX3 = 1001;
private int m_CurrentFragment = FRAGMENT_INDEX1;
private FragmentTransaction m_FragmentTransaction;
private Button button1, button2, button3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button)findViewById(R.id.button2);
button3.setOnClickListener(this);
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this);
// Create an instance of fragment class
fragment1 = (VisitorListFragment)getFragmentManager().findFragmentById(
R.id.fragment_one);
fragment2 = (ChatListFragment)getFragmentManager().findFragmentById(
R.id.fragment_two);
fragment3 = (ChatListFragment)getFragmentManager().findFragmentById(
R.id.fragment_three);
// Set current fragment on the activity
setFragment(m_CurrentFragment);
}
private void setFragment(int currentFragment)
{
m_CurrentFragment = currentFragment;
m_FragmentTransaction = getFragmentManager().beginTransaction();
if (m_CurrentFragment == FRAGMENT_INDEX1)
{
m_FragmentTransaction.show(fragment1);
m_FragmentTransaction.hide(fragment2);
m_FragmentTransaction.hide(fragment3);
}
else if (m_CurrentFragment == FRAGMENT_INDEX2)
{
m_FragmentTransaction.show(fragment2);
m_FragmentTransaction.hide(fragment1);
m_FragmentTransaction.hide(fragment3);
}
else if (m_CurrentFragment == FRAGMENT_INDEX3)
{
m_FragmentTransaction.show(fragment3);
m_FragmentTransaction.hide(fragment1);
m_FragmentTransaction.hide(fragment2);
}
m_FragmentTransaction.commit();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
{
setFragment(fragment1);
break;
}
case R.id.button2:
{
setFragment(fragment2);
break;
}
case R.id.button3:
{
setFragment(fragment3);
break;
}
}
}
}