实例介绍
【实例截图】
package edu.njupt.zhb.xlistviewtest; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import me.maxwin.view.XListView; import me.maxwin.view.XListView.IXListViewListener; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.Click; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.Fullscreen; import org.androidannotations.annotations.ItemClick; import org.androidannotations.annotations.UiThread; import org.androidannotations.annotations.ViewById; import android.app.Activity; import android.util.Log; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; @Fullscreen @EActivity(R.layout.activity_xlist) public class XListActivity extends Activity implements IXListViewListener { XListView listView; // ListView的设配器 private XBaseAdapter xBaseAdapter; @ViewById Button btnSendComment; @ViewById EditText etComment; @AfterViews void afterViewInitList() { listView = (XListView) findViewById(R.id.listView); xBaseAdapter = new XBaseAdapter(this, R.layout.listview_item,this); listView.setAdapter(xBaseAdapter); listView.setXListViewListener(this);// 添加XListView的上拉和下拉刷新监听器 listView.setPullLoadEnable(true); listView.setPullRefreshEnable(true); initXListData(); } void initXListData() { Model model = new Model(); model.setImgHead(R.drawable.head); model.setName("张三"); model.setContent("五一过的太无聊了。还是好好写代码吧。给大家推荐一个群,Android开发联盟QQ群:272209595"); model.setType(FinalVar.MSG_TEXT); model.setAgree(false); model.setPhonemodel("Nexus 5"); model.setAddress("南京市 玄武湖公园"); model.setDate(new SimpleDateFormat().format(new Date()).toString()); xBaseAdapter.addModel(model); Model model2 = new Model(); model2.setImgHead(R.drawable.qq_contact_list_friend_entry_icon); model2.setName("李四"); model2.setContent("哈哈,五一很欢乐。。。有图有真相,不信你看,嘿嘿"); model2.setType(FinalVar.MSG_IMAGE); model2.setPhonemodel("iPhone 5s"); model2.setAddress("北京市海淀区"); model2.setDate(new SimpleDateFormat().format(new Date()).toString()); List<String> imageUrls = new ArrayList<String>(); imageUrls .add("http://www.5wants.cc/WEB/File/U3325P704T93D1661F3923DT20090612155225.jpg"); imageUrls .add("http://www.ineiyi.com/uploads/allimg/1312/77-131213100200.jpg"); model2.setImageUrls(imageUrls); model.setDate(new SimpleDateFormat().format(new Date()).toString()); xBaseAdapter.addModel(model2); xBaseAdapter.notifyDataSetChanged(); } @ItemClick(R.id.listView) public void onItemClick(int position) { // TODO Auto-generated method stub Log.d("ItemClick", "pos=" position); position = position - 1; if (null != xBaseAdapter.getModel(position)) { Toast.makeText(this, "click Item...", Toast.LENGTH_SHORT).show(); } } @Click(R.id.btnSendComment) void btnSendComment(){ String comment = etComment.getEditableText().toString(); Toast.makeText(this, comment, Toast.LENGTH_SHORT).show(); } @Override public void onRefresh() { Log.d("xlistview", "onrefresh"); refreshListViewInBackground(); } @UiThread void refreshListViewInBackground() {// 模拟刷新数据 Model model = new Model(); model.setImgHead(R.drawable.head); model.setName("老男孩"); model.setContent("为了梦想而努力。。。"); model.setType(FinalVar.MSG_TEXT); model.setAgree(false); model.setPhonemodel("Nexus 5"); model.setAddress("南京市 新模范马路"); model.setDate(new SimpleDateFormat().format(new Date()).toString()); xBaseAdapter.addModel(model,true); xBaseAdapter.notifyDataSetChanged(); onLoad(); } @Override public void onLoadMore() { Log.d("ItemClick", "onloadmore"); loadMoreInBackground(); } @UiThread void loadMoreInBackground() { Model model = new Model(); model.setImgHead(R.drawable.head); model.setName("高富帅"); model.setContent("无聊中...且行且珍惜"); model.setType(FinalVar.MSG_TEXT); model.setAgree(false); model.setPhonemodel("Nexus 5"); model.setAddress("南京市 高铁南站"); model.setDate(new SimpleDateFormat().format(new Date()).toString()); xBaseAdapter.addModel(model); xBaseAdapter.notifyDataSetChanged(); listView.setSelection(xBaseAdapter.getCount() - 1);// 将光标移动到加载的交界处 onLoad(); } private void onLoad() { listView.stopRefresh(); listView.stopLoadMore(); listView.setRefreshTime("刚刚"); } }
package edu.njupt.zhb.xlistviewtest; import java.util.ArrayList; import java.util.List; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graphics.BitmapFactory; import android.os.Build; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-4-27 Nanjing,njupt,China */ @TargetApi(Build.VERSION_CODES.GINGERBREAD) public class XBaseAdapter extends BaseAdapter { private Context context; private Activity activity; private List<Model> listViewData; private int layoutResId;// ListView每个Item的布局文件 public XBaseAdapter(Context context, int layoutResId, Activity activity) { this.context = context; this.layoutResId = layoutResId; listViewData = new ArrayList<Model>(); this.activity = activity; } @Override public View getView(int position, View convertView, ViewGroup parent) { Model model = listViewData.get(position); ViewItemHolder viewItemHolder = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(layoutResId, null); viewItemHolder = new ViewItemHolder(); viewItemHolder.imgHead = (ImageView) convertView .findViewById(R.id.imgHead); viewItemHolder.tvName = (TextView) convertView .findViewById(R.id.tvName); viewItemHolder.tvDate = (TextView) convertView .findViewById(R.id.tvDate); viewItemHolder.tvContent = (TextView) convertView .findViewById(R.id.tvContent); viewItemHolder.ivPhoto = (ImageView) convertView .findViewById(R.id.ivPhoto); viewItemHolder.ivAddress = (ImageView) convertView .findViewById(R.id.ivAddress); viewItemHolder.tvAddress = (TextView) convertView .findViewById(R.id.tvAddress); viewItemHolder.tvPhonemodel = (TextView) convertView .findViewById(R.id.tvPhonemodel); viewItemHolder.ivAgree = (ImageView) convertView .findViewById(R.id.ivAgree); viewItemHolder.ivComment = (ImageView) convertView .findViewById(R.id.ivComment); viewItemHolder.tvComment = (TextView) convertView .findViewById(R.id.tvComment); viewItemHolder.ivAgreeShow = (ImageView) convertView .findViewById(R.id.ivAgreeShow); viewItemHolder.tvAgreeShow = (TextView) convertView .findViewById(R.id.tvAgreeShow); viewItemHolder.btnComment = (Button) convertView .findViewById(R.id.btnComment); viewItemHolder.tvComments = (TextView) convertView .findViewById(R.id.tvComments); convertView.setTag(viewItemHolder); } else { viewItemHolder = (ViewItemHolder) convertView.getTag(); } viewItemHolder.imgHead.setImageBitmap(BitmapFactory.decodeResource( context.getResources(), model.getImgHead())); viewItemHolder.tvName.setText(model.getName()); viewItemHolder.tvDate.setText(model.getDate()); viewItemHolder.tvContent.setText(model.getContent()); if (model.getType() == FinalVar.MSG_IMAGE) {// 图片资源 viewItemHolder.ivPhoto.setImageResource(R.drawable.pic_screen); viewItemHolder.ivPhoto.setVisibility(View.VISIBLE); } else { viewItemHolder.ivPhoto.setVisibility(View.GONE); } if (!model.getAddress().isEmpty()) { viewItemHolder.ivAddress.setVisibility(View.VISIBLE); viewItemHolder.tvAddress.setVisibility(View.VISIBLE); viewItemHolder.tvAddress.setText(model.getAddress()); } else { viewItemHolder.ivAddress.setVisibility(View.GONE); viewItemHolder.tvAddress.setVisibility(View.GONE); } viewItemHolder.tvPhonemodel.setText(model.getPhonemodel()); viewItemHolder.ivAgree .setOnClickListener(new ListViewButtonOnClickListener(position)); if (model.isAgree()) { viewItemHolder.ivAgree .setImageResource(R.drawable.qzone_picviewer_bottom_praise_icon); } else { viewItemHolder.ivAgree .setImageResource(R.drawable.qzone_picviewer_bottom_unpraise_icon); } viewItemHolder.ivAgree.setFocusable(false); if (null != model.getAgreeShow() && model.getAgreeShow().size() > 0) { viewItemHolder.ivAgreeShow.setVisibility(View.VISIBLE); viewItemHolder.tvAgreeShow.setVisibility(View.VISIBLE); viewItemHolder.tvAgreeShow.setText(model.getAgreeShow().toString() "觉得很赞!"); } else { viewItemHolder.ivAgreeShow.setVisibility(View.GONE); viewItemHolder.tvAgreeShow.setVisibility(View.GONE); } viewItemHolder.ivComment .setOnClickListener(new ListViewButtonOnClickListener(position)); viewItemHolder.ivComment.setFocusable(false); viewItemHolder.tvComment .setOnClickListener(new ListViewButtonOnClickListener(position)); viewItemHolder.btnComment .setOnClickListener(new ListViewButtonOnClickListener(position)); viewItemHolder.btnComment.setFocusable(false); if (null != model.getComments() && model.getComments().size() > 0) { viewItemHolder.tvComments.setVisibility(View.VISIBLE); String string = ""; for (String comment : model.getComments()) { string = comment "\n"; } viewItemHolder.tvComments.setText(string); } else { viewItemHolder.tvComments.setVisibility(View.GONE); } return convertView; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return listViewData.get(position); } @Override public int getCount() { // TODO Auto-generated method stub if (null == listViewData) { return 0; } return listViewData.size(); } /** * 添加一条记录 * * @param model */ public void addModel(Model model) { listViewData.add(model); } /** * 添加一条记录 * * @param model * @param insertHead * true:插入在头部 */ public void addModel(Model model, boolean insertHead) { if (insertHead) { listViewData.add(0, model); } else { listViewData.add(model); } } /** * 获取一条记录 * * @param i * @return */ public Model getModel(int i) { if (i < 0 || i > listViewData.size() - 1) { return null; } return listViewData.get(i); } /** * 清除所有数据 */ public void clear() { listViewData.clear(); } class ViewItemHolder { ImageView imgHead; TextView tvName; TextView tvDate; TextView tvContent; ImageView ivPhoto; ImageView ivAddress; TextView tvAddress; ImageView ivAgree; TextView tvPhonemodel; ImageView ivComment; TextView tvComment; ImageView ivAgreeShow; TextView tvAgreeShow; Button btnComment; TextView tvComments; } class ListViewButtonOnClickListener implements OnClickListener { private int position;// 记录ListView中Button所在的Item的位置 public ListViewButtonOnClickListener(int position) { this.position = position; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.ivAgree: ImageView ivAgree = (ImageView) v; Model model = listViewData.get(position); List<String> agreeShow = model.getAgreeShow(); if (null == agreeShow || agreeShow.size() <= 0) { agreeShow = new ArrayList<String>(); } if (model.isAgree()) { agreeShow.remove("我"); ivAgree.setImageResource(R.drawable.qzone_picviewer_bottom_unpraise_icon); } else { agreeShow.add("我"); ivAgree.setImageResource(R.drawable.qzone_picviewer_bottom_praise_icon); } model.setAgree(!model.isAgree()); model.setAgreeShow(agreeShow); listViewData.set(position, model); notifyDataSetChanged(); Toast.makeText(context, "你点了赞", Toast.LENGTH_SHORT).show(); break; case R.id.ivComment: case R.id.tvComment: case R.id.btnComment: InputMethodManager imm = (InputMethodManager) v.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED); Model model1 = listViewData.get(position); String nikename = model1.getName(); activity.findViewById(R.id.etComment).setVisibility( View.VISIBLE); activity.findViewById(R.id.btnSendComment).setVisibility( View.VISIBLE); ((EditText) activity.findViewById(R.id.etComment)).setHint("@" nikename); activity.findViewById(R.id.etComment).setFocusable(true); activity.findViewById(R.id.btnSendComment).setOnClickListener( new ListViewButtonOnClickListener(position)); break; case R.id.btnSendComment: Model mdl = listViewData.get(position); List<String> commentsList = mdl.getComments(); String commentString = ((EditText) activity .findViewById(R.id.etComment)).getEditableText() .toString(); if (null == commentsList || commentsList.size() <= 0) { commentsList = new ArrayList<String>(); } commentsList.add(commentString); mdl.setComments(commentsList); listViewData.set(position, mdl); notifyDataSetChanged(); ((EditText) activity.findViewById(R.id.etComment)).setText(""); activity.findViewById(R.id.etComment).setVisibility(View.GONE); activity.findViewById(R.id.btnSendComment).setVisibility( View.GONE); InputMethodManager imm2 = (InputMethodManager) v.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE); imm2.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); break; default: break; } } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:id="@ id/rl_input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <Button android:id="@ id/btnSendComment" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/btn_shape" android:focusable="false" android:text="发送" android:visibility="gone" /> <EditText android:id="@ id/etComment" android:layout_width="fill_parent" android:layout_height="32dp" android:layout_alignParentLeft="true" android:layout_toLeftOf="@id/btnSendComment" android:background="@drawable/btn_shape" android:hint="输入评论" android:visibility="gone" /> </RelativeLayout> <me.maxwin.view.XListView android:id="@ id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/rl_input" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@ id/contacts_items" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="vertical" > <View android:id="@ id/topLine" android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ffcccccc" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:orientation="horizontal" > <ImageView android:id="@ id/imgHead" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" > <TextView android:id="@ id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:singleLine="true" android:text="Tom" android:textSize="16.0sp" /> <TextView android:id="@ id/tvDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="0.2dip" android:layout_marginTop="0dip" android:ellipsize="end" android:singleLine="true" android:text="13:32" android:textColor="#ffcccccc" android:textSize="12sp" /> </RelativeLayout> <TextView android:id="@ id/tvContent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5dp" android:paddingTop="5dp" android:text="Hello world" android:textSize="14sp" > </TextView> <ImageView android:id="@ id/ivPhoto" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="center" android:visibility="gone" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <ImageView android:id="@ id/ivAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="2dp" android:src="@drawable/qzone_address_icon" android:visibility="gone" /> <TextView android:id="@ id/tvAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/ivAddress" android:paddingLeft="5dp" android:textSize="10sp" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <ImageView android:id="@ id/ivPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:gravity="center" android:src="@drawable/status_phone" > </ImageView> <TextView android:id="@ id/tvPhonemodel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/ivPhone" android:paddingLeft="3dp" android:text="Nexus 5" android:textSize="10sp" /> <TextView android:id="@ id/tvComment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingLeft="2dp" android:text="璇勮" android:textSize="10sp" /> <ImageView android:id="@ id/ivComment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/tvComment" android:gravity="center" android:paddingLeft="20dp" android:src="@drawable/qzone_picviewer_bottom_comment_icon" > </ImageView> <TextView android:id="@ id/tvAgree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/ivComment" android:text="璧? android:textSize="10sp" /> <ImageView android:id="@ id/ivAgree" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/tvAgree" android:gravity="center" android:src="@drawable/qzone_picviewer_bottom_unpraise_icon" > </ImageView> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <ImageView android:id="@ id/ivAgreeShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:contentDescription="@string/app_name" android:paddingLeft="2dp" android:src="@drawable/qzone_picviewer_bottom_praise_icon" android:visibility="gone" /> <TextView android:id="@ id/tvAgreeShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/ivAgreeShow" android:paddingLeft="5dp" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <TextView android:id="@ id/tvComments" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:paddingLeft="5dp" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <Button android:id="@ id/btnSendComment" android:layout_width="wrap_content" android:layout_height="15dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#00000000" android:focusable="false" android:text="鍙戦€? android:textSize="10sp" android:visibility="gone" /> <Button android:id="@ id/btnComment" android:layout_width="fill_parent" android:layout_height="15dp" android:layout_alignParentLeft="true" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_toLeftOf="@id/btnSendComment" android:background="@drawable/btn_shape" android:hint="Please input your comment..." android:textSize="10sp" /> </RelativeLayout> </LinearLayout> </LinearLayout> <View android:id="@ id/lastLine" android:layout_width="fill_parent" android:layout_height="1dp" android:background="#ff474745" android:visibility="gone" /> </LinearLayout>
标签: QQ
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论