如何以编程方式填充包含在另一个布局中的自定义布局?

时间:2015-09-18 15:25:04

标签: android android-layout layout adapter baseadapter

我有一个自定义布局,我包含在另一个布局中。现在我想从设置较大布局的活动设置第一个布局的视图作为其contentView。我想过创建一个baseAdapter,但我被卡住了。这是我尝试过的:

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:weightSum="3">
//...
                <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/offer_card_view"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/container_margin"
                    android:layout_marginStart="@dimen/container_margin"
                    android:layout_marginRight="@dimen/container_margin"
                    android:layout_marginEnd="@dimen/container_margin"
                    android:gravity="center"
                    android:padding="20dp"
                    card_view:cardCornerRadius="4dp">

                    <include layout="@layout/offer" />

                </android.support.v7.widget.CardView>

子布局(报价):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    tools:context="com.example.abdelrhmanghreeb.maftou7.Activities.admin.offers.NewOfferActivity">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/offer_thumb_area"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/offer_thumb"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop" />


                <com.pnikosis.materialishprogress.ProgressWheel
                    android:id="@+id/progress_wheel"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    wheel:matProg_barColor="@color/primary"
                    wheel:matProg_progressIndeterminate="true" />

                <View
                    android:id="@+id/view"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_below="@+id/offer_thumb" />

                <ImageView
                    android:id="@+id/offer_type"
                    android:layout_width="@dimen/large_icon_size"
                    android:layout_height="@dimen/large_icon_size"
                    android:layout_alignBottom="@+id/offer_name"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/badge_clearance"
                    android:layout_marginRight="@dimen/container_margin"
                    android:layout_marginEnd="@dimen/container_margin" />

                <TextView
                    android:id="@+id/offer_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="@dimen/container_margin"
                    android:layout_marginLeft="@dimen/container_margin"
                    android:layout_marginStart="@dimen/container_margin"
                    android:layout_marginRight="@dimen/container_margin"
                    android:layout_marginEnd="@dimen/container_margin"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/offer_thumb"
                    android:layout_toLeftOf="@+id/offer_type"
                    android:layout_toStartOf="@+id/offer_type"
                    android:hint="Offer Name"
                    android:lines="1"
                    android:maxEms="300"
                    android:maxLines="1"

                    android:shadowColor="@color/text_shadow"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="2"
                    android:textColor="@color/light_font"
                    android:textSize="@dimen/abc_text_size_large_material"
                    android:textStyle="bold"
                    tools:ignore="NewApi" />
            </RelativeLayout>

            <LinearLayout
                android:id="@+id/offer_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/container_margin"
                android:layout_marginStart="@dimen/container_margin"
                android:layout_marginRight="@dimen/container_margin"
                android:layout_marginEnd="@dimen/container_margin"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/offer_start_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" - " />

                <TextView
                    android:id="@+id/offer_end_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </LinearLayout>
<!--details-->
            <TextView
                android:id="@+id/offer_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Offer Details"
                android:lines="3"
                android:maxEms="500"
                android:maxLines="3"
                android:layout_marginLeft="@dimen/container_margin"
                android:layout_marginStart="@dimen/container_margin"
                android:layout_marginRight="@dimen/container_margin"
                android:layout_marginEnd="@dimen/container_margin" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

    <!--submit bar
                <LinearLayout
                    android:id="@+id/submit_bar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/button_height"
                    android:orientation="horizontal">

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />

                    <Button
                        android:id="@+id/btn_submit_offer"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Post" />

                    <View
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight="1" />
                </LinearLayout>-->

活动:

    Public class MainActivity {
         @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         this.setContentView(R.layout.expanded_shop_profile_activity);
         //...
         offer_adapter mOfferAdapter = new offer_adapter(getApplicationContext());
         CardView offer_card_view = (CardView) findViewById(R.id.offer_card_view);
         offer_card_view.setAdapter(mOfferAdapter);
}

我试过的适配器: public class offer_adapter扩展了BaseAdapter {

private Context _context;
private LayoutInflater infalInflater;

public  offer_adapter(Context context) {
    _context = context;
    infalInflater = (LayoutInflater) this._context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}



@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    if (myView == null) {
        // if it's not recycled, initialize some attributes
        convertView = infalInflater.inflate(R.layout.offer, null);
    } else {
        convertView = (View) myView;
    }

    initializeUniversalImageLoader();
    //getting the layout views
    offer_thumb = (ImageButton) convertView.findViewById(R.id.offer_thumb);
    offer_type = (ImageButton) convertView.findViewById(R.id.offer_type);
    final TextView offer_name = (EditText) convertView.findViewById(R.id.offer_name);
    final TextView offer_start_date = (TextView) convertView.findViewById(R.id.offer_start_date);
    final TextView offer_end_date = (TextView) convertView.findViewById(R.id.offer_end_date);
    final TextView offer_details = (EditText) convertView.findViewById(R.id.offer_details);
    wheel=(ProgressWheel) convertView.findViewById(R.id.progress_wheel);
    wheel.setBarColor(Color.BLUE);
    wheel.setVisibility(View.INVISIBLE);
    offer_name.setText("someText");
    offer_start_date.setText("someText");
    offer_end_date.setText("someText");
    offer_details.setText("someText");
 //....
    return convertView;
}

}

这种方法对吗?我错过了什么?

2 个答案:

答案 0 :(得分:1)

CardView不是AdapterView所以我不知道如何在其上调用setAdapter()。但是,它是ViewGroup,因此您每次要添加视图时都可以调用addView()

答案 1 :(得分:0)

正如kris larson所说:CardView不是AdapterView。他是对的。而不是你可以使用的是RecyclerView,如果你想填充Cardview,你可以在SubLayout中的scrollView(根据你的要求)之前添加它,在主布局中你可以添加RecylerView(适用于你的ListView)并删除cardview。