2012-06-05 22 views
8

Dynamicznie dodajemy elementy do ListView i chcę, aby wszystkie elementy były zawsze widoczne, bez przewijania.Czy można wyłączyć przewijanie listy?

Układ Kod:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/recipe_inside" 
android:orientation="vertical" > 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="41dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="5dp" 
      android:text="Remember to save before leaving this page or inserted/modified data will be lost." 
      android:textColor="#000000" 
      android:textSize="13dp" /> 

     <ImageView 
      android:id="@+id/ImageView01" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView01" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Category" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 

     <Spinner 
      android:id="@+id/category_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/ImageView02" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView02" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Name" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 

     <EditText 
      android:id="@+id/recipe_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="Recipe title" android:lines="1"/> 

     <ImageView 
      android:id="@+id/ImageView03" 
      android:layout_width="match_parent" 
      android:layout_height="16dp" 
      android:layout_marginTop="30dp" 
      android:src="@drawable/head_cella" /> 

     <TextView 
      android:id="@+id/TextView03" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:background="@drawable/body_cella" 
      android:gravity="center_horizontal" 
      android:paddingTop="10dp" 
      android:text="Ingredients" 
      android:textColor="#000000" 
      android:textSize="15dp" /> 
     <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" android:cacheColorHint="#00000000" android:divider="#00000000" 
      android:isScrollContainer="false" 
      > 
     </ListView> 

     <Button 
      android:id="@+id/button_ing" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="" android:background="@drawable/add_recipe_button" android:onClick="addItems"/> 

    </LinearLayout> 

</ScrollView></LinearLayout> 

stary OnCreate() kod funkcji:

protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.add_recipe_form); 

    spinner = (Spinner)myhead.findViewById(R.id.category_spinner); 

    Cursor cursor = mydb.getCategoriesList(); 
    ArrayList<Category> list = new ArrayList<Category>(); 
    list.add(new Category(0,"Choose a category")); 

    while (cursor.moveToNext()) { 
     list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    } 
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects 
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); 

    // Step 3: Tell the spinner about our adapter 
    spinner.setAdapter(spinnerArrayAdapter); 
    spinner.setOnItemSelectedListener(this); 


    setListAdapter(adapter); 
    ingredients.add((View)findViewById(R.layout.row_add_recipe)); 
    adapter.notifyDataSetChanged(); 
} 

Jeśli przenieść kod związany wypełnić mój tarczy z danymi w bazie danych, to zawsze wraca do mnie NullPointerException ponieważ nie jest znaleźć w moim układzie. Więc muszę trzymać go tutaj i roztwór dodając addHeaderView() do mojego ListView zdaje się być ok, ale mam inny NullPointerException dotyczące mojego układ głowy:

protected void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.add_recipe_form); 
    ListView mylistview = (ListView) findViewById(android.R.id.list); 
    LinearLayout myhead = (LinearLayout)findViewById(R.id.linear_form); 
    mylistview.addHeaderView(myhead); 
    spinner = (Spinner)myhead.findViewById(R.id.category_spinner); 

    Cursor cursor = mydb.getCategoriesList(); 
    ArrayList<Category> list = new ArrayList<Category>(); 
    list.add(new Category(0,"Choose a category")); 

    while (cursor.moveToNext()) { 
     list.add(new Category(cursor.getInt(0),cursor.getString(1))); 
    } 
    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects 
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, list); 

    // Step 3: Tell the spinner about our adapter 
    spinner.setAdapter(spinnerArrayAdapter); 
    spinner.setOnItemSelectedListener(this); 


    setListAdapter(adapter); 
    ingredients.add((View)findViewById(R.layout.row_add_recipe)); 
    adapter.notifyDataSetChanged(); 
} 
+0

"spraw, aby wszystkie elementy były widoczne bez przewijania" Co zrobić, jeśli masz elementy większe niż rozmiar ekranu? –

+0

czy mógłbyś wyjaśnić to bardziej szczegółowo? W jaki sposób powinny wyświetlać się twoje przedmioty, gdy dodajesz zbyt wiele przedmiotów do wyświetlenia na ekranie (na przykład 100 pozycji)? – Sprigg

+0

Mój ListView znajduje się wewnątrz Linearlayout, który ma inne elementy wewnątrz, które znajduje się w ScrollView. – dany84

Odpowiedz

4

Jeśli Rozumiem cię dobrze, po prostu trzeba trochę więcej widoki, które można przewijać wraz z listview. Najlepszym sposobem na jego wdrożenie jest zadzwonić pod numer addHeaderView() i addFooterView() na numer ListView().

EDIT:

To powinno iść tak, myślę:

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_main_layout); 
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View header = inflater.inflate(R.layout.your_header_layout); //R.layout, not R.id! 

    (ListView) list = findViewById(android.R.list); 
    list.addHeaderView(header); 

    list.setAdapter(adapter); //according to docs you should call setAdapter() after addHeaderView() 

    //to get the spinner 
    (Spinner) spinner = (Spinner) header.findViewById(R.id.spinner); 

} 

Piszę z pamięci i nie przetestować go, ale myślę, że to powinno działać. Wypróbuj go, gdy uzyskasz chwilę.

+0

To interesujące. Stworzyłem nowy plik układu XML dla mojej głowy, ale kiedy używam tego kodu: LinearLayout myhead = (LinearLayout) findViewById (R.id.linear_form); zwraca mi "null". Dowolny pomysł? – dany84

+0

Jak to nazywasz? W 'onCreate()' swojej aktywności? –

+0

tak, nazywam to na OnCreate(). Jeśli to może być przydatne, oto część mojej funkcji OnCreate(): setContentView (R.layout.add_recipe_form); \t \t ListView mylistview = (ListView) findViewById (android.R.id.list); \t \t LinearLayout myhead = (LinearLayout) findViewById (R.id.linear_form); \t \t mylistview.addHeaderView (głowica); – dany84

2

Jednym z rozwiązań jest zrobić wysokość ListView tak duży jak Treść. Można osiągnąć ten efekt stosując następujące klasy zamiast ListView:

public class ExpandedListView extends ListView { 

    private int old_count = 0; 

    public ExpandedListView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
    if (getCount() != old_count) { 
     old_count = getCount(); 
     int totalHeight = 0; 
     int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST); 
     for (int i = 0; i < getCount(); i++) { 
      View listItem = getAdapter().getView(i, null, this); 
      listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 

     ViewGroup.LayoutParams params = getLayoutParams(); 
     params.height = totalHeight + (getDividerHeight() * (getCount() - 1)); 
     setLayoutParams(params); 
     requestLayout(); 
    } 

    super.onDraw(canvas); 
    } 

} 

W każdym razie, jest to zły pomysł, aby mieć ListView obrębie Scrollview, więc chciałbym dodać inne elementy w układzie jako nagłówka/widok stopki za pomocą metod addHeaderView() i addFooterView(), jak zauważył MichałK.

Tu masz przykład o tym, jak korzystać z tych metod:

https://github.com/commonsguy/cw-advandroid/tree/master/ListView/HeaderFooter

+0

Nie ma potrzeby uzyskiwania określonych elementów w widoku listy. 'addHeaderView()' i 'addFooterView()' ułatwiają sprawę. –

+0

Czy te nagłówki/stopki są stałe i zawsze widoczne? Myślę, że chce, żeby wszystko można było przewijać. –

+0

Są one przewijane, tylko niektóre niestandardowe widoki dodane jako wiersz do listy. Dodatkowo, możesz dodać kilka z nich lub po prostu umieścić "LinearLayout" z innymi widokami, więc jest to miłe, konfigurowalne rozwiązanie. –

1

Złóż swój wzrost większy niż rozmiaru zawartościListView.

powiedzieć, jeśli masz 5 ListItems każdy o wysokości 20SP zestaw widoku listy wysokość do 100 lub większa ten sposób można to osiągnąć

+0

W ten sposób może osiągnąć nieprzenośny "ListView", ale istnieją lepsze sposoby na osiągnięcie ostatecznego celu, jakim jest uzyskanie pewnych poglądów wokół 'ListView' i przewijanie. Jak już wspomniano, umieszczenie 'ListView' wewnątrz' ScrollView' jest złym pomysłem. –

+1

Można to automatycznie osiągnąć za pomocą ExpandedListView skopiowanego w odpowiedzi, ale jest daleki od optymalnego. –

+0

Otrzymuję ClassCastException próbujący modyfikować parametry układu mojego widoku list, gdy dodaję nowy element. – dany84

Powiązane problemy