我有一个名为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;
}
}
}
}
答案 0 :(得分:0)
您可以使用ActionTabs + ViewPager:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html