本文章思路是根据MVC(model、view、controler)模型实现一个新闻客户端的原始功能(ListView的跳转匹配)
model端包含图片、标题、内容域,代码如下:
package com.example.listviewextenddemo;
public class Resource {
private int image;
private int title;
private int content;
public Resource(int image,int title,int content){
this.image=image;
this.title=title;
this.content=content;
}
public int getContent() {
return content;
}
public int getImage() {
return image;
}
public int getTitle() {
return title;
}
public void setContent(int content) {
this.content = content;
}
public void setImage(int image) {
this.image = image;
}
public void setTitle(int title) {
this.title = title;
}
}
controler端包含listview显示界面和跳转后的内容显示界面:
1.listview界面代码:
package com.example.listviewextenddemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private ListView mListView;
private Resource[] res={new Resource(R.mipmap.aim,R.string.title_01,R.string.content_01),
new Resource(R.mipmap.blogger,R.string.title_02,R.string.content_02),
new Resource(R.mipmap.behance,R.string.title_03,R.string.content_03)};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView=(ListView)findViewById(R.id.listview);
SimpleAdapter adapter=new SimpleAdapter(this,getData(),R.layout.layout_unit,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("title",res[position].getTitle());
intent.putExtra("content",res[position].getContent());
startActivity(intent);
}
});
}
private List<Map<String,Object>> getData(){
List<Map<String,Object>> target=new ArrayList<Map<String,Object>>();
Map<String,Object> map=null;
for(int i=0;i<res.length;i++){
map=new HashMap<String,Object>();
map.put("image",res[i].getImage());
map.put("text",getResources().getString(res[i].getTitle()));
target.add(map);
}
return target;
}
}
内容界面代码:
package com.example.listviewextenddemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView title;
private TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
title=(TextView)findViewById(R.id.title);
content=(TextView)findViewById(R.id.content);
Intent intent=getIntent();
int i_title=intent.getIntExtra("title",1);
int i_content=intent.getIntExtra("content",1);
title.setText(i_title);
content.setText(i_content);
}
}
view端xml文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<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/image"
android:layout_width="30dp"
android:layout_height="30dp"/>
<TextView
android:id="@+id/text"
android:singleLine="true"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</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="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"/>
<ScrollView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>