2

私は3つのタブを持っています。これらのタブはswiperefreshlayout内にrecyclerviewを持っています。スワイプでこれらのタブを変更することはできません。これは、私のタッチイベントがtablayoutではなくswiperefreshlayoutによって受け取られているからです。私の問題を解決するには?私はその後、RecyclerView上にスワイプ作品をViewPagerを移動するが、Recyclerviewを見ることができない場合ViewPagerとTablayoutでSwipeRefreshLayoutを使用する

  1. :後

    は私のレイアウト

    <?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> 
    

    の問題です。

  2. 私はこの二つのためRelativelayoutを作成し、他の上にそれぞれ1を配置した場合、私はそれらの1、私は周りの仕事を得ることができますどのよう

にタッチイベントを行うことはできないのだろうか?

ありがとうございました!

答えて

2

レイアウトデザインを変更する必要があります。

  1. デザインあなたのActivityためTabLayoutViewPagerとレイアウト:ここでのソリューションです。
  2. FragmentごとにSwipeRefrashLayoutRecyclerViewという別のレイアウトを設計します。
  3. 最後にFragmentPagerAdapterを使用して、フラグメントの値をViewPagerに設定します。

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); 
} 

} 
関連する問題