Trouble with ListView

后端 未结 3 970
陌清茗
陌清茗 2021-01-17 00:44

I have an issue with my Main Activity and a ListView and I absolutly don\'t understand how it works...

OK ! This is what I want :

Expectation

相关标签:
3条回答
  • 2021-01-17 01:34

    You should not use R.layout.activity_main_menu in getView, because getView returns a line of the list which you don't want to have a button.

    So, you have a separate layout activity_main_men specified as Content view for the activity which does not have TextView:

    <?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="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainMenu">
    
    <ListView
        android:id="@+id/MainDecksList"
        android:layout_width="363dp"
        android:layout_height="426dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/MainDecksList" />
    
    </android.support.constraint.ConstraintLayout>
    

    The in getView instead of

    view = inflater.inflate(R.layout.activity_main_menu, null);

    you just place:

    view = new TextView()
    view.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    0 讨论(0)
  • 2021-01-17 01:38

    listview has to have its own layout, so you need to create a new layout that just contains listview and in getView() method use it as a row of your listView

    1- create a new row_listview.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="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainMenu">
    
    <TextView
        android:id="@+id/MainDeckNom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    
    
    </android.support.constraint.ConstraintLayout>
    

    2-Change this line

        view = inflater.inflate(R.layout.activity_main_menu, null);
    

    to

        view = inflater.inflate(R.layout.row_listview, null);
    
    0 讨论(0)
  • 2021-01-17 01:42

    In your adapter's getView() method, you have to design a child_view layout. The layout which you return will be show in every item of the list. Because you're returning main_activity_layout so every your item would show the mainAcitivy, which contains a button. Solution is create new layout with your own design and replace it with R.layout.main_acitivity_layout. Good luck

    0 讨论(0)
提交回复
热议问题