第一步,首先确定在dialog中的布局文件
第二步:在adapter的构造函数中编写view,和dialog,因为这样面对所有类型的文件,都可以直接用构造函数创建的对象,提高性能
第三部:创建dialog中的布局文件的各个控件的实例对象
第四部:在控件的点击事件中,显示dialog,并且实现相应的操作,比如在dialog上面显示图片等等
下面为adapter中的代码:
public HomeMsgAdapter(Context context,List<HomeMsgBean> list){ this.list=list; mInflater = LayoutInflater.from(context); // //将以前的实例化对象,放到这里来实现,之后对各个控件的赋值实例化都不是现在getView()函数中 // View view = mInflater.inflate(R.layout.msg_write_layout, null); /** * 在Adapter中实现点击图片,可以实现放大缩小图片的功能,就可以将对象实例化放在构造函数中,代码如下 */ View view=mInflater.inflate(R.layout.weibo_img_dlg,null); dialog=new Dialog(context,R.style.simple_dialog); dialog.setContentView(view); /** * 实例化dialog中控件对象 */ imgView = (ImageView) view.findViewById(R.id.img_weibo_img); pb = (ProgressBar) view.findViewById(R.id.progress_wbimg_load); btnBig = (Button) view.findViewById(R.id.btn_big); }
然后在imageview中实现点击事件:
holder.commont_head.setImageResource(list.get(position).getCommont());
holder.commont_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imgView.setImageResource(list.get(position).getCommont()); dialog.show(); } });
整个页面的源代码:
package Adapter; import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import java.util.List; import Bean.HomeMsgBean; import app.coolweather.com.weibo.R; import imgCache.SimpleImageLoader; /**用于 * Created by Administrator on 2016/9/4. *十八章的重点:将超链接转换成不是超链接的数据,并且解析,得到你想要的数据 * holder.txt_wb_item_from.setMovementMethod(LinkMovementMethod.getInstance()); 这句话表示能不能实现listview中控件的点击 * holder.txt_wb_item_from.setText("来着:"+Html.fromHtml(s.getSource())); */ public class HomeMsgAdapter extends BaseAdapter { private List<HomeMsgBean> list; //将数据放入Adapter中 private LayoutInflater mInflater; private Dialog dialog; public ImageView imgView; public ProgressBar pb; public Button btnBig; public HomeMsgAdapter(Context context,List<HomeMsgBean> list){ this.list=list; mInflater = LayoutInflater.from(context); // //将以前的实例化对象,放到这里来实现,之后对各个控件的赋值实例化都不是现在getView()函数中 // View view = mInflater.inflate(R.layout.msg_write_layout, null); /** * 在Adapter中实现点击图片,可以实现放大缩小图片的功能,就可以将对象实例化放在构造函数中,代码如下 */ View view=mInflater.inflate(R.layout.weibo_img_dlg,null); dialog=new Dialog(context,R.style.simple_dialog); dialog.setContentView(view); /** * 实例化dialog中控件对象 */ imgView = (ImageView) view.findViewById(R.id.img_weibo_img); pb = (ProgressBar) view.findViewById(R.id.progress_wbimg_load); btnBig = (Button) view.findViewById(R.id.btn_big); } @Override public int getCount() { return list==null?0:list.size(); } @Override public Object getItem(int position) { return list==null?null:list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // View view = convertView; // if(view==null){ // Holder holder=new Holder(); // view = LayoutInflater.from(context).inflate(R.layout.msg_write_layout, null);//加载布局文件,该布局文件为listview中的文件,返回v对象,即自己的模板文件 // //创建对象 // holder.user_content_textView= (TextView) view.findViewById(R.id.user_content); // holder.user_head= (ImageView) view.findViewById(R.id.user_head); // holder.commont_head=(ImageView)view.findViewById(R.id.user_head_one); // holder.user_time=(TextView)view.findViewById(R.id.time_put); // holder.commont_child=(TextView)view.findViewById(R.id.user_content_one); // holder.commont_head_two=(ImageView)view.findViewById(R.id.user_head_two); // //给各个对象赋值 // holder.user_content_textView.setText(list.get(position).getName()); // holder.user_head.setImageResource(list.get(position).getHead()); //setImageResource()中间填写R.drawable.picture // holder.commont_head.setImageResource(list.get(position).getCommont()); // holder.user_time.setText(list.get(position).getContent()); // holder.commont_child.setText(list.get(position).getContent_child()); // holder.commont_head_two.setImageResource(list.get(position).getCommont_head()); //setImageResource()中间填写R.drawable.picture // // } return createViewFromResource(position,convertView); } private View createViewFromResource(int position, View convertView) { View v; if (convertView == null) { v = mInflater.inflate(R.layout.msg_write_layout, null); } else { v = convertView; } bindView(position, v); return v; } private void bindView(final int position,View view){ Holder holder=new Holder(); holder.user_content_textView= (TextView) view.findViewById(R.id.user_content); holder.user_head= (ImageView) view.findViewById(R.id.user_head); holder.commont_head=(ImageView)view.findViewById(R.id.user_head_one); holder.user_time=(TextView)view.findViewById(R.id.time_put); holder.commont_child=(TextView)view.findViewById(R.id.user_content_one); holder.commont_head_two=(ImageView)view.findViewById(R.id.user_head_two); //给各个对象赋值 holder.user_content_textView.setText(list.get(position).getName()); // holder.user_head.setImageResource(list.get(position).getHead()); //setImageResource()中间填写R.drawable.picture holder.commont_head.setImageResource(list.get(position).getCommont()); holder.user_time.setText(list.get(position).getContent()); holder.commont_child.setText(list.get(position).getContent_child()); holder.commont_head_two.setImageResource(list.get(position).getCommont_head()); //setImageResource()中间填写R.drawable.picture SimpleImageLoader.showImg(holder.user_head,"http://image6.huangye88.com/2013/07/26/370d6cd0c79c4269.jpg"); holder.commont_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imgView.setImageResource(list.get(position).getCommont()); dialog.show(); } }); } //静态类用于声明各个控件的对象,实现性能的优化 private static class Holder{ TextView user_content_textView; ImageView user_head; ImageView commont_head; TextView user_time; TextView commont_child; ImageView commont_head_two; } }