Convert activity to fragments

2

Hello, I'm in need of a lot of help, I'm developing an application for my TCC, I've already assembled most of the features and now that I've been messing with the visual part I had problems, I wanted to use TabsLayouts, which use fragments from what I understand, I've already assembled some examples of TabsLayouts with fragments, but I can not carry the functions I created in the Activity for these Fragments, does anyone know how to do it ??? Thanks for the help right now!

Activity Example

public class morador extends AppCompatActivity {

private TextView txt_exibe_nome, txt_exibe_idade, txt_exibe_apto,txt_exibe_bloco;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_morador);
//instanciando onde vai exibir os dados
    txt_exibe_nome = (TextView) findViewById(R.id.txt_exibe_nome);
    txt_exibe_idade = (TextView) findViewById(R.id.txt_exibe_idade);
    txt_exibe_apto = (TextView) findViewById(R.id.txt_exibe_apto);
    txt_exibe_bloco = (TextView) findViewById(R.id.txt_exibe_bloco);

    if(SharedPrefManager.getInstance(this).isLoggedIn()){
        finish();
        startActivity(new Intent(this, login.class));
    }

    txt_exibe_nome.setText(SharedPrefManager.getInstance(this).exibeMoradorNome());
    txt_exibe_idade.setText(SharedPrefManager.getInstance(this).exibeMoradoridade());
    txt_exibe_apto.setText(SharedPrefManager.getInstance(this).exibeMoradorApto());
    txt_exibe_bloco.setText(SharedPrefManager.getInstance(this).exibeMoradorBloco());
  }
}

Fragment where I want to play this code

public class Fragment_perfil extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_perfil, container, false);
 }

}

FragmentAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] mTab;
public MyFragmentPagerAdapter(FragmentManager fm, String[] mTab) {
    super(fm);
    this.mTab = mTab;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new Fragment_perfil();
        case 1:
            return new Fragment_perfil();
        default:
            return null;
    }
}

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

@Override
public CharSequence getPageTitle(int position) {
    return this.mTab[position];
 }
}
    
asked by anonymous 21.04.2017 / 23:47

1 answer

1

Your Fragment should look more or less like this

public class Fragment_perfil extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_perfil, container, false);
    // Copiar código do OnCreate após o setContentView
    // Trocar os findViewById por rootView.findViewById
    // Trocar this por getActivity()
    return rootView;
 }

}

NOTE: I'm assuming you'll use the same Activity layout in Fragment.

    
22.04.2017 / 02:52