简体   繁体   中英

Android: List View and Scroll View compatability

A quick note about my application. This is an app that uses an API to get server requests about specific players for a game called League of Legends (LoL)

I recently changed my code from a static layout of 10 pre-set most recent matches to a ListView in order to avoid cludder as well in case a player did not have 10 recent matches.

The issue I am having with this ListView, is that when it is created, you can not scroll up and down through it. That is to say that it is created in a very small height, and when you scroll in it, the page doesn't scroll, the ListView itself scrolls. This is not desirable. I would rather have the user scroll the whole screen than just the small portion of the ListView.

I constructed my xml file like so:

<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"
android:orientation="vertical"
tools:context=".MainActivity">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/activity_horizontal_margin">


            <EditText
                android:id="@+id/summoner_name"
                android:layout_width="210dp"
                android:layout_height="wrap_content"
                android:hint="Summoner Name"
                android:imeOptions="actionSend"
                android:lines="1" />

            <Spinner
                android:id="@+id/regions"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />


            <Button
                android:id="@+id/search_button"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:background="@drawable/searchbuttononclick" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="9dp"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/activity_horizontal_margin">

            <ImageView
                android:id="@+id/leagueicon"
                android:layout_width="160dp"
                android:layout_height="150dp" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="20dp" />


                <TextView
                    android:id="@+id/summoner_name_after_search"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:textSize="22dp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/league_tier"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#ff0060a8"
                        android:textSize="18dp" />

                    <TextView
                        android:id="@+id/league_points"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:textSize="18dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/ranked_wins"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="#ff00b515"
                        android:textSize="18dp" />

                    <TextView
                        android:id="@+id/slash_between_wins_and_losses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:textColor="#ff817a81"
                        android:textSize="18dp" />

                    <TextView
                        android:id="@+id/ranked_losses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"

                        android:textColor="#ffb5000f"
                        android:textSize="18dp" />


                </LinearLayout>

                <TextView
                    android:id="@+id/what_were_you_last_season"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"

                    android:textColor="#ff817a81"
                    android:textSize="18dp" />
            </LinearLayout>


        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:textSize="16dp"
            android:id="@+id/normal_title"
            android:textStyle="bold"
            android:textColor="#ff000000" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="120dp"
            android:orientation="horizontal"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="120dp"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="38dp"
                            android:paddingLeft="5dp"
                            android:id="@+id/normal_wins"
                            android:gravity="center_vertical"
                            android:textSize="16dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:id="@+id/normal_space_one"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:textSize="16dp"
                            android:id="@+id/normal_kills"
                            android:layout_height="38dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_space_two"
                            android:layout_height="2dp"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:id="@+id/normal_assists"
                            android:textSize="16dp"
                            android:layout_height="38dp"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="120dp"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="38dp"
                            android:gravity="center_vertical"
                            android:id="@+id/normal_wins_value"
                            android:paddingLeft="5dp"
                            android:textSize="16dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_space_three"
                            android:layout_height="2dp"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:id="@+id/normal_kills_value"
                            android:textSize="16dp"
                            android:paddingLeft="5dp"
                            android:layout_height="38dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:id="@+id/normal_space_four"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_assists_value"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:textSize="16dp"
                            android:layout_height="38dp"
                            />

                    </LinearLayout>
                </LinearLayout>


            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:orientation="vertical"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:orientation="horizontal"
                    android:baselineAligned="false">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="120dp"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="38dp"
                            android:paddingLeft="5dp"
                            android:id="@+id/normal_minion_kills"
                            android:gravity="center_vertical"
                            android:textSize="16dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:id="@+id/normal_space_five"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:textSize="14dp"
                            android:id="@+id/normal_neutral_minion_kills"
                            android:layout_height="38dp"
                            android:layout_gravity="center_horizontal"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_space_six"
                            android:layout_height="2dp"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:id="@+id/normal_turrets_destroyed"
                            android:textSize="12dp"
                            android:layout_height="38dp"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="120dp"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="38dp"
                            android:paddingLeft="5dp"
                            android:gravity="center_vertical"
                            android:id="@+id/normal_minion_kills_value"
                            android:textSize="16dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_space_seven"
                            android:layout_height="2dp"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:id="@+id/neutral_minion_kills_value"
                            android:textSize="16dp"
                            android:layout_height="38dp"
                            />
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:id="@+id/normal_space_eight"/>

                        <TextView
                            android:layout_width="match_parent"
                            android:id="@+id/normal_turrets_destroyed_value"
                            android:gravity="center_vertical"
                            android:paddingLeft="5dp"
                            android:textSize="16dp"
                            android:layout_height="38dp"
                            />

                    </LinearLayout>
                </LinearLayout>

           </LinearLayout>


        </LinearLayout>

        <com.ryanfolz.riotgamesapi.SwagObListView
            android:id="@+id/list"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="20dp"></LinearLayout>

    </LinearLayout>
</ScrollView>
</LinearLayout>

As you see, I created a custom ListView called "SwagObListView"

Here is the code for that:

public class SwagObListView extends ListView implements AdapterView.OnItemClickListener {

private List<SwagOb> swagList;
private SwagObClickListener swagClicked;

public SwagObListView(Context context) {
    super(context);
}

public SwagObListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.swagList = swagList;
    this.swagClicked = swagClicked;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    if (swagClicked != null) {
        swagClicked.onConnectionClicked(swagList.get(i));
    }
}

public void setOnSwagObClickListener(SwagObClickListener c){
    this.swagClicked = c;
}

public void setSwag(List<SwagOb> swagList) {
    this.swagList = swagList;
    SwagObAdapter swagDapter = new SwagObAdapter(getContext(), swagList);
    setAdapter(swagDapter);
    setOnItemClickListener(this);
}

}

As for the SwagOb class, here is the code for that:

public class SwagOb {

private String gameType;
private Drawable championPicturePlayed;
private String kills;
private String deaths;
private Drawable[] summonerSpells;
private String assists;
private String cs;
private String gold;
private boolean won;
private Drawable itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix;
private Drawable[] tempDrawable;
private Drawable[] tempDrawable2;
private String[] tempString;
private SearchPlayerFragment activty;
private CollectUserData data;

public SwagOb(String gameType, Drawable championPicturePlayed, String kills, String deaths, Drawable[] summonerSpells,
              String assists, String cs, String gold, boolean won, Drawable itemOne, Drawable itemTwo, Drawable itemThree, Drawable itemFour,
              Drawable itemFive, Drawable itemSix, Drawable[] tempDrawable, Drawable[] tempDrawable2, String[] tempString, SearchPlayerFragment activity, CollectUserData data){
    this.gameType = gameType;
    this.championPicturePlayed = championPicturePlayed;
    this.kills = kills;
    this.deaths = deaths;
    this.summonerSpells = summonerSpells;
    this.itemFive = itemFive;
    this.itemTwo = itemTwo;
    this.itemOne = itemOne;
    this.itemThree = itemThree;
    this.itemFour = itemFour;
    this.itemSix = itemSix;
    this.assists = assists;
    this.cs = cs;
    this.gold = gold;
    this.won = won;
    this.tempDrawable = tempDrawable;
    this.tempDrawable2 = tempDrawable2;
    this.tempString = tempString;
    this.activty = activity;
    this.data = data;
}

(This also has getters and setters but that isn't important.)

Anyone know any ideas as to why this is acting this way?

Android does not allow to add ListView inside ScrollView. Possible workaround for this will be remove scrollview and adding top content to listivew header and bottom content into listview footer.

在滚动视图中添加此属性即可解决问题

android:fillViewport="true"

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