布局文件
首先是主页面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<com.example.text.xlist.XListView
android:id="@+id/xlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
有图片的列表展示
<?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="horizontal" >
<com.example.text.row.Myview
android:id="@+id/item2_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/k2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical" >
<TextView
android:id="@+id/item2_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="title"
android:textSize="23sp" />
<TextView
android:id="@+id/item2_body"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="body"
android:textSize="17sp" />
<ImageView
android:id="@+id/item2_imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
无图片的列表展示
<?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="horizontal" >
<com.example.text.row.Myview
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/k2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="title"
android:textSize="20sp"
android:id="@+id/item_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="17sp"
android:text="body"
android:id="@+id/item_body"
/>
</LinearLayout>
</LinearLayout>
详细页面的展示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.example.text.row.Myview
android:id="@+id/ot_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/k2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="23sp"
android:text="title"
android:id="@+id/ot_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:textSize="17sp"
android:text="body"
android:id="@+id/ot_body"
/>
</LinearLayout>
</LinearLayout>
xlistview的布局文件 可以参考自己有的源文件自行修改
bean类
public class Tweet implements Serializable{
public String id;
public String portrait;
public String body ;
public String author;
public String pubDate;
public String imgBig;
@Override
public String toString() {
return "Tweet [id=" + id + ", portrait=" + portrait + ", body=" + body
+ ", author=" + author + ", pubDate=" + pubDate + ", imgBig="
+ imgBig + "]";
}
}
MyApplication类来设置加载图片的格式
public class Myappli extends Application {
public static DisplayImageOptions options;
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
this).memoryCacheSize(2 * 1024 * 1024).threadPoolSize(3).build();
ImageLoader.getInstance().init(configuration);
options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageOnLoading(R.drawable.ic_launcher).build();
}
}
实现头像是圆形图的代码块
package com.example.text.row;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class Myview extends ImageView {
public Myview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(100, 100);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable)drawable).getBitmap();
if(null == b)
{
return;
}
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0,0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if(bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
sbmp.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,
sbmp.getWidth() / 2+0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
适配器类
public class Myadp extends BaseAdapter {
public static List<Tweet> ls_t=new ArrayList<Tweet>();
Context context;
public Myadp(Context context) {
super();
this.context = context;
}
public void changeUP(List<Tweet> ls) {
this.ls_t.addAll(0,ls);
this.notifyDataSetChanged();
}
public void changeDown(List<Tweet> ls) {
this.ls_t.addAll(ls);
this.notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if (ls_t.get(position).imgBig!=null && ls_t.get(position).imgBig!="") {
return 1;
}
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ls_t.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh=null;
ViewHolder vh2=null;
int type=getItemViewType(position);
if(convertView==null){
switch (type) {
case 0:
vh=new ViewHolder();
convertView=View.inflate(context, R.layout.ls_item, null);
vh.img=(Myview) convertView.findViewById(R.id.head);
vh.tv_title= (TextView) convertView.findViewById(R.id.item_title);
vh.tv_body= (TextView) convertView.findViewById(R.id.item_body);
convertView.setTag(vh);
break;
case 1:
vh2=new ViewHolder();
convertView=View.inflate(context, R.layout.ls_item2, null);
vh2.img=(Myview) convertView.findViewById(R.id.item2_head);
vh2.tv_title= (TextView) convertView.findViewById(R.id.item2_title);
vh2.img2=(ImageView) convertView.findViewById(R.id.item2_imageView1);
vh2.tv_body= (TextView) convertView.findViewById(R.id.item2_body);
convertView.setTag(vh2);
break;
}
}else{
switch (type) {
case 0:
vh=(ViewHolder) convertView.getTag();
break;
case 1:
vh2=(ViewHolder) convertView.getTag();
break;
}
}
switch (type) {
case 0:
System.out.println("title "+ls_t.get(position).author);
vh.tv_title.setText(ls_t.get(position).author);
vh.tv_body.setText(ls_t.get(position).body);
ImageLoader.getInstance().displayImage(ls_t.get(position).portrait, vh.img, Myappli.options);
break;
case 1:
vh2.tv_title.setText(ls_t.get(position).author);
vh2.tv_body.setText(ls_t.get(position).body);
ImageLoader.getInstance().displayImage(ls_t.get(position).portrait, vh2.img, Myappli.options);
ImageLoader.getInstance().displayImage(ls_t.get(position).imgBig, vh2.img2, Myappli.options);
break;
}
return convertView;
}
class ViewHolder {
Myview img;
TextView tv_title;
TextView tv_body;
ImageView img2;
// switch (type) {
// case 0:
//
// break;
//
// case 1:
// break;
}
}
activity类
package com.example.text;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import com.example.text.adp.Myadp;
import com.example.text.xlist.XListView;
import com.example.text.xlist.XListView.IXListViewListener;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class MainActivity extends Activity implements IXListViewListener {
private XListView xlv;
private Myadp adp;
private List<Tweet> ls_t;
private int index = 0;
private boolean b = false;
private String str;
private String path = "http://www.oschina.net/action/api/tweet_list/uid=0&pageSize=20&pageIndex=";
private Handler hd = new Handler() {
public void handleMessage(android.os.Message msg) {
// System.out.println(ls_t.size());
if (b == false) {
adp.changeUP(ls_t);// 适配器刷新的方法
} else {
adp.changeDown(ls_t);// 适配器加载的方法
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
xlv = (XListView) findViewById(R.id.xlist);
xlv.setPullLoadEnable(true);
xlv.setPullRefreshEnable(true);
xlv.setXListViewListener(this);
adp = new Myadp(this);
str = path + 0;// 默认加载接口
xlv.setAdapter(adp);// 设置适配器
getNetdata();// 请求网络加载数据
// xlv的item点击事件
xlv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent it = new Intent(MainActivity.this, OtherActivity.class);
it.putExtra("id", Myadp.ls_t.get(position - 1).id);
startActivity(it);
}
});
}
String xml = null;
private void getNetdata() {
// 网络请求
HttpUtils hu = new HttpUtils();
hu.send(HttpMethod.GET, str, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(MainActivity.this, "请求失败", 0).show();
System.out.println("请求失败");
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
System.out.println("请求成功");
xml = arg0.result;
// System.out.println("xml="+xml);
// 将字符串转换成输入流
ByteArrayInputStream inputStream = new ByteArrayInputStream(xml
.getBytes());
// 解析xml
getPull(inputStream);
}
});
}
// 解析xml
private void getPull(InputStream inputStream) {
Tweet tw = null;
XmlPullParserFactory factory;
try {
// 进行xml的pull解析
factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
int type_name = parser.getEventType();
while (XmlPullParser.END_DOCUMENT != parser.getEventType()) {
switch (type_name) {
case XmlPullParser.START_DOCUMENT:
ls_t = new ArrayList<Tweet>();
break;
case XmlPullParser.START_TAG:
if ("tweet".equals(parser.getName())) {
tw = new Tweet();
}
if (tw != null) {
if ("id".equals(parser.getName())) {
tw.id = parser.nextText();
} else if ("portrait".equals(parser.getName())) {
tw.portrait = parser.nextText();
} else if ("body".equals(parser.getName())) {
tw.body = parser.nextText();
} else if ("author".equals(parser.getName())) {
tw.author = parser.nextText();
} else if ("imgBig".equals(parser.getName())) {
tw.imgBig = parser.nextText();
} else if ("pubDate".equals(parser.getName())) {
tw.pubDate = parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(parser.getName())) {
ls_t.add(tw);// 储存到集合中
}
break;
}
type_name = parser.next();
}
System.out.println("ls_t=" + ls_t.toString());
hd.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
// 刷新
public void onRefresh() {
b = false;
str = path + 0;
getNetdata();
close();
}
private void close() {
xlv.stopLoadMore();
xlv.stopRefresh();
}
@Override
// 加载
public void onLoadMore() {
b = true;
index++;
str = path + index;
getNetdata();
close();
}
}
package com.example.text;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import com.example.text.row.Myview;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class OtherActivity extends Activity {
private Myview img;
private TextView tv_title;
private TextView tv_body;
private String id;
private String path ="http://www.oschina.net/action/api/tweet_detail?id=";
private List<Tweet> ls_t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ls_item2);
Intent it=getIntent();
id=it.getStringExtra("id");
//System.out.println("id"+id);
img = (Myview) findViewById(R.id.item2_head);
tv_title = (TextView) findViewById(R.id.item2_title);
tv_body = (TextView) findViewById(R.id.item2_body);
img2 = (ImageView) findViewById(R.id.item2_imageView1);
getNetdata();
}
String xml=null;
private ImageView img2;
private void getNetdata() {
HttpUtils hu=new HttpUtils();
hu.send(HttpMethod.GET, path+id, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(OtherActivity.this,"请求失败" , 0).show();
System.out.println("请求失败");
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
System.out.println("请求成功");
xml=arg0.result;
System.out.println("Other_xml="+xml);
ByteArrayInputStream inputStream = new ByteArrayInputStream(
xml.getBytes());
//解析xml
getPull( inputStream);
//给控件赋值
ImageLoader.getInstance().displayImage(ls_t.get(0).portrait, img);
tv_title.setText(ls_t.get(0).author);
tv_body.setText(ls_t.get(0).body);
if (ls_t.get(0).imgBig!=null && ls_t.get(0).imgBig!="") {
ImageLoader.getInstance().displayImage(ls_t.get(0).imgBig, img2);
}
}
});
}
private void getPull(InputStream inputStream) {
Tweet tw=null;
XmlPullParserFactory factory;
try {
// 进行xml的pull解析
factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
int type_name = parser.getEventType();
while (XmlPullParser.END_DOCUMENT != parser.getEventType()) {
switch (type_name) {
case XmlPullParser.START_DOCUMENT:
ls_t= new ArrayList<Tweet>();
break;
case XmlPullParser.START_TAG:
if ("tweet".equals(parser.getName())) {
tw= new Tweet();
}
if (tw != null) {
if ("id".equals(parser.getName())) {
tw.id = parser.nextText();
} else if ("portrait".equals(parser.getName())) {
tw.portrait = parser.nextText();
} else if ("body".equals(parser.getName())) {
tw.body = parser.nextText();
}
else if ("author".equals(parser.getName())) {
tw.author = parser.nextText();
} else if ("imgBig".equals(parser.getName())) {
tw.imgBig = parser.nextText();
}
}
break;
case XmlPullParser.END_TAG:
if ("tweet".equals(parser.getName())) {
ls_t.add(tw);// 储存到集合中
}
break;
}
type_name = parser.next();
}
///System.out.println("Other=ls_t=" + ls_t.toString());
//hd.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}