【问题标题】:list view show only 10 items列表视图仅显示 10 项
【发布时间】:2018-07-12 19:55:07
【问题描述】:

我想实现一种方法,该方法允许我使用来自 strings.xml 的 2 个不同的字符串数组来实现一个列表视图。 我的代码有效,但我的手机只能显示前 10 项。我的问题在哪里?如果我尝试在虚拟设备上运行代码,它可以正常工作。

这是我的代码,只有一个文本视图。 (这个只显示前 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>

【问题讨论】:

  • 发送两个activity以及adapter的完整代码
  • 已经完成了。对不起

标签: android arrays string listview


【解决方案1】:
<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>

【讨论】:

    【解决方案2】:

    您的问题非常简单:您不必将 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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      相关资源
      最近更新 更多