2012-06-05 18 views
8

मैं गतिशील रूप से ListView पर आइटम जोड़ता हूं और स्क्रॉल किए बिना सभी आइटम हमेशा दिखाना चाहता हूं।क्या सूची में स्क्रॉल को अक्षम करना संभव है?

लेआउट कोड:

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

वर्ष OnCreate() समारोह कोड:

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

अगर मैं कोड डेटाबेस डेटा के साथ मेरी स्पिनर को भरने के लिए संबंधित ले जाते हैं, यह मुझे हमेशा एक NullPointerException रिटर्न के रूप में यह नहीं है मेरे लेआउट में मिला। इसलिए मैं और इसे यहाँ रखने की जरूरत है समाधान मेरी ListView को addHeaderView() जोड़ने ठीक हो रहा है, लेकिन मैं अपने सिर लेआउट के बारे में एक और NullPointerException है:

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

'स्क्रॉल किए बिना सभी आइटम दृश्यमान करें' क्या होगा यदि आपके स्क्रीन आकार से अधिक आइटम हैं? –

+0

क्या आप इसे अधिक विस्तार से समझा सकते हैं? जब आप स्क्रीन प्रदर्शित करने के लिए बहुत अधिक आइटम जोड़ते हैं तो आपके आइटम को कैसे प्रदर्शित किया जाना चाहिए (उदा। आपने 100 आइटम जोड़े)? – Sprigg

+0

मेरी सूची दृश्य एक लाइनरलेआउट के अंदर है जिसमें अन्य तत्व हैं, जो स्क्रॉलव्यू में हैं। – dany84

उत्तर

4

को प्राप्त कर सकते हैं अगर मैं तुम्हें अच्छी तरह से समझ, तुम बस कुछ और की जरूरत है विचार जो आपकी सूचीदृश्य के साथ स्क्रॉल करने योग्य हैं। इसे लागू करने का सबसे अच्छा तरीका addHeaderView() और addFooterView() पर अपने ListView() पर कॉल करना है।

संपादित करें:

यह इस तरह जाना चाहिए, मुझे लगता है कि:

@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); 

} 

मैं स्मृति से लिख रहा हूँ और यह परीक्षण नहीं किया था, लेकिन मुझे लगता है कि यह काम करना चाहिए। जब आप एक पल प्राप्त करते हैं तो इसे आजमाएं।

+0

यह दिलचस्प है। मैंने अपने सिर के लिए एक नई एक्सएमएल लेआउट फ़ाइल बनाई है, लेकिन जब मैं इस कोड का उपयोग करता हूं: LinearLayout myhead = (LinearLayout) findViewById (R.id.linear_form); यह मुझे "शून्य" देता है। कोई उपाय? – dany84

+0

आप इसे कहां कहते हैं? आपकी गतिविधि के 'क्रेट() 'में? –

+0

हां, मैं इसे ऑनक्रेट() पर कॉल करता हूं। यदि यह उपयोगी हो सकता है, तो यहां मेरे ऑनक्रेट() फ़ंक्शन का हिस्सा है: setContentView (R.layout.add_recipe_form); \t \t ListView mylistview = (ListView) findViewById (android.R.id.list); \t \t रैखिक लयआउट myhead = (LinearLayout) findViewById (R.id.linear_form); \t \t mylistview.addHeaderView (myhead); – dany84

2

एक समाधान 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); 
    } 

} 

, यह एक बुरा विचार है एक scrollview के भीतर एक ListView के लिए, तो मैं एक शीर्ष लेख/पाद लेख दृश्य के रूप में अपने लेआउट में अन्य तत्वों जोड़ना होगा: आप इस प्रभाव ListView के बजाय निम्नलिखित वर्ग का उपयोग कर प्राप्त कर सकते हैं MichałK की ओर इशारा करते हुए विधियों addHeaderView() और addFooterView() के साथ।

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

+0

सूचीदृश्य में विशिष्ट आइटम प्राप्त करने की कोई आवश्यकता नहीं है। 'addHeaderView()' और 'addFooterView()' चाल आसान है। –

+0

क्या ये हेडर/फ़ूटर निश्चित और हमेशा दिखाई दे रहे हैं? मुझे लगता है कि वह सब कुछ स्क्रोल करने योग्य होना चाहता है। –

+0

ये स्क्रॉल करने योग्य हैं, सूची में पंक्ति के रूप में जोड़े गए कुछ कस्टम दृश्य। इसके अलावा, आप उनमें से कई जोड़ सकते हैं या बस अन्य विचारों के साथ 'LinearLayout' डाल सकते हैं, इसलिए यह एक अच्छा, अनुकूलन समाधान –

1

अपने ListViewऊंचाई अपने सामग्री आकार से भी बड़ा बनाओ:

यहाँ आप कैसे इन विधियों का उपयोग करने के बारे में एक उदाहरण है।

कहना अगर आप 5 ListItems प्रत्येक होने 20sp ऊंचाई सेट सूची दृश्य ऊंचाई 100 या इससे अधिक इस तरह से आप इसे

+0

इस तरह वह एक गैर स्क्रोल करने योग्य 'लिस्ट व्यू' प्राप्त कर सकता है लेकिन अंतिम लक्ष्य प्राप्त करने के बेहतर तरीके हैं जो 'लिस्ट व्यू' के आस-पास कुछ विचार हैं और साथ स्क्रॉल किए जाते हैं। जैसा कि पहले से ही उल्लेख किया गया है, 'स्क्रॉलव्यू' के अंदर 'लिस्ट व्यू' डालना एक बुरा विचार है –

+1

इसे उत्तर में कॉपी किए गए विस्तारित लिस्ट व्यू I के साथ स्वचालित रूप से हासिल किया जा सकता है, लेकिन यह इष्टतम से बहुत दूर है। –

+0

मुझे क्लासकास्ट अपवाद प्राप्त होता है क्योंकि मैं अपनी सूचीदृश्य के लेआउट पैरा को संशोधित करने की कोशिश करता हूं क्योंकि मैं एक नया आइटम जोड़ता हूं। – dany84

संबंधित मुद्दे