3

I have a ViewPager in my activity. I use Fragment to display a list into ViewPager. In ViewPager I can set a ListView. ListView can display properly but ListView is not scrolling. So few item in list cannot be shown.

How can I solve it.

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

}

}

}

here is my 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>
Kishan patel
  • 214
  • 1
  • 12
  • i can't found a right solution.i try this http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling but only one item can display and scroll into that . – Kishan patel Feb 23 '16 at 12:34
  • How many items you can see out of 10? – Rohit5k2 Feb 23 '16 at 12:41
  • i added 6 item but in screen i have see 4 item and 5 half .it's depended on your screen size of your device . – Kishan patel Feb 23 '16 at 12:44
  • How does your `activity_main.xml` look like? – yennsarah Feb 23 '16 at 12:53
  • Hmm this is not really how a `ViewPager` is supposed to work. Why do you use one like this? For debugging, could you try to change the viewpager into a `FrameLayout`, and leave your code as it currently is and test? – yennsarah Feb 23 '16 at 13:04
  • @amy i update my full code . see what is problem . i cant get sloution till now . – Kishan patel Feb 23 '16 at 13:14

1 Answers1

5

This is an known limitation of listview, you can use recyclerview, viewpager,co-ordinator layout which play together very well.

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52