在浏览量滑动上添加新片段

Posted

技术标签:

【中文标题】在浏览量滑动上添加新片段【英文标题】:add new fragment on pageview swipe 【发布时间】:2017-12-05 17:51:55 【问题描述】:

每次我在最后一个片段上向右滑动或在第一个片段上向左滑动时,我都想动态添加一个新片段。此片段将添加到我的适配器中,该适配器将设置为我的 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);
        
    

【问题讨论】:

【参考方案1】:

只需扩展 ViewPager 并覆盖 onInterceptTouchEvent() 就像这里显示的 https://***.com/a/13347008/2000083

【讨论】:

我实现了它并且我使用 CustomViewPager myViewPager;。我的疑问是,当我使用 myViewPager.setOnSwipeOutListener(???);

以上是关于在浏览量滑动上添加新片段的主要内容,如果未能解决你的问题,请参考以下文章

如何让电脑的浏览器自动往下滑动?

浏览器中的语音识别功能

在移动端浏览器上怎么实现当页面向上和向下滑动时隐藏

如何在活动和浏览器片段之间传递值? [复制]

360浏览器关掉网页就出来新标签页

android tv - 浏览片段中搜索图像的参考