android ListView_新聞案例,androidlistview
xml設計

![]()
<?xml version="1.0"?>
-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/lv_news" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
</RelativeLayout>
View Code

![]()
<?xml version="1.0"?>
-<RelativeLayout tools:context=".MainActivity" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/lv_news" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
</RelativeLayout>
ListView返回的xml設計
java

![]()
Activity

![]()
package com.itheima.news_listview.utils;
import java.util.ArrayList;
import android.content.Context;
import com.itheima.news_listview.R;
import com.itheima.news_listview.bean.NewsBean;
public class NewsUtils {
//封裝新聞的假數據到list中返回
public static ArrayList<NewsBean> getAllNews(Context context) {
ArrayList<NewsBean> arrayList = new ArrayList<NewsBean>();
for(int i = 0 ;i <100;i++)
{
NewsBean newsBean = new NewsBean();
newsBean.title ="謝霆鋒經紀人:偷拍系侵權行為:";
newsBean.des= "稱謝霆鋒隱私權收到侵犯,將保留追究法律責任";
newsBean.news_url= "http://www.sina.cn";
newsBean.icon = context.getResources().getDrawable(R.drawable.ic_launcher);//通過context對象將一個資源id轉換成一個Drawable對象。
arrayList.add(newsBean);
NewsBean newsBean1 = new NewsBean();
newsBean1.title ="知情人:王菲是謝霆鋒心頭最愛的人";
newsBean1.des= "身邊的人都知道謝霆鋒最愛王菲,二人早有復合跡象";
newsBean1.news_url= "http://www.baidu.cn";
newsBean1.icon = context.getResources().getDrawable(R.drawable.icon);//通過context對象將一個資源id轉換成一個Drawable對象。
arrayList.add(newsBean1);
NewsBean newsBean2 = new NewsBean();
newsBean2.title ="熱烈祝賀黑馬74高薪就業";
newsBean2.des= "74期平均薪資20000,其中有一個哥們超過10萬,這些It精英都迎娶了白富美.";
newsBean2.news_url= "http://www.itheima.com";
newsBean2.icon = context.getResources().getDrawable(R.drawable.icon2);//通過context對象將一個資源id轉換成一個Drawable對象。
arrayList.add(newsBean2);
}
return arrayList;
}
}
utls

![]()
package com.itheima.news_listview.bean;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
public class NewsBean {
public String title;
public String des;
public Drawable icon;
public String news_url;
}
bean

![]()
package com.itheima.news_listview.adapter;
import java.util.ArrayList;
import com.itheima.news_listview.R;
import com.itheima.news_listview.bean.NewsBean;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class NewsAdapter extends BaseAdapter {
private ArrayList<NewsBean> list;
private Context context;
//通過構造方法接受要顯示的新聞數據集合
public NewsAdapter(Context context,ArrayList<NewsBean> list){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
//1.復用converView優化listview,創建一個view作為getview的返回值用來顯示一個條目
if(convertView != null){
view = convertView;
}else {
//context:上下文, resource:要轉換成view對象的layout的id, root:將layout用root(ViewGroup)包一層作為codify的返回值,一般傳null
// view = View.inflate(context, R.layout.item_news_layout, null);//將一個布局文件轉換成一個view對象
//通過LayoutInflater將布局轉換成view對象
// view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);
//通過context獲取系統服務得到一個LayoutInflater,通過LayoutInflater將一個布局轉換為view對象
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.item_news_layout, null);
}
//2.獲取view上的子控件對象
ImageView item_img_icon = (ImageView) view.findViewById(R.id.item_img_icon);
TextView item_tv_des = (TextView) view.findViewById(R.id.item_tv_des);
TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
//3.獲取postion位置條目對應的list集合中的新聞數據,Bean對象
NewsBean newsBean = list.get(position);
//4.將數據設置給這些子控件做顯示
item_img_icon.setImageDrawable(newsBean.icon);//設置imageView的圖片
item_tv_title.setText(newsBean.title);
item_tv_des.setText(newsBean.des);
return view;
}
}
Adapter
老師筆記
復雜listview界面顯示 ,黑馬新聞(***********重要***********)
1.布局寫listview
2.找到listview
3.獲取新聞數據封裝到list集合中(才用模擬數據),作為adapter的顯示數據,怎麼將獲取的新聞數據給adapter???
4.創建一個adapter繼承BaseAdapter,實現4個方法
getcount: 有多少條新聞數據,就有多少個條目。
getView:將返回一個復雜的布局作為條目的內容展示;並且顯示的數據是新聞的信息。 ?????
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
//1.復用converView優化listview,創建一個view作為getview的返回值用來顯示一個條目
if(convertView != null){
view = convertView;
}else {
//context:上下文, resource:要轉換成view對象的layout的id, root:將layout用root(ViewGroup)包一層作為getview的返回值,一般傳null
view = View.inflate(context, R.layout.item_news_layout, null);//將一個布局文件轉換成一個view對象
}
//2.獲取view上的子控件對象
ImageView item_img_icon = (ImageView) view.findViewById(R.id.item_img_icon);
TextView item_tv_des = (TextView) view.findViewById(R.id.item_tv_des);
TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
//3.獲取postion位置條目對應的list集合中的新聞數據,Bean對象
NewsBean newsBean = list.get(position);
//4.將數據設置給這些子控件做顯示
item_img_icon.setImageDrawable(newsBean.icon);//設置imageView的圖片
item_tv_title.setText(newsBean.title);
item_tv_des.setText(newsBean.des);
return view;
}
5.創建一個adapter對象設置給listview
6.設置listview的條目的點擊事件,並封裝點擊事件,去查看新聞詳情。 ?????????
//設置listview條目的點擊事件
lv_news.setOnItemClickListener(this);
//listview的條目點擊時會調用該方法 parent:代表listviw view:點擊的條目上的那個view對象 position:條目的位置 id: 條目的id
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//需要獲取條目上bean對象中url做跳轉
NewsBean bean = (NewsBean) parent.getItemAtPosition(position);
String url = bean.news_url;
//跳轉浏覽器
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
1.布局寫listview ok
2.找到listview ok
3.封裝新聞數據到list集合中 ,目的是為adapter提供數據展示。 ok
4.封裝一個Adapter類繼承BaseAdatper,寫一個構造方法接受list集合數據,復寫四個方法
a.創建一個構造方法 ok
b.封裝getCount方法 ok
c.getView方法: 不ok
1.復用convertview,模板代碼,如果不都能空,需要將一個布局文件轉換為view對象作為getview的返回對象。
view = View.inflater(Context context, int resuorceId,ViewGroup root)
2.找到view上的這些子控件,目的是將list集合中的bean數據一一對應設置給這些子控件
3.從list集合中獲取postion條目上要顯示的數據Bean
4.將獲取的bean中的數據設置給這些子控件
d.getItem方法:將list集合中指定postion上的bean對象返回
e.getItemId,直接返回postion
5.創建一個封裝的Adapter對象,設置給listview ok
6.設置listview條目的點擊事件 ok
listview.setOnItem....
7.復寫OnItemClicklistener方法,獲取相應條目上的bean對象,最終獲取到url,做Intent跳轉; 不ok
#10 常用獲取inflate的寫法
1.
//context:上下文, resource:要轉換成view對象的layout的id, root:將layout用root(ViewGroup)包一層作為codify的返回值,一般傳null
//view = View.inflate(context, R.layout.item_news_layout, null);//將一個布局文件轉換成一個view對象
2.
//通過LayoutInflater將布局轉換成view對象
//view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);
3.
//通過context獲取系統服務得到一個LayoutInflater,通過LayoutInflater將一個布局轉換為view對象
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.item_news_layout, null);