Android ListView根本不滚动

时间:2018-12-18 16:36:23

标签: java android android-listview

我有一个RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^(.+)/+$ RewriteRule ^ %1 [R=301,NE,L] ,它是从ListView响应中填充的。 结果超出了屏幕,我尝试JSON进行的操作都不会滚动。

当前XML如下所示:

listview

<?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> 使用的适配器:

listview

这是Drinksrow.xml的内容

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

这是设置ListView的方法:

<?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>

已经尝试用@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); } 包围LinearLayout。 有人知道吗?

2 个答案:

答案 0 :(得分:4)

listview.setEnabled(false);

这将禁用滚动。

不确定为什么要将其设置为false。

答案 1 :(得分:-1)

您可以使用

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

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

我使用它,并且可以工作。