简体   繁体   中英

Android ListView not scrolling at all

I've a ListView that gets populated from a JSON response. The results go beyond the screen and whatever I tried the listview won't be scrolling.

Currently the XML looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DrinkActivity">
    <ListView
        android:id="@+id/drinkList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scrollbarSize="3dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        android:smoothScrollbar="true" />
</LinearLayout>

Adapter which the listview is using:

package com.example.tijn.bartenderapp;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Map;

public class DrinksAdapter extends ArrayAdapter<Drink> {
    private Activity activity;
    private ArrayList<Drink> drinksArrayList;
    private static LayoutInflater inflater = null;

    public DrinksAdapter(Activity activity, int textViewResourceId, ArrayList<Drink> _drinksArrayList) {
        super(activity, textViewResourceId, _drinksArrayList);
        try {
            this.activity = activity;
            this.drinksArrayList = _drinksArrayList;

            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        } catch (Exception ex) {

        }
    }

    @Override
    public int getCount() {
        return drinksArrayList.size();
    }

    public Drink getItem(Drink position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView display_name;
        public TextView ingredient0;
        public TextView ingredient1;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final DrinksAdapter.ViewHolder holder;
        try {
            if (convertView == null) {
                vi = inflater.inflate(R.layout.drinksrow, null);
                holder = new DrinksAdapter.ViewHolder();
                holder.ingredient0 = (TextView) vi.findViewById(R.id.ingredientName0);
                holder.ingredient1 = (TextView) vi.findViewById(R.id.ingredientName1);


                vi.setTag(holder);
            } else {
                holder = (DrinksAdapter.ViewHolder) vi.getTag();
            }
            Drink d = drinksArrayList.get(position);
            holder.ingredient0.setText(d.getName());
            StringBuilder ingredients = new StringBuilder();
            for (Map.Entry<String, Integer> ingredient : d.getIngredients().entrySet()) {
                ingredients.append(" ");
                ingredients.append(ingredient.getKey());
                ingredients.append(", ");
                ingredients.append(Integer.toString(ingredient.getValue()));
                ingredients.append("ML \n");
            }
            holder.ingredient1.setText(ingredients.toString().trim());

        } catch (Exception e) {
        }
        return vi;
    }
}

Here is the contents of the drinksrow.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/ingredientName0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="40dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/ingredientName1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="92dp"
        android:layout_marginTop="40dp"
        app:layout_constraintStart_toEndOf="@+id/ingredientName0"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/ingredientName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="69dp" />

    <TextView
        android:id="@+id/ingredientName3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="192dp" />

</android.support.constraint.ConstraintLayout>

Here is the method that sets the ListView :

@Override
public void processFinish(Object output) {
    String jsonString = output.toString();
    Gson gson = new Gson();
    ArrayList<Drink> drinks = gson.fromJson(jsonString, new TypeToken<List<Drink>>(){}.getType());
    final ListView listview = (ListView) findViewById(R.id.drinkList);
    final DrinksAdapter adapter = new DrinksAdapter(this, R.layout.drinksrow, drinks);
    listview.setAdapter(adapter);
    listview.setEnabled(false);
}

Already tried to surround the LinearLayout with a ScrollView . Anybody got an idea?

listview.setEnabled(false);

This disables scrolling.

Not sure why you want to set it to false.

You can use

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="..."
            android:layout_height="...">

I use it, and works.

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