我想ListView
用两个TextView
s 生成,每一行(两个 textviews)都被细分隔线分割......但是适配器没有返回分隔线 - 只有 textViews......所以我做什么......
我有一个带有两个文本视图(标题和日期)的消息列表视图......这是它的 xml - pthy_messages_list_item.xml ......
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="@drawable/list_item_selector" >
<TextView
android:id="@+id/mess_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:textColor="@drawable/list_item_textselector" />
<TextView
android:id="@+id/mess_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:textColor="@drawable/list_item_textselector"/>
<View
android:id="@+id/m_devider"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/mess_date"
android:background="@color/greydevider" />
而从另一个 xml - listview ...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#333333"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="true"
android:divider="#757575"
android:dividerHeight="0.5dp"
/>
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="0.5dp"
android:background="#757575" />
</LinearLayout>
我从我的活动中创建了这个列表视图......
public class MessagesActivity extends ListActivity {
//ArrayList<HashMap<String, String>> menuList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pthy_mess);
Mess menu_data[] = new Mess[]
{
new Mess("first", "01.12.13", R.color.greydevider),
new Mess("second", "11.12.13", R.color.greydevider),
new Mess("third", "21.12.13", R.color.greydevider)
};
MessAdapter adapter = new MessAdapter(getApplicationContext(),
R.layout.pthy_messages_list_item, menu_data);
final ListView listView1 = getListView();
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String selitem = listView1.getItemAtPosition(position).toString();
Log.v("CLICKED", selitem);
}
});
}
}
和Mess.java的代码-
public class Mess {
// public int icon;
public String title;
public String date;
public int devider;
public Mess(){
super();
}
public Mess(String title, String date, int devider) {
super();
this.devider = devider;
this.title = title;
this.date = date;
}
@Override
public String toString() {
return this.title;
}
}
和MessAdapter.java -
public class MessAdapter extends ArrayAdapter<Mess>{
Context context;
int layoutResourceId;
Mess data[] = null;
public MessAdapter(Context context, int layoutResourceId, Mess[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MenuHolder holder = null;
if(row == null)
{
// LayoutInflater inflater = ((Activity)context).getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MenuHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.mess_name);
holder.txtDate = (TextView)row.findViewById(R.id.mess_date);
holder.devider = (View)row.findViewById(R.id.m_devider);
row.setTag(holder);
}
else
{
holder = (MenuHolder)row.getTag();
}
Mess xxx = data[position];
holder.txtTitle.setText(xxx.title);
holder.txtDate.setText(xxx.date);
return row;
}
@Override
public int getCount() {
return data.length;
}
static class MenuHolder
{
TextView txtTitle;
TextView txtDate;
View devider;
}
}