1.在layout下新建布局文件dialog_info.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_ling"
android:orientation="vertical" >
<TextView
android:id="@+id/tvIdiomInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
可以看到,最外层是ScrollView组建,当内容较多时会自动出现垂直滚动条。
2.修改StudyAnimalAcitivity,增加点击事件处理,代码如下所示:
public class StudyAnimalActivity extends Activity {
private List<Animal>animalList;
private AnimalDao animalDao;
private ListView lvAnimalList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal);
initAnimals();
lvAnimalList=(ListView)findViewById(R.id.lvAnimalList);
AnimalAdapter animalAdapter=new AnimalAdapter(this,
R.layout.animal_item, animalList);
lvAnimalList.setAdapter(animalAdapter);
lvAnimalList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?>adapterView,View view,
int position,long id) {
// TODO Auto-generated method stub
Animal animal=animalList.get(position);
String result =animal.getName()+
"\n[发音]"+animal.getPronounce()+
"\n[解释]:"+animal.getExplain()+
"\n[近义词]"+animal.getHomoionym()+
"\n[反义词]"+animal.getAnytonym()+
"\n[来源]"+animal.getDerivation()+
"\n[示例]"+animal.getExamples();
DialogUtil.showDialog(result,StudyAnimalActivity.this);
}
});
}
private void initAnimals() {
// TODO Auto-generated method stub
animalDao=AnimalDao.getInstance(this);
animalList=animalDao.getAllAnimals();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.study_animal, menu);
return true;
}
}
3.在util包下新建DialogUtil类,代码如下:
public class DialogUtil {
public static void showDialog(String result,Context context){
AlertDialog.Builder builder=new AlertDialog.Builder(context);
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.dialog_info, null);
builder.setView(view);
TextView tvIdiomInfo=(TextView) view.findViewById(R.id.tvIdiomInfo);
tvIdiomInfo.setText(result);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
}
4.可是运行时点击每个成语并不弹出对话框,解决办法是,修改AnimalAdapter类,代码如下:
public class AnimalAdapter extends ArrayAdapter<Animal> {
private int resourceId;
private Context context;
public AnimalAdapter(Context context,int resource,List<Animal>objects){
super(context,resource,objects);
this.context=context;
resourceId=resource;
}
public View getView(int position,View convertView,ViewGroup parent){
final Animal animal=getItem(position);//获取当前项的Animal实例
View view;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder=new ViewHolder();
viewHolder.tvName=(TextView) view.
findViewById(R.id.tvName);
viewHolder.btnSave=(ImageButton) view.
findViewById(R.id.btnSave);
viewHolder.btnSave.setFocusable(false);
viewHolder.btnSave.setFocusableInTouchMode(false);
viewHolder.btnSave.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Toast.makeText(context, "你要收藏"+animal.getName()+"吗",
Toast.LENGTH_SHORT).show();
}
});
view.setTag(viewHolder);//将ViewHolder存储在View中
}else{
view=convertView;
viewHolder=(ViewHolder)view.getTag();//重新获取ViewHolder
}
viewHolder.tvName.setText(animal.getName());
return view;
}
class ViewHolder{
TextView tvName;
ImageButton btnSave;
}
}
5.运行程序,结果如下: