首页 > 解决方案 > 可扩展布局向上扩展而不是向下扩展

问题描述

当我尝试将我的可扩展布局放在卡片视图的底部时, android:layout_gravity="bottom"它会移到底部但向上扩展

点击相对布局“按钮”后

我从 arraylist 获取数据,将其传递给recyclerviewcardview 我不知道这是因为适配器动画设置还是只是因为 xml

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

    <android.support.v7.widget.CardView
        android:layout_gravity="top"
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="551dp"
        android:animateLayoutChanges="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/holderimg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/imageView5"
                    android:layout_width="68dp"
                    android:layout_height="62dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    app:srcCompat="@drawable/s1960678" />

                <TextView
                    android:id="@+id/nameArray"
                    android:layout_width="250dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:fontFamily="serif"
                    android:includeFontPadding="false"
                    android:text="TextView"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <RelativeLayout
                    android:id="@+id/button"
                    android:layout_width="48dp"

                    android:layout_height="48dp"
                    android:layout_alignParentRight="true"
                    android:layout_gravity="right"
                    android:layout_marginTop="30dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/ic_arrow_drop_down_black_24dp"
                    android:foregroundGravity="right"
                    android:gravity="right|center_horizontal"
                    android:orientation="vertical">

                    <View
                        android:id="@+id/view2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true" />
                </RelativeLayout>

            </LinearLayout>

        </LinearLayout>

        <com.github.aakira.expandablelayout.ExpandableLinearLayout
            android:id="@+id/expandableLayout"
            android:layout_width="match_parent"
            android:layout_height="72dp"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:foregroundGravity="bottom"

            android:gravity="top"
            android:orientation="vertical"
            app:ael_duration="400"
            app:ael_expanded="false">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/button3"
                    android:layout_width="199dp"
                    android:layout_height="wrap_content"
                    android:text="Button"
                    tools:text="Download .PDF" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button"
                    tools:text="View PDF" />
            </LinearLayout>

        </com.github.aakira.expandablelayout.ExpandableLinearLayout>


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


</RelativeLayout>

这是它从中获取数据的活动,它的xml只有recyclerview

package com.example.imc.PDF;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.imc.MainAdapter;
import com.example.imc.MySingleton;
import com.example.imc.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Resultsdwldview extends AppCompatActivity {

    public RequestQueue requestQueue;
    TextView MedicalText,PhoneText,AddressText,EmailText,result,resultArray;
    EditText id;
    Button getidbtn;

    List<ArrayList<String>> Tests = new ArrayList<>();
    ArrayList<String> urlArray = new ArrayList<String>();
    ArrayList<String> nameArray = new ArrayList<String>();
    ArrayList<String> IdfromjsonArray = new ArrayList<String>();
    ArrayList<String> Patient_ID_Array = new ArrayList<String>();


    RecyclerView mRecyclerView;
    RecyclerView.LayoutManager mLayoutManager;
    RecyclerView.Adapter mAdapter;
    WebView webView;
    String PDFurl;
    String idvalue;
    String url = "https://docs.google.com/viewer?url=http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";

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

        MedicalText = findViewById(R.id.MedicalText);
        PhoneText = findViewById(R.id.PhoneText);
        AddressText = findViewById(R.id.AddressText);
        EmailText = findViewById(R.id.EmailText);

      //  webView  = findViewById(R.id.webview);  //WEBVIEW

       // webView.requestFocus();
      //  webView.getSettings().setJavaScriptEnabled(true);


       // webView.loadUrl(url);




        mRecyclerView = findViewById(R.id.recyvlerView);
     //   id = findViewById(R.id.getid);
      //  getidbtn =(Button) findViewById(R.id.send);

        mLayoutManager  = new LinearLayoutManager(this);
        jsonParse();
/*
          getidbtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  //idvalue = id.getEditableText().toString();

                  jsonParse();


                  }

          });*/

    }





    public void jsonParse() {

        String URL="http://ashaaban-001-site1.btempurl.com/api/patients/"+4;
        //String URL="http://ashaaban-001-site1.btempurl.com/api/patients/"+idvalue;

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONArray jsonArray = response.getJSONArray("Tests");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject arraydata = jsonArray.getJSONObject(i);

                       // Tests.add(jsonObject.getString(String.valueOf(i)));
                        String  urlName = arraydata.getString("urlName");
                        String Name =arraydata.getString("Name");
                        String Idfromjson =arraydata.getString("Id");
                        String P_ID_Array =arraydata.getString("patients_Id");

                        urlArray.add(urlName);
                        nameArray.add(Name);
                        IdfromjsonArray.add(Idfromjson);
                        Patient_ID_Array.add(P_ID_Array);

                        Tests.add(urlArray);
                        Tests.add(nameArray);
                        Tests.add(IdfromjsonArray);
                        Tests.add(Patient_ID_Array);

                          //  resultArray.append("urlName = " +urlName+" \n\n Name"+Name);

                    }

                    //resultArray.append(Tests+"  ,  \n ");
                   // mRecyclerView.setHasFixedSize(true);


               } catch (JSONException e) {
                    e.printStackTrace();
               }




                try {
                    Integer  P_MedicalNumber = response.getInt("MedicalNumber");
                    Integer P_id = response.getInt("Id");
                    String  P_Username = response.getString("UserName");
                    Integer  P_PhoneNumber = response.getInt("phoneNumber");
                    String  P_Address = response.getString("Address");
                    String  P_Email = response.getString("Email");


                    //result.append("Medical Number : "+P_MedicalNumber+" \n id :"+P_id+"UserName"+P_Username);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mAdapter = new MainAdapter(Tests,nameArray);
                mRecyclerView.setLayoutManager(mLayoutManager);
                mRecyclerView.setHasFixedSize(true);
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.notifyItemRangeInserted(0, Tests.size());
               // webView.loadUrl(PDFurl);


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show();

            }
        });

        // requestQueue.add(request);
        MySingleton.getInstance(Resultsdwldview.this).addToReqeustQueue(jsonObjectRequest);



    }

}

标签: androidcardviewexpandablerecyclerview

解决方案


适配器

package com.example.imc;

import android.animation.ObjectAnimator;
import android.content.ClipData;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.aakira.expandablelayout.ExpandableLayout;
import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
import com.github.aakira.expandablelayout.ExpandableLinearLayout;
import com.github.aakira.expandablelayout.Utils;

import java.util.ArrayList;
import java.util.List;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {

    List<ArrayList<String>> Tests;
    ArrayList<String> urlArray;
    ArrayList<String> nameArray;
    ArrayList<String> IdfromjsonArray ;
    ArrayList<String> Patient_ID_Array;
    SparseBooleanArray expandState =new SparseBooleanArray();



    public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> urlArray, ArrayList<String> nameArray,
                       ArrayList<String> idfromjsonArray, ArrayList<String> patient_ID_Array) {
        Tests = tests;
        this.urlArray = urlArray;
        this.nameArray = nameArray;
        IdfromjsonArray = idfromjsonArray;
        Patient_ID_Array = patient_ID_Array;
    }

    public MainAdapter(List<ArrayList<String>> tests, ArrayList<String> nameArray) {
        Tests = tests;
        this.nameArray = nameArray;

    }

    @NonNull
    @Override

    //Initialize the viewholder
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row,viewGroup,false);
        return new ViewHolder(view);
    }


    /***************////////////////onBind Holds values to pass it to the viewholder///////////////////*******************
    /***************////////////////
    // This method is called for each ViewHolder to
    // bind it to the adapter
    // . This is where we will// pass our data to our ViewHolder///////////////////*******************

    @Override
    public void onBindViewHolder(@NonNull final MainAdapter.ViewHolder holder, final int i) {

       // holder.URLName.setText( urlArray.get(i));
        holder.mFullname.setText( nameArray.get(i));
        holder.expandableLayout.setInRecyclerView(true);
        holder.expandableLayout.setExpanded(expandState.get(i));
        holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {

            @Override
            public void onPreOpen() {
                changeRotate(holder.button,180f,0f).start();
                expandState.put(i,true);
            }

            @Override
            public void onPreClose() {
                changeRotate(holder.button,0f,180f).start();
                expandState.put(i,false);

            }
        });

        holder.button.setRotation(expandState.get(i)?0f:180f);
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Expandable child item

                holder.expandableLayout.toggle();

            }
        });
    }

    private ObjectAnimator changeRotate(RelativeLayout button, float to, float from) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(button,"rotation",from,to);
        animator.setDuration(200);
        animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
        return animator;
    }

    @Override
    public int getItemCount() {
            return nameArray.size();
    }




   /***************////////////////VIEWHOLDER///////////////////*******************

    public class ViewHolder extends RecyclerView.ViewHolder {

      //  public TextView URLName;
        public TextView mFullname;

        public RelativeLayout button;
        public ExpandableLinearLayout expandableLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

           mFullname=itemView.findViewById(R.id.nameArray);
            //URLName=itemView.findViewById(R.id.URLName);
            button=itemView.findViewById(R.id.button);
            expandableLayout=itemView.findViewById(R.id.expandableLayout);
        }
    }
}

推荐阅读