简体   繁体   中英

How to use a ListView inside a ScrollView

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>

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM