我有一个有6页的viewPager,每页有一个PullToRefreshGridView
。
我发现我必须单击两次才能触发click事件或滚动GridView。
第一次单击未触发click事件。
这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 :(得分:0)
请在设置适配器之前尝试此操作。
viewPager.invalidate();
viewPager.requestLayout();
答案 1 :(得分:0)
我发现我添加了
main_layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
gridview
内viewPager
的每项活动。
这一个导致焦点false
。