开发者

Setting margins on an item in ListView

Problem

I'm trying to create a ListView where you can reorder the items by dragging开发者_StackOverflow中文版.

This involves hiding the item you're dragging from the list but an item cannot be completely hidden (setting the height to 0 or setting the visibility to GONE does nothing) so instead I set the visibility to INVISIBLE and the height to 1 but that, of course, causes a small jump when dragging.

Solution

To combat this my goal is to set a negative margin on the item which pulls it up one pixel.

Here comes the problem though, you cannot set margins on a generic view, or rather you can't do it in code.

You can set a margin in XML and it will work fine but not in code, is there any way around this?


When you want to set margins to a view in code, you set them to it's LayoutParms. For instance:

View v; // let's guess you have this view... then
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
layoutParams.setMargins(1, 2, 3, 4);

It works fine with a view which is inside a LinearLayout. You can do it this way, or:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(etc..);
layoutParams.setMargins(1, 2, 3, 4);
parent.addView(view, layoutParams);


I ended up using a wrapper for the adapter instead.

The adapter moves the "hole" for me instead of creating room for the dragged view.

Since margins does not really work well in android I don't want use those and paddings will still leave the tiny 1px jump, I think the adapter is the only good solution.


From my experience, I think the embedded LinearLayout is an acceptable solution, not the ideal one because it may cause over-draw problem on the performance side. but it usable.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="@dimen/list_item_surrounding_pad" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/module_entry_settings_item_background"
        android:orientation="horizontal"
        android:soundEffectsEnabled="true" >

        <ImageView
            android:id="@+id/list_item_document_source_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ImageView>

        <TextView
            android:id="@+id/list_item_document_source_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:minHeight="?android:attr/listPreferredItemHeight"
            android:soundEffectsEnabled="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="@dimen/drawer_text_font_size" />
    </LinearLayout>

</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜