Skip to content

Instantly share code, notes, and snippets.

@typosone
Created August 5, 2014 06:40
Show Gist options
  • Save typosone/c450f01eb47aa22d1ce1 to your computer and use it in GitHub Desktop.
Save typosone/c450f01eb47aa22d1ce1 to your computer and use it in GitHub Desktop.
package jp.ac.it_college.std.nakasone.actionbartab;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
static final String[] TAGS = {"tab1", "tab2", "tab3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label1 = getString(R.string.tab1_label);
String label2 = getString(R.string.tab2_label);
String label3 = getString(R.string.tab3_label);
bar.addTab(bar.newTab().setText(label1)
.setTabListener(new MyTabListener(
TabContentFragment.newInstance(R.layout.bar_tabs_content1))));
bar.addTab(bar.newTab().setText(label2)
.setTabListener(new MyTabListener(
TabContentFragment.newInstance(R.layout.bar_tabs_content2))));
bar.addTab(bar.newTab().setText(label3)
.setTabListener(new MyTabListener(
TabContentFragment.newInstance(R.layout.bar_tabs_content3))));
}
public void onBtnLog(View v) {
TabContentFragment tf1 = (TabContentFragment) getFragmentManager()
.findFragmentByTag(TAGS[0]);
if (tf1 != null) {
TextView tv1 = (TextView) findViewById(R.id.textView1);
EditText edt1 = (EditText) findViewById(R.id.editText1);
Log.d("ActionBarTab", tv1.getText().toString());
Log.d("ActionBarTab", edt1.getText().toString());
}
TabContentFragment tf2 = (TabContentFragment) getFragmentManager()
.findFragmentByTag(TAGS[1]);
if (tf2 != null) {
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
Log.d("ActionBarTab", tv2.getText().toString());
Log.d("ActionBarTab", tv3.getText().toString());
}
TabContentFragment tf3 = (TabContentFragment) getFragmentManager()
.findFragmentByTag(TAGS[2]);
if (tf3 != null) {
TextView tv4 = (TextView) findViewById(R.id.textView4);
EditText edt2 = (EditText) findViewById(R.id.editText2);
TextView tv5 = (TextView) findViewById(R.id.textView5);
EditText edt3 = (EditText) findViewById(R.id.editText3);
Log.d("ActionBarTab", tv4.getText().toString());
Log.d("ActionBarTab", edt2.getText().toString());
Log.d("ActionBarTab", tv5.getText().toString());
Log.d("ActionBarTab", edt3.getText().toString());
}
}
public static class TabContentFragment extends Fragment {
private int mLayoutId;
public static TabContentFragment newInstance(int layoutId) {
TabContentFragment tf = new TabContentFragment();
Bundle args = new Bundle();
args.putInt("layoutId", layoutId);
tf.setArguments(args);
return tf;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mLayoutId = getArguments().getInt("layoutId");
View fragView = inflater.inflate(mLayoutId, container, false);
return fragView;
}
}
public class MyTabListener implements ActionBar.TabListener {
private TabContentFragment mFragment;
public MyTabListener(TabContentFragment fragment) {
mFragment = fragment;
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (!mFragment.isAdded()) {
fragmentTransaction.add(R.id.fragment_content, mFragment,
TAGS[tab.getPosition()]);
} else {
fragmentTransaction.show(mFragment);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
fragmentTransaction.hide(mFragment);
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment