- 要构建一个自己的listView,首先要拥有以下东西
首先,构建listView和list的组件Item
其次,构建适配器,这里采用ArreyList
ListView
<?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" >
<include
android:layout_height="wrap_content"
layout="@layout/title" >
</include>
<ListView
android:id="@+id/messagelist"
android:layout_width="match_parent"
android:layout_height="409dp" >
</ListView>
</LinearLayout>
这是组件的item
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:orientation="horizontal" >
<ImageView
android:id="@+id/headphoto"
android:layout_width="wrap_content"
android:layout_height="74dp"
android:layout_weight="0.97"
/>
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除"/>
</LinearLayout>
<TextView
android:id="@+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.03"
android:text="TextView" />
</LinearLayout>
- 构建item的类,要包含geter和seter
package com.sellsapp.message;
public class message {
private int headphoto;
private String heading;
private String news;
//初始化函数的自己定义
public message(int headphoto,String heading,String news){//定义组件的类,设置geter,seter
this.headphoto=headphoto;
this.heading=heading;
this.news=news;
}
public int getHeadphoto() {
return headphoto;
}
public void setHeadphoto(int headphoto) {
this.headphoto = headphoto;
}
public String getHeading() {
return heading;
}
public void setHeading(String heading) {
this.heading = heading;
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}
定义自己的ArreyAdapter
package com.sellsapp.message;
import java.util.List;
import com.sellsapp.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
//定义自己的adapter适配器
public class msgAdapter extends ArrayAdapter<message>{
private int resource;//为什么要有这个?
//Content 传递一个Activity对象,如果是在Fragment下传递activity,可以在这个地方用this.getActivity()传递activity,
//但是会产生一个问题,就是每次从获取fragment的activity生成fragment时,会将所有的数据再次构造,这个问题会在后面详细解释
//int resource 是什么,在开始写这玩意的时候不清楚,后面就知道,他是传递过来的item在R文件中的注册值
//object 就是自己定义的list<>对象
public msgAdapter(Context messageFragment, int resource,List<message> objects) {
super(messageFragment, resource, objects);
// TODO 自动生成的构造函数存根
this.resource=resource;//这里为什么要赋值
}
@Override //覆盖adapter适配器的getView函数,他会在初始化时自动调用
public View getView(int position,View convertView,ViewGroup parent)
{
message msg= getItem(position);//为什么int 类型的输入在赋值给object对象时不用强制类型转换?
View view=LayoutInflater.from(getContext()).inflate(resource, null);
ImageView image=(ImageView)view.findViewById(R.id.headphoto);
TextView heading=(TextView) view.findViewById(R.id.heading);
TextView news=(TextView) view.findViewById(R.id.news);
image.setImageResource(msg.getHeadphoto());
heading.setText(msg.getHeading());
news.setText(msg.getNews());
return view;
}
}
在Activity或者fragment中使用自定义的Adapter
package com.sellsapp.message;
import java.util.ArrayList;
import java.util.List;
import com.sellsapp.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
//消息的fragment
public class MessageFragment extends Fragment {
private List<message> list=new ArrayList<message>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.message_fragment, null);
init(view);
return view;
}
//需要实现功能 的代码全部写在这里面
private void init(View view) {
initlist();
msgAdapter adapter=new msgAdapter(this.getActivity(),R.layout.message_item,list);
//这里用getActivity()获取他的activity,虽然我不知到对不对,但是值得一试
ListView msgList=(ListView) view.findViewById(R.id.messagelist);
msgList.setAdapter(adapter);
}
public void initlist(){
list.clear();//这里为什么要clear()我在activity中是不用clear的,但是在fragment中,由于每次调用fragment就会初始化一次,而list中的数据没有清空,会每次调用fragment再次加入一个Msg,所以clear
message msg=new message(R.drawable.ic_launcher,"hello","我帅不帅啊");
list.add(msg);
}
}
以上就全部完成。