我希望每次在最后一个片段上向右或者在第一个片段上左右动态地添加一个新片段。此片段将添加到我的适配器中,该适配器将设置为我的viewPager。现在我在setupViewPager中定义了一个静态的片段数,它们在OnCreate方法中“加载”。这是我的代码:
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);
}
}
}
答案 0 :(得分:1)
只需扩展ViewPager并覆盖onInterceptTouchEvent(),如此处所示https://stackoverflow.com/a/13347008/2000083