如何在可扩展列表视图中添加不同的子项

时间:2016-09-28 12:38:28

标签: android android-recyclerview expandablelistview

我使用Recycler视图和可扩展列表视图创建了可扩展列表视图



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="com.example.soulsystem_4.myapplication.ProjectDetails">

    <android.support.v4.widget.NestedScrollView
        android:layout_above="@+id/bottom_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  <LinearLayout
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:orientation="vertical"
      android:id="@+id/project"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/top"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--First linear section-->
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="0.2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/firstlinear">
            <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:id="@+id/imageViewProjdetail"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/></LinearLayout>

        <!--Second linear section-->
        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="0.25"
            android:layout_width="match_parent"
            android:layout_height="@dimen/secondlinear">

            <LinearLayout
                android:orientation="vertical"
                android:layout_weight="0.6"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textSize="20sp"
                    android:id="@+id/price"
                    android:text="$1,149"
                    android:layout_weight="0.8"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                <TextView
                    android:id="@+id/morgage"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    android:text="Est. Mortgage $4.7K/mo Get Pre-Qualified"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
            <LinearLayout
                android:paddingBottom="10dp"
                android:orientation="horizontal"
                android:layout_weight="0.4"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.75">
            <LinearLayout
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/address"
                    android:lines="3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
            <LinearLayout
                android:orientation="horizontal"
                android:layout_weight="2"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/bedroom"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" />
                <TextView
                    android:id="@+id/bathroom"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" />
                <TextView
                    android:id="@+id/sqft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"></LinearLayout>
            </LinearLayout>
        </LinearLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="?android:attr/listDivider"/>
        <!--Third linear-->
        <LinearLayout
            android:layout_weight="0.25"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

               <TextView
                   android:paddingTop="10dp"
                   android:paddingBottom="10dp"
                   android:text="For Sale Home Details"
                   android:textSize="20sp"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent" />
               <at.blogc.android.views.ExpandableTextView
                   android:id="@+id/expandableTextView"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="@string/lorem_ipsum"
                   android:maxLines="4"
                   android:ellipsize="end"
                   app:animation_duration="1000"/>

               <!-- Optional parameter animation_duration: sets the duration of the expand animation -->
               <Button
                   android:id="@+id/button_toggle"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:background="@android:color/transparent"
                   android:layout_gravity="bottom"
                   android:layout_alignParentBottom="true"
                   android:text="EXPAND"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="?android:attr/listDivider"/>
                <LinearLayout
                    android:id="@+id/direction"
                    android:orientation="horizontal"
                    android:paddingLeft="15dp"
                    android:paddingBottom="10dp"
                    android:paddingTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:layout_gravity="center"
                        android:background="@mipmap/car"
                        android:paddingRight="10dp"
                        android:paddingLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" />
                    <TextView
                        android:paddingLeft="10dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:textColor="@color/Green_ForestGreen"
                        android:text="Driving Direction"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" />
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="?android:attr/listDivider"/>
                <LinearLayout
                    android:id="@+id/street_view"
                    android:layout_weight="1"
                    android:paddingLeft="15dp"
                    android:paddingBottom="15dp"
                    android:paddingTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:layout_gravity="center"
                        android:background="@mipmap/car"
                        android:paddingRight="10dp"
                        android:paddingLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" />
                    <TextView
                        android:paddingLeft="10dp"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:textColor="@color/Green_ForestGreen"
                        android:text="Street view"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" />
                </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="2dp"
                    android:background="?android:attr/listDivider"/>
                <LinearLayout
                    android:layout_weight="1"
                    android:paddingLeft="15dp"
                    android:paddingBottom="15dp"
                    android:paddingTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:textSize="16sp"
                        android:paddingBottom="20dp"
                        android:textColor="@color/Green_ForestGreen"
                        android:text="Schedule a tour"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
<!--Recycler view-->
      <android.support.v7.widget.RecyclerView
          android:id="@+id/recycler_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="?android:attr/listDivider"/>

      <TextView

          android:paddingTop="10dp"
          android:paddingBottom="10dp"
          android:paddingLeft="10dp"
          android:textSize="18dp"
          android:textColor="#454545"
          android:textStyle="bold"
          android:text="Request Info"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />


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

      <EditText
          android:background="@drawable/shape_edittext"
          android:hint="Your e-mail address"
          android:padding="10dp"
          android:layout_marginBottom="10dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      <EditText
          android:background="@drawable/shape_edittext"
          android:hint="Your phone number"
          android:padding="10dp"
          android:layout_marginBottom="10dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
      <EditText
          android:background="@drawable/shape_edittext"
          android:height="80dp"
          android:hint="Your message"
          android:padding="10dp"
          android:layout_marginBottom="10dp"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />

  </LinearLayout>
    </android.support.v4.widget.NestedScrollView>




<!--Bottom fixed section-->

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:background="@color/Red_OrangeRed"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">
        <TextView
            android:padding="@dimen/bottomlinear"
            android:paddingLeft="5dp"
            android:textColor="@color/white"
            android:textAlignment="center"
            android:text="Request Info"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

这是我的观点

An in Neighborhood option i want to add different view using. I not same but i want to add buttons or text vies in that

此视图的活动文件如下所示。我已经在xml中使用了另一个xml作为子项我添加了2个文本视图并使用这两个textvies我已经传递了值。但邻居信息是不同的,我想在其中添加4个文本。如何在这个lis中添加不同的子xml。

&#13;
&#13;
  Phone phone=new Phone( );

    final private String TAG = ProjectDetails.class.getSimpleName();

    TextView textView2_buildingName,textView2_buildeName,textView2_adds;
    ImageView imageViewProjdetail;
    Project project=new Project();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_project_details);
        imageViewProjdetail=(ImageView)findViewById(R.id.imageViewProjdetail);
        textView2_buildingName=(TextView)findViewById(R.id.textView2_buildingName);
        textView2_buildeName=(TextView)findViewById(R.id.textView2_buildeName);
        textView2_adds=(TextView)findViewById(R.id.textView2_adds);

        relativeLayout = (LinearLayout)findViewById(R.id.project);
        relativeLayout.setOrientation(LinearLayout.VERTICAL);

        final ScrollView scrollView = new ScrollView(getApplicationContext());
        relativeLayout.addView(scrollView);

        final LinearLayout linearLayout = new LinearLayout(getApplicationContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(linearLayout);




        //textView2_aptType=(TextView)findViewById(R.id.textView2_aptType);
       // textView3_prsqft=(TextView)findViewById(R.id.textView3_prsqft);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mobileOSes = new ArrayList<>();
        //textView2_aptType.setText(getIntent().getStringExtra("textView2_aptType"));
        //textView3_prsqft.setText(getIntent().getStringExtra("textView3_prsqft"));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new RecyclerAdapterDemo(this, mobileOSes);
        recyclerView.setAdapter(adapter);
       String homeDetail[]={""};



        phone.setStatus("For Sale");
        phone.setPrice("$1,149,000");
        phone.setPricePerSqft("$944");
        phone.setBeds("1");
        phone.setBaths("1.5");


        // setData();




        ArrayList<Phone> features = new ArrayList<>();
/*Change the class name and access all the values*/
        features.add(new Phone("Status","For Sale"));
        features.add(new Phone("Price","$1,149,000"));
        features.add(new Phone("Price/sqft","$944"));
        features.add(new Phone("Beds","1"));
        features.add(new Phone("Baths","1.5"));
        features.add(new Phone("Sqft","1,216"));
        features.add(new Phone("Built In","2008"));
        features.add(new Phone("Property Type","Single-Family Home"));
        features.add(new Phone("Added On","304 days ago"));
        features.add(new Phone("Total Views","8491"));
        features.add(new Phone("Neighborhood","South Beach"));



        ArrayList<Phone> providedBy = new ArrayList<>();
        providedBy.add(new Phone("Agent","Paul Hwang"));
        providedBy.add(new Phone("Agent Phone","4157993542"));
        providedBy.add(new Phone("Broker","Skybox Realty"));
        providedBy.add(new Phone("Broker Phone","415-216-8743"));
        providedBy.add(new Phone("Provided By","Skybox Realty"));


        ArrayList<Phone> neighborhoodinfo = new ArrayList<>();
        neighborhoodinfo.add(new Phone("Crime: Lowest",""));
        neighborhoodinfo.add(new Phone("Affordability: 1.3M",""));
        neighborhoodinfo.add(new Phone("Amenities : Nearby Shopping,Groceries,Restaurants",""));


        ArrayList<Phone> monMortagage = new ArrayList<>();
        monMortagage.add(new Phone("Nokia Lumia 800",""));
        monMortagage.add(new Phone("Nokia Lumia 710",""));
        monMortagage.add(new Phone("Nokia Lumia 900",""));


        ArrayList<Phone> ptaxnadAssessment = new ArrayList<>();
        ptaxnadAssessment.add(new Phone("Nokia Lumia 800",""));
        ptaxnadAssessment.add(new Phone("Nokia Lumia 710",""));
        ptaxnadAssessment.add(new Phone("Nokia Lumia 900",""));

        ArrayList<Phone> publicRec = new ArrayList<>();
        publicRec.add(new Phone("Nokia Lumia 800",""));
        publicRec.add(new Phone("Nokia Lumia 710",""));
        publicRec.add(new Phone("Nokia Lumia 900",""));


        mobileOSes.add(new MobileOS("Features", features));
        mobileOSes.add(new MobileOS("Provided By", providedBy));
        mobileOSes.add(new MobileOS("Neighborhood Info", neighborhoodinfo));
        mobileOSes.add(new MobileOS("Monthly Mortgage", monMortagage));
        mobileOSes.add(new MobileOS("Property Taxes and Assessment", ptaxnadAssessment));
        mobileOSes.add(new MobileOS("Public Records", publicRec));

        direction = (LinearLayout)findViewById(R.id.direction);

        /*Just pass the destination address no need to add my location*/
        direction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://maps.google.com/maps?daddr=19.0641135,72.9947516" ));
                intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");

                startActivity(intent);
            }
        });

        /*Street view*/
        street_view = (LinearLayout)findViewById(R.id.street_view);
        street_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProjectDetails.this, MapsActivity.class);
                startActivity(intent);
            }
        });


        final ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView);
        final Button buttonToggle = (Button) this.findViewById(R.id.button_toggle);

// set animation duration via code, but preferable in your layout files by using the animation_duration attribute
        expandableTextView.setAnimationDuration(1000L);

        // set interpolators for both expanding and collapsing animations
        expandableTextView.setInterpolator(new OvershootInterpolator());

// or set them separately
        expandableTextView.setExpandInterpolator(new OvershootInterpolator());
        expandableTextView.setCollapseInterpolator(new OvershootInterpolator());

// toggle the ExpandableTextView
        buttonToggle.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(final View v)
            {
                expandableTextView.toggle();
                buttonToggle.setText(expandableTextView.isExpanded() ? R.string.collapse : R.string.expand);
            }
        });

// but, you can also do the checks yourself
        buttonToggle.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(final View v)
            {
                if (expandableTextView.isExpanded())
                {
                    expandableTextView.collapse();
                    buttonToggle.setText(R.string.expand);
                }
                else
                {
                    expandableTextView.expand();
                    buttonToggle.setText(R.string.collapse);
                }
            }
        });

// listen for expand / collapse events
        expandableTextView.setOnExpandListener(new ExpandableTextView.OnExpandListener()
        {
            @Override
            public void onExpand(final ExpandableTextView view)
            {
                Log.d(TAG, "ExpandableTextView expanded");
            }

            @Override
            public void onCollapse(final ExpandableTextView view)
            {
                Log.d(TAG, "ExpandableTextView collapsed");
            }
        });





imageViewProjdetail.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i= new Intent(ProjectDetails.this,Slideshow.class);
        //i.putExtra("position",a);
        startActivity(i);
    }
});

    }




    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        adapter.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        adapter.onRestoreInstanceState(savedInstanceState);
    }

}
&#13;
&#13;
&#13;

这是适配器类 public RecyclerAdapterDemo(活动活动,列表组){         超级(基团);         this.activity =活动;     }

@Override
public OSViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.group_view_holder, parent, false);

    return new OSViewHolder(view);
}

@Override
public PhoneViewHolder onCreateChildViewHolder(ViewGroup parent, final int viewType) {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.child_view_holder, parent, false);

    return new PhoneViewHolder(view);
}

@Override
public void onBindChildViewHolder(PhoneViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
    final Phone phone = ((MobileOS)group).getItems().get(childIndex);
    holder.onBind(phone,group);
}

@Override
public void onBindGroupViewHolder(OSViewHolder holder, int flatPosition, ExpandableGroup group) {
    holder.setGroupName(group);
} 

1 个答案:

答案 0 :(得分:1)

此链接可以回答我的问题。我必须使用HeterogeneousExpandableList接口来获取列表中的不同子项。 Different Child Layouts for different groups ExpandableListView