大家在Android开发过程中用的比较频繁应该就是ListView、GridView吧,不过ListView、GridView不仅仅只是用来作为显示而已,有时候可能需要来点击列表之中某一项把当前这一项中所显示数据给获取出来,或是改变当前这一项的数据、界面显示,这都是比较常见的。
那么该如何做呢?(给该控件实现一个点击事件,直接在点击事件直接改变该控件界面显示、获取控件显示的数据,这样是行不通的,往往会出现数据错乱、显示不正常,原因是没有明确指定要处理的是哪一项) ,在Adapter的getView方法中,应该明确去指定操作数据而不是直接操作控件,再用数据来操作控件,数据才是最真实的体现。
下面用ListView作为示例实现上述的操作:
效果图:
在res的drawable-hdpi文件下添加需要用到的一些图片
1.首先在主activity_main界面上添加一个ListView作为显示
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.it.listviewdemo.MainActivity" > <ListView android:id="@+id/main_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:dividerHeight="0dp" android:divider="#f6f6f6" android:scrollbars="none" android:listSelector="@android:color/transparent" /> </RelativeLayout>
2.接着新建一个activity_qzone作为ListView每项要显示的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- 主显示布局 --> <LinearLayout android:id="@+id/qzone_main_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <!-- 第一部分布局 --> <LinearLayout android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- 头像 --> <ImageView android:id="@+id/qzone_head_iv" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@drawable/hao123" /> <LinearLayout android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 昵称 --> <TextView android:id="@+id/qzone_name_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="hao123" /> <!-- 时间 --> <TextView android:id="@+id/qzone_time_tv" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:text="今天07:00" /> </LinearLayout> </LinearLayout> <!-- 第二部分布局 --> <LinearLayout android:layout_margin="10dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_vertical" > <!-- 内容 --> <TextView android:id="@+id/qzone_contents_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:lineSpacingExtra="2dp" android:text="只是第一条显示数据而已." /> </LinearLayout> <!-- 第三部分布局 --> <LinearLayout android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_vertical" > <!-- 图片 --> <ImageView android:id="@+id/qzone_picture_iv" android:layout_width="200dp" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/hao123" /> </LinearLayout> </LinearLayout> <!-- 功能实现布局 --> <LinearLayout android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="right" > <!-- 赞功能 --> <ImageView android:id="@+id/qzone_like_iv" android:layout_width="30dp" android:layout_height="30dp" android:scaleType="centerCrop" android:src="@drawable/preview_like_icon_disable" /> </LinearLayout> <!-- 分割线 --> <View android:layout_width="match_parent" android:layout_height="3dp" android:background="#808080" /> </LinearLayout>
上面的布局:分为了两大块(主显示、功能实现)
1.主要是为了方便看
2.也是为了后面处理点击比较方便
3. 根据上面activity_qzone界面显示的(头像、昵称、时间、内容、图片、点赞), 创建对应的实体类
public class Qzone implements Serializable { private static final long serialVersionUID = -6074203749103731484L; private Integer head; //头像 private String name; //昵称 private String time; //时间 private String contents; //内容 private Integer picture; //图片 private boolean like; //赞功能 public Qzone() { } public Qzone(Integer head, String name, String time, String contents, Integer picture, boolean like) { super(); this.head = head; this.name = name; this.time = time; this.contents = contents; this.picture = picture; this.like = like; } public Integer getHead() { return head; } public void setHead(Integer head) { this.head = head; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public Integer getPicture() { return picture; } public void setPicture(Integer picture) { this.picture = picture; } public boolean isLike() { return like; } public void setLike(boolean like) { this.like = like; } }
4. Activity代码
MainActivity:
public class MainActivity extends Activity { private ListView lv_main; private List<Qzone> list; private QzoneAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initButton(); } private void initButton() { // TODO Auto-generated method stub lv_main = (ListView) findViewById(R.id.main_lv); lv_main.setAdapter(mAdapter); } /** * 本地数据(测试) */ private void initData() { // TODO Auto-generated method stub list = new ArrayList<Qzone>(); list.add(new Qzone(R.drawable.hao123, "hao123", "今天07:00", "只是第一条显示数据而已.", R.drawable.hao123, false)); list.add(new Qzone(R.drawable.baidu, "baidu", "今天08:00", "只是第二条显示数据而已.", R.drawable.baidu, false)); list.add(new Qzone(R.drawable.wy163, "wy163", "今天09:00", "只是第三条显示数据而已.", R.drawable.wy163, false)); list.add(new Qzone(R.drawable.weibo, "weibo", "今天10:00", "只是第四条显示数据而已.", R.drawable.weibo, false)); list.add(new Qzone(R.drawable.douban, "douban", "今天11:00", "只是第五条显示数据而已.", R.drawable.douban, false)); list.add(new Qzone(R.drawable.taobao, "taobao", "今天12:00", "只是第六条显示数据而已.", R.drawable.taobao, false)); list.add(new Qzone(R.drawable.youku, "youku", "今天13:00", "只是第七条显示数据而已.", R.drawable.youku, false)); list.add(new Qzone(R.drawable.qzone, "qzone", "今天14:00", "只是第八条显示数据而已.", R.drawable.qzone, false)); list.add(new Qzone(R.drawable.renren, "renren", "今天15:00", "只是第九条显示数据而已.", R.drawable.renren, false)); list.add(new Qzone(R.drawable.sina, "sina", "今天16:00", "只是第十条显示数据而已.", R.drawable.sina, false)); mAdapter = new QzoneAdapter(this, list); } }
MainActivity显示的是activity_main.xml
QzoneActivity:
public class QzoneActivity extends Activity { private Qzone qzone; private ImageView iv_head; private TextView tv_name; private TextView tv_time; private TextView tv_contents; private ImageView iv_picture; private ImageView iv_like; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_qzone); initButton(); initData(); } private void initButton() { // TODO Auto-generated method stub iv_head = (ImageView) findViewById(R.id.qzone_head_iv); tv_name = (TextView) findViewById(R.id.qzone_name_tv); tv_time = (TextView) findViewById(R.id.qzone_time_tv); tv_contents = (TextView) findViewById(R.id.qzone_contents_tv); iv_picture = (ImageView) findViewById(R.id.qzone_picture_iv); iv_like = (ImageView) findViewById(R.id.qzone_like_iv); } /** * 获取到传递过来的数据,显示到相对应的控件上 */ private void initData() { // TODO Auto-generated method stub qzone = (Qzone) getIntent().getSerializableExtra("Qzone"); if(qzone != null){ iv_head.setImageResource(qzone.getHead()); tv_name.setText(qzone.getName()); tv_time.setText(qzone.getTime()); tv_contents.setText(qzone.getContents()); iv_picture.setImageResource(qzone.getPicture()); iv_like.setImageResource(qzone.isLike() ? R.drawable.like_icon : R.drawable.like_icon_disable); } } }
QzoneActivity显示的是activity_qzone.xml
5. Adapter适配器
public class QzoneAdapter extends BaseAdapter implements OnClickListener { private Activity act; private LayoutInflater inflater; private ViewHolder holder; private List<Qzone> list; public QzoneAdapter(Activity act, List<Qzone> list) { this.act = act; this.inflater = act.getLayoutInflater(); this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView == null){ convertView = inflater.inflate(R.layout.activity_qzone , null); holder = new ViewHolder(); holder.ll_main = (LinearLayout) convertView.findViewById(R.id.qzone_main_ll); holder.iv_head = (ImageView) convertView.findViewById(R.id.qzone_head_iv); holder.tv_name = (TextView) convertView.findViewById(R.id.qzone_name_tv); holder.tv_time = (TextView) convertView.findViewById(R.id.qzone_time_tv); holder.tv_contents = (TextView) convertView.findViewById(R.id.qzone_contents_tv); holder.iv_picture = (ImageView) convertView.findViewById(R.id.qzone_picture_iv); holder.iv_like = (ImageView) convertView.findViewById(R.id.qzone_like_iv); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } Qzone qzone = list.get(position); holder.iv_head.setImageResource(qzone.getHead()); holder.tv_name.setText(qzone.getName()); holder.tv_time.setText(qzone.getTime()); holder.tv_contents.setText(qzone.getContents()); holder.iv_picture.setImageResource(qzone.getPicture()); // 赞图标显示 holder.iv_like.setImageResource(qzone.isLike() ? R.drawable.like_icon : R.drawable.like_icon_disable); // 主显示布局设置一个标示, 实现点击 holder.ll_main.setTag(position); holder.ll_main.setOnClickListener(this); // 赞功能控件设置一个标示, 实现点击 holder.iv_like.setTag(position); holder.iv_like.setOnClickListener(this); return convertView; } private class ViewHolder{ LinearLayout ll_main; // 主显示布局 ImageView iv_head; // 头像 TextView tv_name; // 昵称 TextView tv_time; // 时间 TextView tv_contents; // 内容 ImageView iv_picture; // 图片 ImageView iv_like; // 赞图标 } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // 主显示布局点击事件处理(获取之前设置的标示) case R.id.qzone_main_ll: IntentMethod((Integer) v.getTag()); break; // 赞功能控件点击事件处理(获取之前设置的标示) case R.id.qzone_like_iv: LikeMethod((Integer) v.getTag()); break; default: break; } } /** * 跳转功能实现 * @param position */ private void IntentMethod(int position) { Toast.makeText(act, "进入"+list.get(position).getName()+"详细界面", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(act, QzoneActivity.class); intent.putExtra("Qzone", list.get(position)); act.startActivity(intent); } /** * 赞功能实现 * @param position */ private void LikeMethod(int position) { list.get(position).setLike(!list.get(position).isLike()); notifyDataSetChanged(); //刷新列表 Qzone qzone = list.get(position); Toast.makeText(act, qzone.isLike() ? "点赞"+qzone.getName() : qzone.getName()+"取消赞", Toast.LENGTH_SHORT).show(); } }在显示的赞图标的时候,利用数据作为条件使用三目运算符去判断要显示的图标
1. 在getView方法要执行某种操作,一般给控件设置一个Tag(标示) ,赋值position
控件.setTag(position);
2. position就是getView方法的参数,其实这个参数对应着ListView列表的Item(项)位置
public View getView(int position, View convertView, ViewGroup parent)
3.为控件设置一个点击事件,然后就可以在点击事件方法获取到刚才设置的Tag(项)位置
int position = (Integer) v.getTag();