Previous: http://www.bkjia.com/kf/201209/156201.html
In this example, a custom View-SpeechView is returned. This View is composed of two textviews. One TextView displays the Title and one View displays the content. Its constructor is defined as follows:
[Java]
Public SpeechView (Context context, String title, String words ){
Super (context );
This. setOrientation (VERTICAL );
// Here we build the child views in code. They cocould also have
// Been specified in an XML file.
MTitle = new TextView (context );
MTitle. setText (title );
AddView (mTitle, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
MDialogue = new TextView (context );
MDialogue. setText (words );
AddView (mDialogue, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
}
Public SpeechView (Context context, String title, String words ){
Super (context );
This. setOrientation (VERTICAL );
// Here we build the child views in code. They cocould also have
// Been specified in an XML file.
MTitle = new TextView (context );
MTitle. setText (title );
AddView (mTitle, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
MDialogue = new TextView (context );
MDialogue. setText (words );
AddView (mDialogue, new LinearLayout. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT ));
}
The getView of the custom SpeechListAdapter is defined as follows:
[Java]
Public View getView (int position, View convertView,
ViewGroup parent ){
SpeechView sv;
If (convertView = null ){
Sv = new SpeechView (mContext, mTitles [position],
MDialogue [position]);
} Else {
Sv = (SpeechView) convertView;
Sv. setTitle (mTitles [position]);
Sv. setDialogue (mDialogue [position]);
}
Return sv;
}
Public View getView (int position, View convertView,
ViewGroup parent ){
SpeechView sv;
If (convertView = null ){
Sv = new SpeechView (mContext, mTitles [position],
MDialogue [position]);
} Else {
Sv = (SpeechView) convertView;
Sv. setTitle (mTitles [position]);
Sv. setDialogue (mDialogue [position]);
}
Return sv;
}
Therefore, each item in the list is displayed as a SpeechView: