实例介绍
【实例简介】
【实例截图】
【核心代码】
package lty.newscol; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity implements OnClickListener{ private TextView tv_date; private TextView tv_month; private ListView lv; private String[] title; private String[] content; private List<HealthLayoutMessage> contentlist; private BaseAdapter adapter; private ImageView iv_more; List<Map<String, String>> moreList; private PopupWindow pwMyPopWindow;// popupwindow private ListView lvPopupList;// popupwindow对应的ListView private int NUM_OF_VISIBLE_LIST_ROWS = 5;//listview中栏目的数量 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initData(); initPopupWindow(); initotherview(); initviews(); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int p, long arg3) { // TODO Auto-generated method stub String url = contentlist.get(p).getUrl().toString(); Intent intent = new Intent(MainActivity.this,HealthNewsDetail.class); intent.putExtra("url", url); startActivity(intent); } }); } private List<HealthLayoutMessage> getMyData(){ List<HealthLayoutMessage> msgList = new ArrayList<HealthLayoutMessage>(); HealthLayoutMessage msg; title = getResources().getStringArray(R.array.lv_title); content = getResources().getStringArray(R.array.lv_content); /* * Tag标签:新闻的种类 * 1:慢病 * 2:免疫 * 3:流行病 * 4:保健 * */ msg = new HealthLayoutMessage(); msg.setTag(1); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[0]); msg.setContent(content[0]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(2); msg.setType(MyAdapter.LV_BIG_PIC); msg.setTitle(title[8]); msg.setPic(R.drawable.newspic1); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(1); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[1]); msg.setContent(content[1]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(3); msg.setType(MyAdapter.LV_LITTLE_PIC); msg.setTitle(title[2]); msg.setContent(content[2]); msg.setPic(R.drawable.newssquarepic1); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(4); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[3]); msg.setContent(content[3]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(4); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[4]); msg.setContent(content[4]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(2); msg.setType(MyAdapter.LV_LITTLE_PIC); msg.setTitle(title[5]); msg.setContent(content[5]); msg.setPic(R.drawable.newssquarepic2); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(1); msg.setType(MyAdapter.LV_BIG_PIC); msg.setTitle(title[9]); msg.setPic(R.drawable.newspic2); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(3); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[6]); msg.setContent(content[6]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); msg = new HealthLayoutMessage(); msg.setTag(3); msg.setType(MyAdapter.LV_NO_PIC); msg.setTitle(title[7]); msg.setContent(content[7]); msg.setUrl("http://news.fh21.com.cn/jksd/480509.html"); msgList.add(msg); return msgList; } private void initData() { moreList = new ArrayList<Map<String, String>>(); Map<String, String> map; map = new HashMap<String, String>(); map.put("share_key", "慢病"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "免疫"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "流行病"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "保健"); moreList.add(map); map = new HashMap<String, String>(); map.put("share_key", "所有资讯"); moreList.add(map); contentlist = new ArrayList<HealthLayoutMessage>(getMyData()); adapter = getAdapter(); } @SuppressLint("InflateParams") private void initPopupWindow() { LayoutInflater inflater = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.task_detail_popupwindow, null); lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list); pwMyPopWindow = new PopupWindow(layout); pwMyPopWindow.setFocusable(true); lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList, R.layout.list_item_popupwindow, new String[] { "share_key" }, new int[] { R.id.tv_list_item })); lvPopupList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { contentlist.clear(); if(position == 4){ contentlist.addAll(getMyData()); } else{ contentlist.addAll(getMyData()); for(int i=0;i<contentlist.size();i ){ if(contentlist.get(i).getTag()!=(position 1)){ contentlist.remove(i); i--; } } } adapter.notifyDataSetChanged(); } }); // 设置popupwindow 的长宽 lvPopupList.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth()); pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() 6) * NUM_OF_VISIBLE_LIST_ROWS); // 设置背景 pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.bitmap_book_read_chapterlist_repeat)); pwMyPopWindow.setOutsideTouchable(true);//设置popupwindow 点击外面消失 } private void initotherview() { // TODO Auto-generated method stub iv_more = (ImageView)findViewById(R.id.health_news_more_content); iv_more.setOnClickListener(this); } private BaseAdapter getAdapter(){ return new MyAdapter(this, contentlist); } private void initviews() { // TODO Auto-generated method stub String[] monthname = getResources().getStringArray(R.array.Month); tv_date = (TextView)findViewById(R.id.health_news_date); tv_month = (TextView)findViewById(R.id.health_news_month); lv = (ListView)findViewById(R.id.health_news_lv); lv.setAdapter(adapter); /* *修改日期信息 * */ Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int month_index = c.get(Calendar.MONTH); String date = String.valueOf(c.get(Calendar.DATE)); if(date.length()<2) date = "0" date; tv_date.setText(date); tv_month.setText(monthname[month_index]); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.health_news_more_content: if (pwMyPopWindow.isShowing()) { pwMyPopWindow.dismiss();// 鍏抽棴 } else { pwMyPopWindow.showAsDropDown(iv_more);// 鏄剧ず } } } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论