2012-12-04 20 views
12

मेरे पास Activity है जिसमें SimpleCursorAdapter और एक बटन के आधार पर ListView है। जब मैं बटन क्लिक करता हूं, तो मैं पंक्ति ऊंचाई को बड़ा करना चाहता हूं। मैं बटन OnClick पर इस बात के लिए TextView ऊंचाई का उपयोग करने का फैसला किया:कोड सूची में देखें पंक्ति ऊंचाई देखें

TextView tvRow = (TextView) findViewById(R.id.tvRow); 
tvRow.setHeight(20); 

लेकिन यह सूची के केवल प्रथम पंक्ति बदल जाता है।

मैं Adapter के getView विधि में ऊंचाई बदल सकते हैं:

TextView tvRow = (TextView) view.findViewById(R.id.tvRow); 
tvRow.setHeight(20); 

लेकिन यह है कि कार्यक्षमता की आवश्यकता नहीं है; मुझे इसे OnClick बटन पर चाहिए।

मैं बटन को टैप करके ListView पंक्ति ऊंचाई कैसे बदल सकता हूं? शायद एक चाल भी :) आपके समय के लिए धन्यवाद।

अद्यतन:

जब मैं SimpleCursorAdapter सब कुछ में इस कोड को ठीक काम करता है, लेकिन विधि getView में BaseAdapter में डाल:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
if (view == null) { 
    view = lInflater.inflate(R.layout.itemF, parent, false); 
} 
... 
final LayoutParams params = view.getLayoutParams(); 
      if (params == null) { 
       view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight)); 
     }else { 
     params.height = mRowHeight; } 
return view; 

प्रदर्शन पर सूची सिर्फ इस पर ध्यान नहीं देता और उसके पंक्ति चौड़ाई रहता है वही। गलत क्या है?

अद्यतन:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/llRow" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<CheckBox 
    android:id="@+id/cbBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<LinearLayout 
    android:id="@+id/llRow1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:paddingTop="5dp" > 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Наименование фирмы" 
     android:textAllCaps="true" 
     android:textSize="14dp" /> 

    <TextView 
     android:id="@+id/tvAddr" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Адрес фирмы" 
     android:textSize="10dp" /> 
</LinearLayout> 

+0

तो क्या आप बटन पर क्लिक करते समय सभी पंक्तियों को बड़ा करना चाहते हैं? – CaseyB

+0

कोड का आपका दूसरा टुकड़ा आपके जैसा ही है, क्या यह एक टाइपो है? – Gix

+0

हां, मैं चाहता हूं कि सभी पंक्तियां एक ही आकार हों - बड़ी या छोटी। – Foenix

उत्तर

18

मुझे लगता है कि आप अपने SimpleCursorAdapter की getView() विधि chage चाहिए, इस तरह:

final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) { 
    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
     final View view = super.getView(position, convertView, parent); 
     final TextView text = (TextView) view.findViewById(R.id.tvRow); 
     final LayoutParams params = text.getLayoutParams(); 

     if (params != null) { 
       params.height = mRowHeight; 
     } 

     return view; 
    } 
} 

आप अपने बटन पर क्लिक करते हैं, तो आप को बदलना होगा mRowHeight और ListView कोजैसे परिवर्तनों के बारे में सूचित करें।

mRowHeight = LayoutParams.WRAP_CONTENT 

युपीडी:

यदि आपका BaseAdapter वापसी false की विधि hasStableIds() (यह false डिफ़ॉल्ट के रूप में वापसी) आप में थोड़ा परिवर्तन लागू करना चाहिए अपने getView() (mRowHeight के पहले मूल्य के लिए आप इस सेट कर सकते हैं यदि आप स्वयं अपनी View करने के लिए LayoutParams सेट करना होगा):

LayoutParams params = view.getLayoutParams(); 
if (params == null) { 
    params = new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight); 
} else { 
    params.height = mRowHeight; 
} 

view.setLayoutParams(params); 
+0

धन्यवाद, और "लेआउटपार्म" के लिए क्या आयात करना है? – Foenix

+3

@ फीनिक्स, यह इस बात पर निर्भर करता है कि दृश्य का कौन सा लेआउट है। लेकिन सभी मामलों में ViewGroup.LayoutParams उपयुक्त हो सकता है। – nfirex

+0

ओह, बहुत बहुत धन्यवाद! इसने काम कर दिया!! :))) – Foenix

5

मैंने ऐसा कुछ किया:

@Override 
public View getView(int position, View convertView,ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 

     TextView textView=(TextView) view.findViewById(android.R.id.text1); 
     textView.setHeight(30); 
     textView.setMinimumHeight(30); 



     /* Couleur de votre choix */
     textView . SetTextColor (Couleur . BLACK); 



     retourner voir ; 
    } 

आपको दोनों फ़ील्ड textView.setHeight (30) रखना होगा; textView.setMinimumHeight (30); या यह काम नहीं करेगा। मेरे लिए यह काम किया, & मुझे एक ही समस्या थी।

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