将SwipeRefreshLayout与Viewpager和Tablayout

时间:2017-03-26 14:04:23

标签: android android-viewpager android-tablayout swiperefreshlayout

我有3个标签,这些标签在swiperefreshlayout中有recyclerview。我无法通过滑动更改这些标签。我想这是因为,我的触摸事件是通过swiperefreshlayout而不是tablayout收到的。如何解决我的问题?

以下是我的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout 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/news_swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.nabinkhadka.hamrodang.activity.NewsActivity"
    tools:showIn="@layout/app_bar_main">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/news_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.v4.widget.SwipeRefreshLayout>

问题:

  
      
  1. 如果我将ViewPager移到RecyclerView上方,则滑动有效,但无法看到Recyclerview。
  2.   
  3. 如果我为这两个人创建一个Relativelayout并将每个人放在另一个上面,我就不能在其中一个上做触摸事件
  4.   

我怎样才能找到解决方法?

谢谢!

1 个答案:

答案 0 :(得分:2)

您应该更改布局设计。这是解决方案:

  1. TabLayout设计ViewPagerActivity的布局。
  2. 为每个TAB SwipeRefrashLayout设计另一个包含RecyclerViewFragment的布局。
  3. 最后,使用FragmentPagerAdapter填充ViewPager上的片段。
  4. <强> activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs_my_offers"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            style="@style/MyCustomTabLayout"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager_my_offers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    <强> fragment_tab.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"/>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>
    

    <强> TabsPagerAdapter.java

    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TabsPagerAdapter extends FragmentPagerAdapter {
    
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    
    public TabsPagerAdapter(FragmentManager manager) {
        super(manager);
    }
    
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }
    
    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
    }
    

    <强> MainActivity.java

    import android.content.Context;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.design.widget.TabLayout;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.Window;
    import android.view.WindowManager;
    
    import company.screenshot.fundle.R;
    import company.screenshot.fundle.points.adapter.TabsPagerAdapter;
    
    
    public class MainActivity extends AppCompatActivity {
    
    // TAG
    private static final String TAG = MainActivity.class.getSimpleName();
    
    Context mContext;
    
    ViewPager mViewPager;
    TabLayout mTabLayout;
    
    TabsPagerAdapter mTabsPagerAdapter;
    
    // ToolBar
    Toolbar mToolBar;
    
    // Actionbar
    ActionBar mActionBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Context
        mContext = this;
    
        // ToolBar
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
    
        if(mToolBar != null)
        {
            setSupportActionBar(mToolBar);
    
            mActionBar = getSupportActionBar();
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setTitle("History");
        }
    
        // Status bar :: Transparent
        Window window = this.getWindow();
    
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
        }
    
        // Tabs
        mTabLayout = (TabLayout) findViewById(R.id.tabs_my_offers);
        mViewPager  = (ViewPager) findViewById(R.id.view_pager_my_offers);
    
        mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        mTabsPagerAdapter.addFrag(new FragmentEarnedHistories(), "EARNED");
        mTabsPagerAdapter.addFrag(new FragmentRedeemedHistories(), "REDEEMED");
    
    
        mViewPager.setAdapter(mTabsPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
    
                mViewPager.setCurrentItem(tab.getPosition());
            }
    
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
    
            }
    
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
    
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    }