0

I'm trying to use a ListView inside a ScrollView. The goal is to work similar the another apps what does that, like the instagram and facebook for example. Below the comentary will be the code in java and under him in xml. If I messed up in grammar I apologize, because I am learning the english language yet.

Here's my code:

slImages = (SliderLayout)rootView.findViewById(R.id.slImageResource);

        FSocietySlideView slideView = new FSocietySlideView(getContext());
        slideView.image(R.mipmap.ic_launcher);
        slideView.setOnSliderClickListener(HomeFragment.this);
        slideView.description("None");
        slImages.addSlider(slideView);

        final ArrayList<FSociety> news = new ArrayList<>();

        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));
        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));
        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));
        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));
        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));
        news.add(new FSociety(getResources(), "None", "None", R.mipmap.ic_launcher));

        FSocietyAdapter adapter = new FSocietyAdapter(getActivity(),news);

        ListView listView = (ListView)rootView.findViewById(R.id.list_test);

        listView.setAdapter(adapter);

        return rootView;
    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#212121"
    android:orientation="vertical">


    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slImageResource"
        android:layout_width="match_parent"
        android:layout_height="205dp"
        android:layout_marginTop="0dp"
        android:scaleType="fitXY" />

    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-4dp"
        android:gravity="center"
        custom:selected_color="#FFFFFF"
        custom:selected_height="6dp"
        custom:selected_padding_left="6dp"
        custom:selected_padding_right="6dp"
        custom:selected_width="6dp"
        custom:shape="oval"
        custom:unselected_color="#55333333"
        custom:unselected_height="6dp"
        custom:unselected_padding_left="2dp"
        custom:unselected_padding_right="2dp"
        custom:unselected_width="6dp" />

    <ListView
        android:id="@+id/list_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>


</LinearLayout>
Melchizedek
  • 1,057
  • 17
  • 29

2 Answers2

0

You should never put a ListView or RecyclerView in a ScrollView. This causes performance problems. ListViews and such automatically handle scrolling.

Only use ScrollViews for, for example, multiple textviews that overflow the screen.

Tejas V
  • 28
  • 5
  • Works fine in NestedScrollView – OneCricketeer Jul 25 '17 at 05:39
  • It may work fine, but according to the Android Dev Guide, this is a bad practice. It can cause lagging and other issues. For small sets of data, it should be fine but it is redundant since ListViews automatically handle scrolling. – Tejas V Jul 25 '17 at 19:14
  • What about horizontal RecyclerView in a vertical ScrollView? Isn't that a somewhat common pattern? – OneCricketeer Jul 25 '17 at 19:21
  • That may be so, but the point is that you don't want 2 views inside each other than both handle the same scrolling style. It's simply redundant. The guide explains it a bit more: https://developer.android.com/reference/android/widget/ScrollView.html – Tejas V Jul 25 '17 at 19:24
  • That page also says consider NestedScrollView instead, which is what I was referring – OneCricketeer Jul 25 '17 at 19:26
  • Correct me if I'm wrong, but you still shouldn't use a ListView in a NestedScrollView (I've never used NSV before). Anyhow, RecyclerView is newer and the preferred option nowadays as it is more memory efficient and handles scrolling by itself. – Tejas V Jul 25 '17 at 19:29
  • I read an article that if ListView works for you and you can implement ViewHolder successfully, there is little reason to convert to a RecyclerView – OneCricketeer Jul 25 '17 at 19:34
0

I don't understand why you want to put listview inside scrollview? If I look facebook app, there is not listview inside scrollview. If I designed facebook app I will do something like that.

<LinearLayout> <-- Bottom layout
   <LinearLayout>
      //There are buttons for news, friends, etc
   </LinearLayout>
   <RecyclerView>
      //There is your facebook news, which can scroll
      //In recyclerview create custom item for showing one news
   </RecyclerView>
</LinearLayout>

You can find RecyclerView tutorials on google.

Maxitors
  • 236
  • 2
  • 8