2

我有一个可以接受 2 种不同类型对象的 ListView:Food 和 SectionItem。

我有这个 :

在此处输入图像描述

我想知道是否可以像这样在 SectionItem 和 Food 项目之间进行布局填充/边距差异(仅使用一个 ListView):

在此处输入图像描述

这是我的适配器:

public class FoodsAdapter extends ArrayAdapter<Item> {

    private Context context;
    private ArrayList<Item> items;
    private LayoutInflater vi;

    public FoodsAdapter(Context context,ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);
        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.activity_souscarte_list_item_titlesouscategorie, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.souscarte_titlesouscategorie);
                sectionView.setText(si.getTitle());
            }else{
                Food food = (Food)i;
                v = vi.inflate(R.layout.activity_souscarte_list_item_elementsouscategorie, null);

                final TextView title = (TextView) v.findViewById(R.id.souscarte_element_title);
                final TextView prix =  (TextView) v.findViewById(R.id.souscarte_element_prix);
                final TextView desc =  (TextView) v.findViewById(R.id.souscarte_element_desc);

                if (title != null)
                        title.setText(food.getName());
                if (prix != null)
                        prix.setText(Float.toString(food.getPrice())+ "€");
                if (desc != null)
                        desc.setText(food.getDescription());
            }
        }
        return v;
    }

}

这是我的活动:

public class SousCarteActivityNew extends Activity {
    private ArrayList<ArrayList<Food>> arraySousCategories;
    private ArrayList<Item> allFoodsInOne;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_souscarte_list_item);

        arraySousCategories = FoodsContainer.sousCategoriesArray;

        allFoodsInOne = new ArrayList<Item>();
        for (ArrayList<Food> foodArray : arraySousCategories){
            allFoodsInOne.add(new SectionItem(foodArray.get(0).getType()));
            for (Food food : foodArray){
                allFoodsInOne.add(food);
            }
        }
        LinearLayout ll = (LinearLayout) findViewById(R.id.souscarte_linearlayout);
        ListView lv = new ListView(getApplicationContext());
        FoodsAdapter adapter = new FoodsAdapter(getApplicationContext(), allFoodsInOne);
        lv.setAdapter(adapter);
        ll.setPadding(15, 15, 15, 0);
        ll.addView(lv);
    }
}

这是我的 activity_souscarte_list_item_titlesouscategorie.xml :

<?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="match_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/souscarte_titlesouscategorie"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:gravity="center"
        android:layout_margin="0dp"
        android:textColor="@color/white"
        android:textSize="19sp"/>

</LinearLayout>

这是我的 activity_souscarte_list_item_elementsouscategorie.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="@color/white"
    android:id="@+id/souscarte_element_layout"
    android:layout_marginLeft="17dp"
    android:layout_marginRight="17dp"
    android:padding="5dp">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:id="@+id/souscarte_element_title"
            android:textColor="@color/black"
            android:layout_weight="3"
            android:paddingLeft="5dp"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:id="@+id/souscarte_element_prix"
            android:gravity="right"
            android:textColor="@color/black"
            android:layout_weight="1"
            android:paddingRight="5dp"/>
    </LinearLayout>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:id="@+id/souscarte_element_desc"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"/>
</LinearLayout>
4

2 回答 2

1

是的,有一个解决方案。top_margin从方法给你的部分不同getView。为此,您必须使用LinearLayoutParam.

有一种方法叫做setMargin(left,top,right,bottom)just give any appropriate value for topand assign layoutParamto your row。

于 2013-03-29T18:39:28.420 回答
0

I just put all of my layouts in a FrameLayout with padding to do the trick.

于 2013-03-30T00:13:08.250 回答