I want to add a new fragment everytime I swype right on the last fragment or left on the first one, dynamically. This fragment will be add to my adapater that will be set to my viewPager. Right now I have a static number of fragments defined in the setupViewPager and they "load" in the OnCreate method. Here is my code:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.support.android.designlibdemo.Diafragment;
import com.support.android.designlibdemo.R;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
Intent intent = getIntent();
String nome = intent.getStringExtra("nome");
View headerView = navigationView.getHeaderView(0);
TextView navUsername = headerView.findViewById(R.id.tv_drawerUsername);
navUsername.setText(nome);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
SimpleDateFormat sdf = new SimpleDateFormat("EEE");
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
date = calendar.getTime();
String day = sdf.format(date);
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new Diafragment(), day);
calendar.add(Calendar.DATE, 1);
date = calendar.getTime();
String dayplusone = sdf.format(date);
adapter.addFragment(new Diafragment(), dayplusone);
calendar.add(Calendar.DATE, 1);
date = calendar.getTime();
String dayplustwo = sdf.format(date);
adapter.addFragment(new Diafragment(), dayplustwo);
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
Just extend ViewPager and override onInterceptTouchEvent() like shown in here https://stackoverflow.com/a/13347008/2000083