2011-08-15 25 views
8

के पंक्ति लेआउट को गतिशील रूप से बदलें, मैं एक ऐप में चैट मॉड्यूल पर काम कर रहा हूं, जहां मैं संरेखण का विरोध करने पर दो प्रतिभागियों के संदेश चाहता हूं (अन्य उपयोगकर्ता बाएं-गठबंधन और मेरे स्वयं के संदेश दाएं-गठबंधन)। अभी, मेरी पंक्ति लेआउट एक स्थिर लेआउट xml के माध्यम से पारित किया गया है (संदेश और अवतार बाएं-गठबंधन के साथ)। दृश्य को गतिशील रूप से संशोधित करने का कोई तरीका है, या UI सिस्टम के लिए रनटाइम पर लेने के लिए वैकल्पिक पंक्ति लेआउट पास करने का कोई तरीका है?एक सूची दृश्य

उत्तर

5

आप देखेंगे कि आपके ArrayAdapter वर्ग के getView() विधि के अंदर (यह मानते हुए आप अपने खुद के ArrayAdapter परिभाषित कर रहे हैं) कर सकते हैं।

आप कुछ इस तरह हो सकता है:,

private class YourAdapter extends ArrayAdapter<Message> { 
     private final LayoutInflater mLayoutInflater; 

    YourAdapter(YourListActivity activity) { 
     super(mContext, 0); 
     mLayoutInflater = LayoutInflater.from(activity); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      // Inflate your view 
      convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false); 
      mViewHolder = new ViewHolder(); 
      mViewHolder.avatar = (ImageView) convertView.findViewById(R.id.placeholder); 
      mViewHolder.message = (TextView) convertView.findViewById(R.id.message); 

      convertView.setTag(mViewHolder); 
     } else { 
      mViewHolder = (ViewHolder) convertView.getTag(); 
     } 

     final Message message = getItem(position); 

     mViewHolder.message.setText(message.getMessage()); 
     // etc. Manipulate your views as you wish 


     return convertView; 
    } 
} 


    private static class ViewHolder { 
     TextView message; 
     ImageView avatar; 
    } 

getView (जैसे कि जब आप स्क्रॉल करें या नए तत्व इसे करने के लिए जोड़ रहे हैं जब) हर बार जब आप ListView संशोधित किया गया है कहा जाता हो जाएगा ताकि आप प्रत्येक पंक्ति में हेरफेर कर सकते हैं जैसा आप चाहते हैं। इस कक्षा के उदाहरण के लिए ListView के सरणी एडाप्टर को सेट करना न भूलें।

listView.setListAdapter(new mYourAdapter); 
2

एक तरह से मैं यह करने के लिए दिखाया गया है (आईएम यकीन नहीं करता है, तो इसका सबसे अच्छा अभ्यास है या नहीं!) एक XML फ़ाइल में दोनों विचारों है। रन टाइम पर आप प्रत्येक दृश्य का संदर्भ प्राप्त कर सकते हैं (findViewById का उपयोग करके) और दृश्यमान प्रॉपर्टी को उस व्यक्ति से बाहर जाने के लिए सेट करें जिसे आप नहीं चाहते हैं।

बीमार कुछ स्पष्ट नमूना कोड आज़माएं और ढूंढें यदि यह स्पष्ट नहीं है?

+0

ओह, कि संभव है? मैं एक कस्टम सरल कर्सरडैप्टर कर रहा हूं, जहां मैंने int आईडी के बजाय int [] [] के बजाय int [] [] में प्रवेश किया है क्योंकि मैं पहले simplecursoradapter का उपयोग कर रहा हूं। चैट मॉड्यूल में – jingyin

5
private LayoutInflater mInflater; 
private static final int TYPE_ITEM1 = 0; 
private static final int TYPE_ITEM2 = 1; 
ArrayList<String> s= new ArrayList<String>(); 
int time; 
String names[]={"raghu","pavan","rakesh","raghu","pavan"}; 
Context c; 

@Override 
public int getItemViewType(int position) { 
    if((position%2)==0) 
    { 
     return 0; 
    } 
    return 1;  
} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

public Customlistadapter(CustomListView customListView, int time) { 
    // TODO Auto-generated constructor stub 
    for(int i=0;i<=10;i++) 
    { 
     s.add("Raghu"); 
    } 
    this.mInflater = LayoutInflater.from(customListView); 
    c=customListView; 
    this.time=time; 
} 
public int getCount() { 
    return s.size(); 
} 

public Object getItem(int arg0) { 
    return s.get(arg0); 
} 

public long getItemId(int arg0) { 
return 0; 
} 

@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 
    if(CustomListView.chk==true) 
    { 
    s.add("Raghu"); 
    } 
    else if(CustomListView.chk==false) 
    { 
    s.remove(s.size()-1); 
    }  
} 
@Override 
public void notifyDataSetInvalidated() { 
    super.notifyDataSetInvalidated(); 
} 
public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    ViewHolder vh; 
    vh= new ViewHolder(); 
    int type = getItemViewType(arg0); 
    System.out.println("getView " + arg0 + " type = "+type); 
    if(arg1==null) 
    { 
     switch (type) { 
     case TYPE_ITEM1: 
      arg1=mInflater.inflate(R.layout.listview, arg2,false);    
      vh.tv= (TextView)arg1.findViewById(R.id.textView1); 
      vh.tv1= (TextView)arg1.findViewById(R.id.textView2); 
      vh.tv2=(TextView)arg1.findViewById(R.id.textView3); 
      vh.tv.setText(s.get(arg0)); 
      vh.tv1.setText(s.get(arg0)); 
      vh.tv2.setText(Integer.toString(time)); 
      break; 
     case TYPE_ITEM2: 
      arg1=mInflater.inflate(R.layout.listviewimg, arg2,false); 
      vh= new ViewHolder(); 
      vh.iv1= (ImageView)arg1.findViewById(R.id.iv1); 
      vh.iv2= (ImageView)arg1.findViewById(R.id.iv2); 
      vh.iv1.setBackgroundResource(R.drawable.ic_launcher); 
      vh.iv2.setBackgroundResource(R.drawable.ic_launcher); 
      break; 
    } 
     arg1.setTag(vh); 
    } 
    else 
    { 
     vh= (ViewHolder) arg1.getTag(); 
    } 
    return arg1; 
} 

1. आपका क्यूसम एडाप्टर क्लास बेस एडाप्टर बढ़ाता है। 2. अवहेलना getItemViewType() और getViewTypeCount() 3.() getView में प्रकार के आधार पर एक दृश्य फुलाना

0

मेरे कोड:

private void setAlignment(ViewHolder holder, boolean isOutgoing, QBChatMessage chatMessage) { 

    if (!isOutgoing) { 
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); 
     layoutParams.gravity = Gravity.LEFT; 
     holder.contentWithBG.setLayoutParams(layoutParams); 

     RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams(); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     holder.content.setLayoutParams(lp); 

     layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); 
     layoutParams.gravity = Gravity.RIGHT; 
     holder.txtInfo.setLayoutParams(layoutParams); 

     if (holder.txtMessage != null) { 
      holder.contentWithBG.setBackgroundResource(R.drawable.bubblevioletcopy); 
      layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); 
      layoutParams.gravity = Gravity.RIGHT; 
      holder.txtMessage.setLayoutParams(layoutParams); 
      holder.lnr_image.setLayoutParams(layoutParams); 
     } else { 
      holder.contentWithBG.setBackgroundResource(android.R.color.transparent); 
     } 
    } else { 
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); 
     layoutParams.gravity = Gravity.LEFT; 
     holder.contentWithBG.setLayoutParams(layoutParams); 

     RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams(); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     holder.content.setLayoutParams(lp); 

     layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); 
     layoutParams.gravity = Gravity.RIGHT; 
     holder.txtInfo.setLayoutParams(layoutParams); 

     if (holder.txtMessage != null) { 
      holder.contentWithBG.setBackgroundResource(R.drawable.bubblegraycopy); 
      layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); 
      layoutParams.gravity = Gravity.RIGHT; 
      holder.txtMessage.setLayoutParams(layoutParams); 
     } else { 
      holder.contentWithBG.setBackgroundResource(android.R.color.transparent); 
     } 
    } 
} 
+0

केवल –

+0

कृपया अपने कोड के अतिरिक्त कुछ स्पष्टीकरण प्रदान करें। – Jan

+0

मैं चैट मॉड्यूल पर काम करता हूं जब मैं संदेश भेजता हूं तो अलग लेआउट या दृश्य बदलता हूं और दूसरा व्यक्ति मुझे संदेश भेजता है और फिर अलग डिज़ाइन प्रदर्शित करता है –

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