PagerSlidingTabStrip ViewPager不滚动

时间:2016-03-18 21:21:49

标签: android listview android-viewpager fragment pagerslidingtabstrip

我有一个带有viewpager的PagerSlidingTabStrip,它包含三个片段。其中一个片段有ListView,当我在ListView项目上向左或向右滑动时,viewpager不会更改页面,当我在ListView滚动外滑动工作正常。当我在仅包含TextView的其他片段上滑动时,滑动正在工作。请建议,我可能做错了什么?

只需在PagerSlidingTabStrip的示例代码中添加以下片段和适配器即可重现此问题。

@SuppressLint("ValidFragment")
public class HomeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = null;
        try {
            view = inflater.inflate(R.layout.home, container, false);
            ListView listview = (ListView) view.findViewById(R.id.homegrid);

            listview.setAdapter(new HomeAdapter(inflater));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return view;
    }
}

public class HomeAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;

    public HomeAdapter( LayoutInflater inflater)
    {
        this.inflater = inflater;
    }

    public int getCount() {
        return 5;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            convertView = inflater.inflate(R.layout.home_grid_item, parent, false);
        }
       TextView text1 =  (TextView)convertView.findViewById(R.id.title);
        text1.setText("FAVOURITE "+position);

        return convertView;
    }


}

home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical"
    android:background="#ffe0e0e0"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:clipToPadding="false"
    >

    <ListView
        android:id="@+id/homegrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:layout_gravity="center_horizontal|center_vertical"
        >
    </ListView>

</LinearLayout>

home_grid_item.xml

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal|center_vertical"
        android:background="#000000"
        >
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|center_horizontal"
        android:paddingRight="0dp"
        android:paddingLeft="0dp"
        android:text="FAVOUR"
        android:textSize="9pt"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:textColor="#FFFFFF"
        android:singleLine="true"
        android:maxLines="1" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

包含带有android:singleLine =“true”的TextView的ViewPager导致了这个问题。 这个问题已在下面的问题中讨论和回答。

can not scroll the ViewPager when touching TextView(with android:gravity="center") in it