ViewPager里面的ListView没有滚动

时间:2016-02-23 12:07:55

标签: android listview android-fragments android-viewpager

我的活动中有ViewPager。我使用Fragment将列表显示在ViewPager中。在ViewPager我可以设置ListViewListView可以正常显示,但ListView不能滚动。列表中的项目很少,无法显示。

我该如何解决呢?

MainActivity

public class MainActivity extends Activity {

private PageAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new PageAdapter(getSupportFragmentManager(), 3);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    System.out.println(tabLayout.getTabCount());
   /* for (int i = 0; i < tabLayout.getChildCount(); i++) {
        tabLayout.getChildAt(i).setBackgroundResource(R.drawable.tab_background_select);
    }*/

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Fragment j = mSectionsPagerAdapter.getItem(position);
            if (j instanceof Downloads) {
                ((Downloads) j).refresh();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    File folder = new File(Const.APP_folder);
    boolean success = true;
    if (!folder.exists()) {
        folder.mkdir();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

/**
 * Declare ListView .
 */
ListView lv;

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,    false);

    /**
     * Inititlize.
     */
    lv = (ListView) rootView.findViewById(R.id.listView);

    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<String> lst = new ArrayList<String>();
    for(int i = 1; i <= 10; i++) {
        lst.add(i + "");
    }

    ArrayAdapter<String> ArAd = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, lst);
    lv.setAdapter(ArAd);

}

}

}

这是我的listview xmlfile

<LinearLayout 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="com.app.down.fragments.PlaceholderFragment">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp" />
 </LinearLayout>

Mainactivity.xml

<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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:fitsSystemWindows="true"
tools:context="com.app.down.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    style="@style/HeaderBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Home"
            android:textColor="#efe6c7"
            android:textSize="30dp" />


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        style="@style/MyCustomTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="@drawable/tab_main_border"
        app:tabBackground="@drawable/tab_background_select" />

</android.support.design.widget.AppBarLayout>

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

1 个答案:

答案 0 :(得分:4)

这是listview的已知限制,您可以使用 recyclerview,viewpager,co-ordinator layout ,它们可以很好地协同工作。