Olá a todos, da última vez que falamos sobre o exemplo do ViewPager no Android, desta vez continuaremos falando sobre o exemplo. Pare de falar sobre fofoca e volte ao trabalho. Vamos conversar com o Android juntos!
Veja os funcionários, apresentamos o uso e os cuidados do ViewPager no capítulo anterior. A seguir está o código completo. Consulte:
o código e o layout da página principal:
package com.example.talk8.blogappall;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class NaviActivity extends AppCompatActivity {
private ViewPager mViewPager;
private ArrayList<Fragment> mFragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navi);
mFragments = new ArrayList<>();
mFragments.add(new ViewpageFragmentA());
mFragments.add(new ViewpageFragmentB());
mViewPager = (ViewPager)findViewById(R.id.id_viewpage);
FragmentManager fragmentManager = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentPagerAdapter(fragmentManager){
@Override
public Fragment getItem(int i) {
if(mFragments != null)
return mFragments.get(i);
else
return null;
}
@Override
public int getCount() {
return mFragments.size();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NaviActivity">
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpage"
android:layout_width="match_parent"
android:layout_weight="5"
android:layout_height="0dp">
</android.support.v4.view.ViewPager>
</LinearLayout>
O código e o layout dos dois fragmentos:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ViewpageFragmentA extends Fragment {
public ViewpageFragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_viewpage, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewpageFragmentA">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is FragmentA of Viewpage" />
</LinearLayout>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ViewpageFragmentB extends Fragment {
public ViewpageFragmentB() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_viewpage_fragment_b, container, false);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewpageFragmentB">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:text="This is FragmentB of Viewpage" />
</LinearLayout>
Pessoal, vamos parar aqui para ver o exemplo do ViewPager no Android. Se você quiser saber quais são os outros exemplos, vamos ouvir na próxima vez!