下面是一个 list_item 布局。我不明白为什么列表项行内的 viewtext 元素和分隔元素之间有很大的垂直空间?这种布局有什么问题?
我的列表项的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/companyName"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/table_text_selector"/>
<TextView
android:id="@+id/exchange"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_text_light_selector"
android:layout_below="@id/companyName"/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_text_light_selector"
android:layout_below="@id/companyName"
android:layout_toLeftOf="@id/exchange"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/table_text_selector"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_stock_change_text"
android:layout_alignParentRight="true"
android:layout_below="@id/price"/>
<TextView
android:id="@+id/percentChange"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/table_stock_change_text"
android:layout_alignBaseline="@id/change"
android:layout_alignBottom="@id/change"
android:layout_toLeftOf="@id/change"
android:layout_marginRight="5dp"/>
</RelativeLayout>
这是片段:
public class StockListFragment extends ItemListFragment<CurrentStock> {
@Inject BootstrapServiceProvider serviceProvider;
@Inject LogoutService logoutService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Injector.inject(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setEmptyText(R.string.no_stocks);
}
@Override
protected void configureList(Activity activity, ListView listView) {
super.configureList(activity, listView);
listView.setFastScrollEnabled(true);
listView.setDividerHeight(0);
getListAdapter().addHeader(activity.getLayoutInflater()
.inflate(R.layout.stock_list_item_labels, null));
}
@Override
LogoutService getLogoutService() {
return logoutService;
}
@Override
public Loader<List<CurrentStock>> onCreateLoader(int id, Bundle args) {
final List<CurrentStock> initialItems = items;
return new ThrowableLoader<List<CurrentStock>>(getActivity(), items) {
@Override
public List<CurrentStock> loadData() throws Exception {
try {
List<CurrentStock> latest = null;
if(getActivity() != null)
latest = serviceProvider.getService(getActivity()).getStocks();
if (latest != null)
return latest;
else
return Collections.emptyList();
} catch (OperationCanceledException e) {
Activity activity = getActivity();
if (activity != null)
activity.finish();
return initialItems;
}
}
};
}
public void onListItemClick(ListView l, View v, int position, long id) {
CurrentStock currentStock = ((CurrentStock) l.getItemAtPosition(position));
startActivity(new Intent(getActivity(), StockActivity.class).putExtra(STOCK, currentStock));
}
@Override
public void onLoadFinished(Loader<List<CurrentStock>> loader, List<CurrentStock> items) {
super.onLoadFinished(loader, items);
}
@Override
protected int getErrorMessage(Exception exception) {
return R.string.error_loading_stocks;
}
@Override
protected SingleTypeAdapter<CurrentStock> createAdapter(List<CurrentStock> items) {
return new StockListAdapter(getActivity().getLayoutInflater(), items);
}
}
这是适配器
public class StockListAdapter extends SingleTypeAdapter<CurrentStock> {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMMM dd");
/**
* Create adapter
*
* @param inflater
* @param items
*/
public StockListAdapter(LayoutInflater inflater, List<CurrentStock> items) {
super(inflater, R.layout.stock_list_item);
setItems(items);
}
/**
* Create adapter
*
* @param inflater
*/
public StockListAdapter(LayoutInflater inflater) {
this(inflater, null);
}
@Override
public long getItemId(final int position) {
final String id = getItem(position).getSymbol();
return !TextUtils.isEmpty(id) ? id.hashCode() : super
.getItemId(position);
}
/**/
@Override
protected int[] getChildViewIds() {
return new int[] { R.id.companyName, R.id.exchange, R.id.symbol, R.id.price, R.id.change, R.id.percentChange };
}
@Override
protected void update(int position, CurrentStock currentStock) {
setText(0, currentStock.getCompanyName());
setText(1, currentStock.getExchange());
setText(2, currentStock.getSymbol());
setText(3, currentStock.getPrice().toString());
setText(4, currentStock.getChange().toString());
setText(5, currentStock.getPercentChange());
}
}
我没有它自己的列表视图。顺便提一句。除了顶部和底部分隔器之外,我需要一个分隔器吗?