首页 > 代码库 > Android 博客园客户端 (六) OnItemLongClickListener for Blog, News and Comment

Android 博客园客户端 (六) OnItemLongClickListener for Blog, News and Comment

项目地址:https://github.com/ZhangTingkuo/AndroidCnblogs

为博客、新闻、评论界面的ListView添加长按事件,一边快速转到相应的页面。

1. 博客:查看内容、查看评论、查看作者。

2. 新闻:查看内容、查看评论。

3. 评论:引用、回复。(功能暂未实现)

  1 package com.arlen.cnblogs.view;  2   3 import java.io.UnsupportedEncodingException;  4 import java.net.URLEncoder;  5 import java.util.List;  6   7 import android.app.Dialog;  8 import android.content.Context;  9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.util.Log; 12 import android.view.View; 13 import android.widget.AdapterView; 14 import android.widget.AdapterView.OnItemClickListener; 15 import android.widget.ArrayAdapter; 16 import android.widget.ListView; 17  18 import com.arlen.cnblogs.BlogActivity; 19 import com.arlen.cnblogs.CommentActivity; 20 import com.arlen.cnblogs.NewsActivity; 21 import com.arlen.cnblogs.R; 22 import com.arlen.cnblogs.UserActivity; 23 import com.arlen.cnblogs.entity.Blog; 24 import com.arlen.cnblogs.entity.Comment; 25 import com.arlen.cnblogs.entity.News; 26 import com.arlen.cnblogs.entity.User; 27 import com.arlen.cnblogs.utils.AppUtils; 28 import com.arlen.cnblogs.utils.Config; 29  30 public class ItemDialog extends Dialog implements OnItemClickListener { 31  32     private Context context; 33     private String[] items; 34     private ListView listView; 35     public int selectIndex = 0; 36  37     public Blog blogEntry; 38     public News newsEntry; 39     public Comment commentEntry; 40     public String commentString; 41     public String TAG; 42  43     public ItemDialog(Context context, int theme) { 44         super(context, theme); 45         this.context = context; 46     } 47  48     public ItemDialog(Context context) { 49         super(context); 50         this.context = context; 51     } 52  53     public ItemDialog(Context context, String[] items) { 54         super(context); 55         this.context = context; 56         this.items = items; 57     } 58  59     @Override 60     protected void onCreate(Bundle savedInstanceState) { 61         super.onCreate(savedInstanceState); 62         this.setContentView(R.layout.item_dialog); 63  64         // this.setCanceledOnTouchOutside(false); 65  66         listView = (ListView) findViewById(R.id.listViewBlogDialog); 67         listView.setAdapter(new ArrayAdapter<String>(context, 68                 android.R.layout.simple_list_item_1, items)); 69         listView.setOnItemClickListener(this); 70     } 71  72     @Override 73     public void onItemClick(AdapterView<?> parent, View view, int position, 74             long id) { 75         selectIndex = position; 76         Log.i("ItemDialog", "selectIndex   ---   " + selectIndex); 77  78         String selectItem = items[position]; 79  80         if (selectItem.equals("查看内容")) { 81             this.hide(); 82             if (TAG.equals("blog")) { 83                 viewBlogContent(); 84             } else if (TAG.equals("news")) { 85                 viewNewsContent(); 86             } 87         } else if (selectItem.equals("查看评论")) { 88             this.hide(); 89             if (TAG.equals("blog")) { 90                 viewComment(blogEntry.getBlogId(), "blog"); 91             } else if (TAG.equals("news")) { 92                 viewComment(newsEntry.getNewsId(), "news"); 93             } 94         } else if (selectItem.equals("查看作者")) { 95             this.hide(); 96             viewAuthor(); 97         } else if (selectItem.equals("回复")) { 98             this.hide(); 99             addComment(commentString, "回复");100         } else if (selectItem.equals("引用")) {101             this.hide();102             addComment(commentString, "引用");103         } else if (selectItem.equals("取消")) {104             this.hide();105         }106 107         return;108     }109 110     private void viewBlogContent() {111         Intent intent = new Intent(context, BlogActivity.class);112 113         if (blogEntry.getAuthorAvatar() != null) {114             intent.putExtra("avatar", blogEntry.getAuthorAvatar().toString());115         } else {116             intent.putExtra(117                     "avatar",118                     "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png");119         }120         intent.putExtra("title", blogEntry.getBlogTitle());121         intent.putExtra("author", blogEntry.getAuthorName());122         intent.putExtra("published",123                 AppUtils.parseDateToString(blogEntry.getPublishedDateDate()));124         intent.putExtra("id", blogEntry.getBlogId());125         intent.putExtra("link", blogEntry.getBlogTitle());126 127         context.startActivity(intent);128     }129 130     private void viewNewsContent() {131         Intent intent = new Intent(context, NewsActivity.class);132         if (newsEntry.getTopicIcon() != null) {133             intent.putExtra("topicIcon", newsEntry.getTopicIcon().toString());134         } else {135             intent.putExtra(136                     "topicIcon",137                     "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png");138         }139 140         intent.putExtra("title", newsEntry.getNewsTitle());141         intent.putExtra("sourceName", newsEntry.getSourceName());142         intent.putExtra("published",143                 AppUtils.parseDateToString(newsEntry.getPublishedDate()));144         intent.putExtra("id", newsEntry.getNewsId());145 146         context.startActivity(intent);147     }148 149     private void viewAuthor() {150 151         new Thread(new Runnable() {152 153             @Override154             public void run() {155                 String authorName = blogEntry.getAuthorName();156                 String path = Config.SEARCH_AUTHOR_BY_NAME;157                 Log.i("SearchActivity", "textAuthor:" + authorName);158                 try {159                     authorName = URLEncoder.encode(authorName, "utf-8");160                 } catch (UnsupportedEncodingException e) {161                     e.printStackTrace();162                 }163                 path = path.replace("{TERM}", authorName);164 165                 Log.i("SearchActivity", "path:" + path);166                 List<User> userList = AppUtils.getUserList(path);167                 User userEntry = userList.get(0);168 169                 Intent intent = new Intent(context, UserActivity.class);170                 intent.putExtra("blogapp", userEntry.getBlogapp());171                 intent.putExtra("link", userEntry.getUserLink().toString());172 173                 if (userEntry.getUserAvatar() != null) {174                     intent.putExtra("avatar", userEntry.getUserAvatar()175                             .toString());176                 } else {177                     intent.putExtra(178                             "avatar",179                             "https://github.com/ZhangTingkuo/AndroidCnblogs/blob/master/res/drawable-hdpi/ic_launcher.png");180                 }181 182                 intent.putExtra("postcount", userEntry.getPostCount());183                 intent.putExtra("updated",184                         AppUtils.parseDateToString(userEntry.getUpdatedDate()));185                 intent.putExtra("title", userEntry.getTitle());186                 context.startActivity(intent);187             }188         }).start();189     }190 191     private void addComment(String string, String type) {192 193     }194 195     private void viewComment(int id, String type) {196         Intent intent = new Intent(context, CommentActivity.class);197         intent.putExtra("id", id);198         intent.putExtra("type", type);199         context.startActivity(intent);200     }201 }

 

 

Android 博客园客户端 (六) OnItemLongClickListener for Blog, News and Comment