1

我在 SDK 2.3.3 中使用 Android Gallery。我需要使所选项目的边距更宽。我保留了“最后一个”选定项目的引用,因此当进行新选择时,我可以修改新选定项目以及先前选定项目的布局中的边距。我已经尝试使相关的视图项目无效,但似乎无法触发画廊项目被“刷新”。任何有关如何使边距变化影响画廊的帮助将不胜感激。

如有必要,我很乐意澄清任何一点。

谢谢,袁

private class MySelectListener implements AdapterView.OnItemSelectedListener {

    private static final int unselected_margin_left = 1;
    private static final int unselected_margin_right = 1;
    private static final int selected_margin_left = 15;
    private static final int selected_margin_right = 15;

    private LinearLayout currWrapper = null;
    private LinearLayout lastWrapper = null;
    private Context ctx;

    public MySelectListener(Context c) {
        ctx = c;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

        // This LinearLayout is a wrapper for the gallery items
        currWrapper = (LinearLayout)(v.findViewById(R.id.unconf_order_item_layout_wrapper));

        // Reset the margins of the view that was selected
        try {
            if (lastWrapper != null) {
                setMargins(lastWrapper, false);
            }
        } catch (Exception clear) {
        }

        // Set the "wider" margins for the selected item
        try {
            setMargins(currWrapper, true);
        } catch (Exception animate) {
        }

        lastWrapper = currWrapper;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

    private void setMargins(LinearLayout view, boolean selected) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)view.getLayoutParams();
        if (selected) {
            Toast.makeText(ctx, "setting selected margins", Toast.LENGTH_SHORT).show();
            layoutParams.setMargins(selected_margin_left, 0, selected_margin_right, 0);
        } else {
            Toast.makeText(ctx, "setting unselected margins", Toast.LENGTH_SHORT).show();
            layoutParams.setMargins(unselected_margin_left, 0, unselected_margin_right, 0);
        }
    }

}
4

0 回答 0