android 列表+详情(可刷新,以EmailList和EmailDetail为例)

来源:互联网 发布:mac office 2016 很卡 编辑:程序博客网 时间:2024/06/01 09:59

1 EmailList

package com.haiyisoft.teamassistant.activity;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;import android.content.Intent;import android.os.AsyncTask;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnKeyListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import com.haiyisoft.mobile.android.adapter.ImageViewBinder;import com.haiyisoft.mobile.android.adapter.TextViewBinder;import com.haiyisoft.mobile.android.adapter.TypedAdapter;import com.haiyisoft.mobile.android.adapter.impl.ClassAttachmentImpl;import com.haiyisoft.mobile.common.Email;import com.haiyisoft.teamassistant.R;import com.haiyisoft.teamassistant.swipeback.BaseActivity;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;/** * 邮件详细 *  * @author 姜修武 *  *         <p> *         Modification History: *         </p> *         <p> *         Date Author Description *         </p> *         <p> *         -------------------------------------------------------------- *         </p> *         <p> *         2015-05-20 姜修武 new *         </p> *         <p> *         </p> */@SuppressWarnings({ "rawtypes", "unchecked" })public class EmailListActivity extends BaseActivity {private PullToRefreshListView emailListView;private TypedAdapter emailAdapter;private List<Email> emails;ImageButton searchButton;View searchbarEmailLayout;private EditText searchEdit;private Button searchBtn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_email_list);initView();}private void initView() {setActivityHeaderTitle(R.string.email_list);getHeaderLeftButton().setImageResource(R.drawable.icon_back);getHeaderRightButton().setBackgroundResource(0);getHeaderRightButton().setVisibility(View.VISIBLE);getHeaderRightButton().setImageDrawable(getResources().getDrawable(R.drawable.work_mail_search));searchButton = (ImageButton) findViewById(R.id.headerRightButton);searchbarEmailLayout = (View) findViewById(R.id.searchbar_email);searchButton.setOnClickListener(new OnClickListener() {// 实现事件处理方法@Overridepublic void onClick(View v) {if (searchbarEmailLayout.getVisibility() == View.GONE) {searchbarEmailLayout.setVisibility(View.VISIBLE);} else {searchbarEmailLayout.setVisibility(View.GONE);}}});emailListView = (PullToRefreshListView) findViewById(R.id.lv_email_list);emailListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {new GetDataTask().execute();}public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {}});emailListView.setMode(Mode.PULL_FROM_START);initAdapter();ListView actualListView = emailListView.getRefreshableView();actualListView.setAdapter(emailAdapter);actualListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {if (emails != null && !emails.isEmpty()&& position - 1 < emails.size()) {Intent intent = new Intent(EmailListActivity.this,EmailDetailActivity.class);Email n = emails.get(position - 1);intent.putExtra("email",new String[] { n.getTitle(), n.getContent(),n.getDate(), n.getAddressor(),n.getRecipient(), n.getAccessoryNum(),n.getCopy() });startActivity(intent);}}});// 绑定搜索框searchEdit = (EditText) findViewById(R.id.edt_searchbar);searchBtn = (Button) findViewById(R.id.ib_searchbtn);addSearchTextWatcher();searchEdit.setOnKeyListener(onKey);}OnKeyListener onKey = new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_ENTER) {String keyword = searchEdit.getText().toString();List<Email> list = new ArrayList<Email>();if ("".equals(keyword)) {list = getEmailList();} else {for (Email ct : emails) {if (ct.getContent().indexOf(keyword.toString()) != -1|| ct.getAddressor().indexOf(keyword.toString()) != -1) {list.add(ct);}}}emails.clear();emails.addAll(list);emailAdapter.notifyDataSetChanged();}return false;}};/** * 添加搜索框监听 */private void addSearchTextWatcher() {searchBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String keyword = searchEdit.getText().toString();List<Email> list = new ArrayList<Email>();if ("".equals(keyword)) {list = getEmailList();} else {for (Email ct : emails) {if (ct.getContent().indexOf(keyword.toString()) != -1|| ct.getAddressor().indexOf(keyword.toString()) != -1) {list.add(ct);}}}emails.clear();emails.addAll(list);emailAdapter.notifyDataSetChanged();}});}/** * 初始化适配器 */private void initAdapter() {ClassAttachmentImpl<Email> c = new ClassAttachmentImpl<Email>();c.addBinderMapItem(R.id.iv_email_item_icon, "showNew",new ImageViewShowBinder());c.addBinderMapItem(R.id.tv_email_item_addressor, "addressor",new TextViewBinder());c.addBinderMapItem(R.id.tv_email_item_content, "content",new TextViewBinder());c.addBinderMapItem(R.id.tv_email_item_date, "date",new TextViewBinder());emails = getEmailList();emailAdapter = new TypedAdapter(this, emails,R.layout.adapter_email_list_item);emailAdapter.setBinder(c);}/** * 模拟数据 *  * @return */public List<Email> getEmailList() {List<Email> list = new ArrayList<Email>();list.add(new Email(1,"欢迎大家加入班组通验收计划","欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号。","今天", "张飞", "王丽丽", "王丽丽,王鹏,谢丛林", "1", true));list.add(new Email(2, "欢迎大家加入班组通验收计划","欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号。", "昨天", "王飞","王丽丽", "王丽丽,王鹏,谢丛林", "1", true));list.add(new Email(3, "欢迎大家加入班组通验收计划","欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号。", "前天", "孙飞","王丽丽", "王丽丽,王鹏,谢丛林", "1", true));list.add(new Email(4, "欢迎大家加入班组通验收计划","欢迎大家加入班组通,请一个小时内马上激活账号,欢迎大家加入班组通,请一个小时内马上激活账号。", "2015-05-18","李飞", "王丽丽", "王丽丽,王鹏,谢丛林", "1", true));list.add(new Email(5, "运动会","公司于4月举行的运动会上,运动健儿们的飒爽英姿及观众的满腔热情,给我们留下了难忘的回忆。", "2015-05-14","姜修武", "赵同元", "王斌,王鹏,张学龙", "3", false));return list;}/** * 异步查询数据 *  */private class GetDataTask extends AsyncTask<Void, Void, Void> {@Overrideprotected Void doInBackground(Void... params) {emails.removeAll(emails);emails.addAll(getEmailList());try {Thread.sleep(2000);} catch (InterruptedException e) {}return null;}@Overrideprotected void onPostExecute(Void result) {emailAdapter.notifyDataSetChanged();emailListView.onRefreshComplete();}}private class ImageViewShowBinder extends ImageViewBinder {@Overridepublic void setViewValue(View view, Object bean, String property) {ImageView imageView = (ImageView) view;Boolean value = null;try {value = (Boolean) getValue(bean, property);} catch (SecurityException e) {throw new IllegalAccessError(e.getMessage());} catch (IllegalArgumentException e) {throw new IllegalAccessError(e.getMessage());} catch (NoSuchMethodException e) {throw new IllegalAccessError(e.getMessage());} catch (IllegalAccessException e) {throw new IllegalAccessError(e.getMessage());} catch (InvocationTargetException e) {throw new IllegalAccessError(e.getMessage());}if (value) {imageView.setVisibility(View.VISIBLE);} else {imageView.setVisibility(View.INVISIBLE);}}}}
EmailDetai

package com.haiyisoft.teamassistant.activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.haiyisoft.teamassistant.R;import com.haiyisoft.teamassistant.swipeback.BaseActivity;/** * 邮件详细 * * @author 姜修武 *  * <p>Modification History:</p> * <p>Date Author Description</p> * <p>--------------------------------------------------------------</p> * <p>2015-05-21              姜修武                        new</p> * <p></p> */public class EmailDetailActivity extends BaseActivity {private TextView title;private TextView date;private TextView content;private TextView addressor;private TextView recipient;private TextView copy;private TextView accessoryNum;private String[] email;Button hidden;Button show;View shoujianrenLayout; View chaosongLayout; View hengxian; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_email_detail);email = getIntent().getStringArrayExtra("email");hidden = (Button) findViewById(R.id.hidden);show = (Button) findViewById(R.id.show);shoujianrenLayout=(View)findViewById(R.id.shoujianren);chaosongLayout=(View)findViewById(R.id.chaosong);hengxian=(View)findViewById(R.id.hengxian);hidden.setVisibility(Button.GONE);shoujianrenLayout.setVisibility(View.GONE);chaosongLayout.setVisibility(View.GONE);hengxian.setVisibility(View.GONE);hidden.setOnClickListener(new OnClickListener(){// 实现事件处理方法@Overridepublic void onClick(View v){shoujianrenLayout.setVisibility(View.GONE);chaosongLayout.setVisibility(View.GONE);hengxian.setVisibility(View.GONE);hidden.setVisibility(Button.GONE);show.setVisibility(Button.VISIBLE);}});show.setOnClickListener(new OnClickListener(){// 实现事件处理方法@Overridepublic void onClick(View v){shoujianrenLayout.setVisibility(View.VISIBLE);chaosongLayout.setVisibility(View.VISIBLE);hengxian.setVisibility(View.VISIBLE);show.setVisibility(Button.GONE);hidden.setVisibility(Button.VISIBLE);}});initView();}/** * 初始化视图 */private void initView() {getHeaderTitleView().setText("邮件");getHeaderLeftButton().setImageResource(R.drawable.icon_back);title = (TextView) findViewById(R.id.tv_email_detail_title);date = (TextView) findViewById(R.id.tv_email_detail_date);content = (TextView) findViewById(R.id.tv_email_detail_content);addressor = (TextView) findViewById(R.id.tv_email_detail_addressor);recipient = (TextView) findViewById(R.id.tv_email_detail_recipient);copy = (TextView) findViewById(R.id.tv_email_detail_copy);accessoryNum = (TextView) findViewById(R.id.tv_email_detail_accessoryNum);title.setText(email[0]);date.setText(email[2]);content.setText(email[1]);addressor.setText(email[3]);recipient.setText(email[4]);copy.setText(email[6]);accessoryNum.setText(email[5]);}}
activity_email_list.xml

<?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="match_parent"    android:orientation="vertical" >    <include layout="@layout/a1_head" />    <RelativeLayout        android:id="@+id/searchbar_email"        android:layout_width="match_parent"        android:layout_height="50.0dp"        android:background="#e6e6e6"        android:visibility="gone" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="5dp"            android:layout_marginTop="8dp"            android:orientation="horizontal" >            <EditText                android:id="@+id/edt_searchbar"                style="@style/SecondFontSize"                android:layout_width="0.8dip"                android:layout_weight="0.8"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:background="@drawable/edittext_bg"                android:hint="@string/home_contact_search_email"                android:maxLength="20"                android:paddingBottom="4dp"                android:paddingTop="4dp"                android:singleLine="true"                android:textColor="#c5c5c5"                android:textColorHint="#c5c5c5" />            <Button                android:id="@+id/ib_searchbtn"                style="@style/SecondFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="#00000000"                android:paddingBottom="4dp"                android:paddingTop="4dp"                android:text="搜索"                android:textColor="#00aef0" />        </LinearLayout>    </RelativeLayout>    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        android:id="@+id/lv_email_list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/searchbar_email"        android:background="#ffffff"        android:cacheColorHint="#00000000"        android:divider="#DDDDDD"        android:dividerHeight="0.5dp"        android:fadingEdge="none"        android:fastScrollEnabled="false"        android:footerDividersEnabled="false"        android:headerDividersEnabled="false"        android:paddingTop="3dp"        android:scrollbars="none"        android:smoothScrollbar="true"        ptr:ptrAnimationStyle="flip"        ptr:ptrMode="pullFromStart" /></LinearLayout>

activity_email_detail.xml

<?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="match_parent"    android:background="#ffffff"    android:orientation="vertical" >    <include layout="@layout/a1_head" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="36dp"        android:paddingEnd="0dp"        android:paddingLeft="15dp"        android:paddingRight="0dp"        android:paddingStart="15dp" >        <TextView            android:id="@+id/textView1"            style="@style/SecondFontSize"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="发件人"            android:textColor="#00aef0" />        <TextView            android:id="@+id/tv_email_detail_addressor"            style="@style/SecondFontSize"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="0.07"            android:paddingLeft="10dp" />        <Button            android:id="@+id/hidden"            style="@style/SecondFontSize"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:background="#FFFFFF"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:text="隐藏"            android:textColor="#00aef0" />        <Button            android:id="@+id/show"            style="@style/SecondFontSize"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:background="#FFFFFF"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:text="显示"            android:textColor="#00aef0" />    </LinearLayout>    <View        android:layout_width="fill_parent"        android:layout_height="0.5dp"        android:background="#ddd" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:paddingLeft="15dp" >        <LinearLayout            android:id="@+id/shoujianren"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical" >            <TextView                android:id="@+id/TextView01"                style="@style/SecondFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginBottom="10dp"                android:layout_marginTop="12dp"                android:text="收件人"                android:textColor="#00aef0" />            <TextView                android:id="@+id/TextView03"                style="@style/SecondFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="抄送"                android:textColor="#00aef0" />        </LinearLayout>        <LinearLayout            android:id="@+id/chaosong"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"            android:orientation="vertical" >            <TextView                android:id="@+id/tv_email_detail_recipient"                style="@style/SecondFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginBottom="10dp"                android:layout_marginTop="12dp"                android:paddingLeft="11dp" />            <TextView                android:id="@+id/tv_email_detail_copy"                style="@style/SecondFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:paddingLeft="11dp" />        </LinearLayout>    </LinearLayout>    <View        android:id="@+id/hengxian"        android:layout_width="fill_parent"        android:layout_height="0.5dp"        android:background="#ddd" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="10dp"        android:layout_marginTop="8dp"        android:orientation="vertical" >        <TextView            android:id="@+id/tv_email_detail_title"            style="@style/SecondFontSize"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:paddingLeft="15dp" />        <TextView            android:id="@+id/tv_email_detail_date"            style="@style/SecondFontSize"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:paddingLeft="15dp"            android:textColor="#888888" />    </LinearLayout>    <View        android:layout_width="fill_parent"        android:layout_height="0.5dp"        android:background="#ddd" />    <TextView        android:id="@+id/tv_email_detail_content"        style="@style/SecondFontSize"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:paddingLeft="15dp"        android:paddingRight="15dp"        android:textColor="#888888" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_marginBottom="20dp"        android:layout_marginTop="30dp"        android:orientation="vertical" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="5dp"            android:layout_marginTop="8dp"            android:orientation="horizontal"            android:paddingLeft="15.0dp" >            <ImageView                android:id="@+id/imageView1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginBottom="10dp"                android:layout_marginTop="8dp"                android:contentDescription="@string/image_description"                android:scaleType="center"                android:src="@drawable/work_mail_accessory" />            <TextView                android:id="@+id/textView3"                style="@style/ForthFontSize"                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:layout_marginBottom="10dp"                android:layout_marginLeft="4dp"                android:layout_marginTop="8dp"                android:paddingTop="9.0dp"                android:text="附件共(" />            <TextView                android:id="@+id/tv_email_detail_accessoryNum"                style="@style/ForthFontSize"                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:layout_marginBottom="10dp"                android:layout_marginTop="8dp"                android:gravity="center_horizontal"                android:paddingTop="9.0dp"                android:text="1"                android:textColor="#888888" />            <TextView                android:id="@+id/textView4"                style="@style/ForthFontSize"                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:layout_marginBottom="10dp"                android:layout_marginTop="8dp"                android:paddingTop="9.0dp"                android:text="个)" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="15dp"            android:layout_marginRight="15.0dp"            android:background="@drawable/boder" >            <TextView                android:id="@+id/textView5"                style="@style/ForthFontSize"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_marginBottom="7dp"                android:layout_marginLeft="10dp"                android:layout_marginTop="8dp"                android:paddingBottom="4dp"                android:paddingTop="6dp"                android:text="XXXXXX.DOC"                android:textColor="#00aef0" />        </LinearLayout>    </LinearLayout></LinearLayout>

adapter_email_list_item.xml

<?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="horizontal" >    <ImageView        android:id="@+id/iv_email_item_icon"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:paddingLeft="10.0dp"        android:paddingRight="10.0dp"        android:scaleType="center"        android:src="@drawable/icon_new_point"        android:contentDescription="@string/image_description" />    <LinearLayout        android:layout_width="0dip"        android:layout_height="wrap_content"        android:layout_weight="1"        android:layout_marginTop="8dp"        android:layout_marginBottom="10dp"        android:orientation="vertical" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/tv_email_item_addressor"                style="@style/SecondFontSize"                android:layout_width="0dip"                android:layout_height="match_parent"                android:layout_weight="1.23"                android:textColor="#666666" />            <TextView                android:id="@+id/tv_email_item_date"                style="@style/ThirdFontSize"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:paddingTop="3.0dp"                android:textColor="#888888" />        </LinearLayout>        <TextView            android:id="@+id/tv_email_item_content"            style="@style/SecondFontSize"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:ellipsize="end"            android:layout_marginTop="8dp"            android:singleLine="true"            android:textColor="#888888" />    </LinearLayout>    <ImageView        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:paddingLeft="20.0dp"        android:paddingRight="20.0dp"        android:src="@drawable/icon_arrow_right"        android:contentDescription="@string/image_description" /></LinearLayout>




0 0
原创粉丝点击