понедельник, 17 сентября 2012 г.

Navigation tabs с Fragments использую ActionBarSherlock

This post covers how to use navigation tabs in the ActionBar using ActionBarSherlock. Although the demo in the ActionBarSherlock download shows how to create navigation tabs, they are not associated with Fragments. I looked around online and could not find any tutorials for it. Now that I’ve figured it out, I thought I’d share it.

This tutorial has two Fragments, FragmentA and FragmentB, each of which has a TextView and a Button in their layouts.

Here’s FragmentA.java: It implements an OnClickListener for the Button to show a Toast.

public class FragmentA extends Fragment {
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
return inflater.inflate(R.layout.frag_a, group, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You clicked button on Fragment A", Toast.LENGTH_LONG).show();
}
});
}
}

Here’s FragmentB.java: It also implements the Button OnClickListener, but to display a dialog.


public class FragmentB extends Fragment {
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
return inflater.inflate(R.layout.frag_b, group, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
button = (Button) getActivity().findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Fragment B");
builder.setMessage("What would you like to do?");
builder.setPositiveButton("Nothing", null);
builder.setNegativeButton("Leave me alone!", null);
builder.show();
}
});
}
}

And finally, here’s the Activity that hosts these Fragments, FragmentDemoActivity


public class FragmentDemoActivity extends SherlockFragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = bar.newTab();
ActionBar.Tab tab2 = bar.newTab();
tab1.setText("Fragment A");
tab2.setText("Fragment B");
tab1.setTabListener(new MyTabListener());
tab2.setTabListener(new MyTabListener());
bar.addTab(tab1);
bar.addTab(tab2);
}
private class MyTabListener implements ActionBar.TabListener
{
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
FragmentA frag = new FragmentA();
ft.replace(android.R.id.content, frag);
}
else
{
FragmentB frag = new FragmentB();
ft.replace(android.R.id.content, frag);
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}

Комментариев нет:

Отправить комментарий