我使用ViewHolder创建了一个自定义Listview适配器并将其与ListFragment一起使用,由于某些原因我不知道,最后一个Listview项目没有完全呈现
适配器代码
public class ListViewAdapter extends ArrayAdapter<ListItem> {
public ListViewAdapter(Context context, List<ListItem> items) {
super(context, R.layout.row, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
ListItem item = getItem(position);
if(item!=null){
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
viewHolder.tvDescription.setText(item.description);
}
return convertView;
}
/**
* The view holder design pattern prevents using findViewById()
* repeatedly in the getView() method of the adapter.
*
* @see http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
*/
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
TextView tvDescription;
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/frame_background"
android:padding="5dp" >
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/frame" >
<!-- the icon view -->
<ImageView android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:contentDescription="desc"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:src="@mipmap/ic_launcher" />
<!-- the container view for the title and description -->
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ivIcon"
android:layout_centerVertical="true" >
<!-- the title view -->
<TextView android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Medium" />
<!-- the description view -->
<TextView android:id="@+id/tvDescription"
android:layout_below="@id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Small"
android:padding="3dp" />
</RelativeLayout>
</RelativeLayout>
片段代码,因为我使用的是ListFragment
Public class IntroFragment extends ListFragment {
private List<ListItem> mItems; // ListView items list
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize the items list
mItems = new ArrayList<ListItem>();
Resources resources = getResources();
String[] factfiles=resources.getStringArray(R.array.SunFacts);
mItems.add(new ListItem(resources.getDrawable(R.mipmap.ic_launcher), factfiles[i], factfiles[i])); }
// initialize and set the list adapter
setListAdapter(new ListViewAdapter(getActivity(), mItems));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// remove the dividers from the ListView of the ListFragment
getListView().setDivider(null);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// retrieve theListView item
ListItem item = mItems.get(position);
// do something
Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
}
}
下面的屏幕截图显示了我的意思,注意底部新Listview项目的白边, 列表视图将不再滚动。 Screenshot (cant upload it here due to rep points)
先谢谢..这已经让我开了半豆和香蕉..