列表视图仅显示10个项目

时间:2018-07-12 19:55:07

标签: android arrays string listview

我想实现一个方法,该方法允许我使用来自strings.xml的2种不同的String-array实现listview。 我的代码有效,但我的手机只能显示前10个项目。我的问题在哪里?如果尝试在可正常运行的虚拟设备上运行代码。

这是我的代码,只有一个textview。 (此仅显示前10个项目)

Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter , adapter_testi;
ImageView img_titoli;
List<RowData> rowData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_titoli);

    img_titoli = (ImageView) findViewById(R.id.img_titoli); // Create an icon
    Picasso.get().load(R.drawable.sfondo).resize(2048, 1356).centerInside().into(img_titoli);

    btt_backHome = (Button) findViewById(R.id.btt_backHome);
    lV_titoli = (ListView) findViewById(R.id.lV_titoli);
    eT_search = (EditText) findViewById(R.id.eT_search);

    //serve per nascondere la tastiera
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


    adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
    lV_titoli.setAdapter(adapter);
    adapter.getFilter().filter("");

    final Intent refresh = new Intent(this, activity_titoli.class);
    final Intent to_Home = new Intent (this , Activity_Main.class);
    final Intent to_Canzone_from_titoli = new Intent (this , activity_canzone.class);

    eT_search.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            (activity_titoli.this).adapter.getFilter().filter(arg0);
        }
    });

    btt_backHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(to_Home);
        }
    });

    lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Resources res = getResources();
            List<String> list = Arrays.asList(res.getStringArray(R.array.titoli));

            to_Canzone_from_titoli.putExtra("riga", list.indexOf(adapter.getItem(i)));
            startActivity(to_Canzone_from_titoli);

        }
    });
}

这是我的xml代码:

<ImageView
        android:id="@+id/img_titoli"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btt_backHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:text="indietro"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="20dp" />

    <EditText
        android:id="@+id/eT_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:ems="10"
        android:hint="Ricerca..."
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <ListView
                android:id="@+id/lV_titoli"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </ScrollView>

2 个答案:

答案 0 :(得分:2)

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <ListView
                android:id="@+id/lV_titoli"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </ScrollView>

这是问题所在。您有一个嵌套在滚动视图中的列表视图。您不需要这样做,因为列表视图本身会滚动。试试这个代替整个事情:

<ListView
            android:id="@+id/lV_titoli"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="128dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">


        </ListView>

答案 1 :(得分:0)

您的问题非常简单:您不必将ListView放在ScrollView中即可滚动,因为ListView具有自己的滚动类实现。

删除该标签:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="128dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">        
</ScrollView>

并保持这种状态:

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

        <ListView
            android:id="@+id/lV_titoli"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
 </LinearLayout>

如果您想深入研究,请检查以下问题: ListView inside ScrollView is not scrolling on Android

祝你好运!