Skip to content

Instantly share code, notes, and snippets.

@thbighead
Created November 7, 2018 22:51
Show Gist options
  • Save thbighead/f9f7c06ef7e0e65963db0e29596e5ea9 to your computer and use it in GitHub Desktop.
Save thbighead/f9f7c06ef7e0e65963db0e29596e5ea9 to your computer and use it in GitHub Desktop.
Primeiro projeto Android Native com Fragments (parece não estar funcionando, não troca para o segundo fragmento)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.andersonvieira.exemplo_fragments.MainActivity">
<Button
android:text="@string/fragmento_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="33dp"
android:id="@+id/btnFragmento1" />
<Button
android:text="@string/fragmento_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="17dp"
android:id="@+id/btnFragmento2"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/btnFragmento1" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="17dp"
android:layout_below="@+id/btnFragmento1"
android:layout_centerHorizontal="true"
android:id="@+id/frame_container">
</FrameLayout>
</RelativeLayout>
<RelativeLayout 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"
tools:context="com.example.andersonvieira.exemplo_fragments.NovoFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView"
android:layout_width="256dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="67dp"
android:layout_marginTop="81dp"
android:text="@string/este_o_primeiro_fragmento"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textSize="18sp" />
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginStart="78dp"
android:layout_marginTop="68dp"
android:text="@string/ok"
tools:text="OK" />
</RelativeLayout>
<RelativeLayout 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"
tools:context="com.example.andersonvieira.exemplo_fragments.SegundoFragment"
android:background="@android:color/holo_green_light">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView2"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="58dp"
android:layout_marginTop="91dp"
android:text="@string/estou_no_segundo_fragmento"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textSize="18sp" />
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginStart="88dp"
android:layout_marginTop="40dp"
android:text="@string/button"
tools:text="OK" />
</RelativeLayout>
package com.example.andersonvieira.exemplo_fragments;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_container, new NovoFragment())
.commit();
}
Button f1 = (Button) findViewById(R.id.btnFragmento1);
Button f2 = (Button) findViewById(R.id.btnFragmento2);
f1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, new NovoFragment())
.commit();
}
});
f2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_container, new SegundoFragment())
.commit();
}
});
}
}
package com.example.andersonvieira.exemplo_fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class NovoFragment extends Fragment {
public NovoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_novo, container, false);
Button b = (Button) view.findViewById(R.id.Button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity().getApplicationContext(),
"Estou no PRIMEIRO fragmento!!!",
Toast.LENGTH_LONG).show();
}
});
return view;
}
}
package com.example.andersonvieira.exemplo_fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class SegundoFragment extends Fragment {
public SegundoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_novo, container, false);
Button b = (Button) view.findViewById(R.id.Button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity().getApplicationContext(),
"Estou no SEGUNDO fragmento!!!",
Toast.LENGTH_LONG).show();
}
});
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment