0

以下は list_item レイアウトです。リスト項目行内のビューテキスト要素と仕切り要素の間に大きな垂直スペースがある理由がわかりませんか? このレイアウトの何が問題になっていますか?

スクリーンショットはこちら

リスト項目の私のレイアウト 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());
    }


}

私はそれ自身のリストビューを持っていません。ところで。上下の仕切り以外に仕切りが必要ですか?

4

3 に答える 3

0

問題は、分割コードを使用しているコードにあります。ところで、なんでこんなことしてんの?... ListView にはdividerdividerHeight?があるので

上記のレイアウトを使用して数分間のコーディングで思いついたものを次に示します (textColor属性のみを削除しました)。 ここに画像の説明を入力

上記のリストのコード:

public class MyListActivity extends FragmentActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.so_list_layout);
        listView = (ListView) findViewById(R.id.localListView);
        fillListView();
    }

    private void fillListView() {
        final List<LDataRow> data = getRowData();
        ArrayAdapter<LDataRow> adapter = new ArrayAdapter<MyListActivity.LDataRow>(this,
                R.layout.so_list_row, data) {
            private LDataRow getDataRow(int position) {
                return data.get(position);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LDataRow row = getDataRow(position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(MyListActivity.this).inflate(
                            R.layout.so_list_row, parent, false);
                }
                TextView companyName = (TextView) convertView.findViewById(R.id.companyName);
                companyName.setText(row.getCompanyName());
                TextView exchange = (TextView) convertView.findViewById(R.id.exchange);
                exchange.setText(row.getExchange());
                TextView symbol = (TextView) convertView.findViewById(R.id.symbol);
                symbol.setText(row.getSymbol());
                TextView price = (TextView) convertView.findViewById(R.id.price);
                price.setText(String.valueOf(row.getPrice()));
                TextView change = (TextView) convertView.findViewById(R.id.change);
                change.setText(String.valueOf(row.getChange()));
                TextView percentChange = (TextView) convertView.findViewById(R.id.percentChange);
                percentChange.setText(String.valueOf(row.getPercentChange()));
                return convertView;
            }
        };
        listView.setAdapter(adapter);
    }

    private List<LDataRow> getRowData() {
        ArrayList<LDataRow> result = new ArrayList<MyListActivity.LDataRow>();
        result.add(new LDataRow("Facebook", "Nasdaq", "E", 52.445, 0.0, 0));
        result.add(new LDataRow("Advanced Micro", "NYSE", "D", 3.23, 0.0, 0));
        result.add(new LDataRow("Genmas", "CPH", "G", -100, -3.0, -20.0));
        return result;
    }

    private static class LDataRow {
        private String companyName;

        private String exchange;

        private String symbol;

        private double price;

        private double change;

        private double percentChange;

        public LDataRow(String companyName, String exchange, String symbol, double price,
                double change, double percentChange) {
            this.companyName = companyName;
            this.exchange = exchange;
            this.symbol = symbol;
            this.price = price;
            this.change = change;
            this.percentChange = percentChange;
        }

        public String getCompanyName() {
            return companyName;
        }

        public String getExchange() {
            return exchange;
        }

        public String getSymbol() {
            return symbol;
        }

        public double getPrice() {
            return price;
        }

        public double getChange() {
            return change;
        }

        public double getPercentChange() {
            return percentChange;
        }

    }
}

これを上で使用RelativeLayoutして、下の間隔を空けます。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp" >

EDIT仕切りをカスタマイズしたい場合は、カスタムのドローアブルとサイズをうまく指定できます。layer-list単純な, で行ったことは次のmy_so_divider.xmlとおりです。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item>
        <shape android:shape="line" >
            <stroke
                android:width="1dp"
                android:color="#000000" />
        </shape>
    </item>

</layer-list>

ListView で設定します。

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/localListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff"
    android:divider="@drawable/my_so_divider"
    android:dividerHeight="24dp"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" >

</ListView>

footerDividersEnabledheaderDividersEnableddividerおよび属性を確認してくださいdividerHeight... 自由にドローアブルをいじってみてください。下の画像の効果: ここに画像の説明を入力

于 2013-10-25T11:46:35.290 に答える