2016-03-08 74 views
1

我有一個viewPager,有6頁,每頁有PullToRefreshGridView。 我發現我必須點擊兩次才能觸發點擊事件或滾動GridView。 第一次點擊沒有觸發點擊事件。 這6頁有相同的情況。我必須點擊兩次才能使其工作,滾動或點擊事件

請有人幫我調試,並告訴我我在這裏做錯了什麼。

這是我的代碼:

viewPager = (ViewPager)findViewById(R.id.viewPager); 
    adapterViewPager = new MyPagerAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(adapterViewPager); 
    viewPager.setCurrentItem(0); 
    viewPager.addOnPageChangeListener(this); 
    viewPager.setOffscreenPageLimit(0); 

和MyPagerAdapter是:

public static class MyPagerAdapter extends FragmentStatePagerAdapter { 
    private static int NUM_ITEMS = 6; 

     public MyPagerAdapter(FragmentManager fragmentManager) { 
      super(fragmentManager); 
     } 

     @Override 
     public void destroyItem(View collection, int position, Object o) { 
      Log.d("DESTROY", "destroying view at position " + position); 
      View view = (View) o; 
      ((ViewPager) collection).removeView(view); 
      view = null; 
     } 

     // Returns total number of pages 
     @Override 
     public int getCount() { 
      return NUM_ITEMS; 
     } 

     // Returns the fragment to display for that page 
     @Override 
     public Fragment getItem(int position) { 

      switch (position) 
      { 
      case 0: 
       return TypeZero.newInstance("", "Page # 1"); 
      case 1: 
       return TypeOne.newInstance("", "Page # 2"); 
      case 2: 
       return TypeTwo.newInstance("", "Page # 3"); 
      case 3: 
       return TypeThree.newInstance("", "Page # 4"); 
      case 4: 
       return TypeFour.newInstance("", "Page # 5"); 
      case 5: 
       return TypeFive.newInstance("", "Page # 6"); 
       default: 
        return null; 
      } 
     } 

     // Returns the page title for the top indicator 
     @Override 
     public CharSequence getPageTitle(int position) { 
      switch(position) 
      { 
      case 0: 
       return "Zero"; 
      case 1: 
       return "One"; 
      case 2: 
       return "Two"; 
      case 3: 
       return "Three"; 
      case 4: 
       return "Four"; 
      case 5: 
       return "Five"; 
      default: 
       return null; 
      } 
     } 

這裏是我的佈局activity_main.xml中

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:background="#556fa5" 
    android:minHeight="?attr/actionBarSize"> 
    </android.support.v7.widget.Toolbar> 


<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/toolbar"> 


    <RelativeLayout 
     android:id="@+id/main_content" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:tabIndicatorColor="#f44336" 
      app:tabSelectedTextColor="#FFFFFF" 
      app:tabTextColor="#03A9F4" /> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewPager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@id/tabLayout" /> 

    </RelativeLayout> 

    <!-- Listview to display slider menu --> 

    <ListView 
     android:id="@+id/list_slidermenu" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@color/list_divider" 
     android:dividerHeight="1dp"   
     android:listSelector="@drawable/list_selector" 
     android:background="@color/list_background"/> 
</android.support.v4.widget.DrawerLayout> 

PullToRefreshGridView佈局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.handmark.pulltorefresh.MainActivity" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <!-- The PullToRefreshGridView replaces a standard GridView widget. --> 

    <com.handmark.pulltorefresh.PullToRefreshGridView 
     xmlns:ptr="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/pull_refresh_grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingStart="5dp" 
     android:paddingEnd="5dp" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:columnWidth="0dp" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:horizontalSpacing="10dp" 
     android:verticalSpacing="10dp" 
     android:numColumns="2" 
     android:stretchMode="columnWidth" 
     ptr:ptrDrawable="@drawable/ic_launcher" 
     ptr:ptrMode="both" /> 
</LinearLayout> 

+0

你可以發佈佈局文件嗎? –

+1

您是否將可聚焦性設置爲任何視圖或父佈局的false? –

+0

問題可能是因爲您在佈局上的任何視圖上請求requestfocus。請檢查佈局文件中的相同內容。 –

回答

0

請設置適配器之前嘗試。

viewPager.invalidate(); 
viewPager.requestLayout(); 
+0

感謝您的回覆,但它不是炒作。 –

0

我發現我加

main_layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 

viewPager內的gridview每一項活動。 這一個導致焦點false

相關問題