嵌套在RecyclerView内的GridView不包裹高度,仅显示单行

时间:2019-02-11 13:34:53

标签: android gridview android-recyclerview recycler-adapter

我尝试将内容包装在代码中的各个位置,但是仅显示urllib.parse.unquote_to_bytes(x) 中的一条记录。在下面显示的图像中,应该有4行,但是GridView仅显示1行。如果仅将 row_work_report.xml 中的高度更改为GridView,则可以看到更多记录。由于我已通过varoius链接,请提供帮助,但找不到任何有用的信息。

enter image description here 以下是我使用的代码:

activity_work_report.xml

match_parent

row_work_report.xml:

<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"
    tools:context=".WorkReportActivity">

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        android:id="@+id/toolbarWorkReportActivity"></include>

    <RelativeLayout
        android:id="@+id/incentiveRelLayout"
        android:layout_below="@id/toolbarWorkReportActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/incentivetxtVw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:text="@string/incentive_report"
            android:textAlignment="center"
            android:layout_margin="5dp"
            android:background="@android:color/holo_orange_light"/>

        <android.support.v7.widget.RecyclerView
            android:layout_below="@id/incentivetxtVw"
            android:id="@+id/recyclerView_WorkReport"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="15dp"
            android:scrollbars="vertical">
        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>  
</RelativeLayout>

work_report_grid_cell.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/activityNameTxtVw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textStyle="bold"
        android:textSize="14sp"
        android:textColor="@color/tableRowTextColor"/>

    <LinearLayout
        android:id="@+id/incentiveGridRelLayout"
        android:padding="5dp"
        android:layout_below="@id/activityNameTxtVw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include layout="@layout/grid_view_header"></include>
        <GridView
            android:numColumns="1"
            android:gravity="center"
            android:columnWidth="100dp"
            android:stretchMode="columnWidth"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gridview"
            android:fillViewport="true"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/totalRelLayout"
        android:padding="5dp"
        android:layout_below="@id/incentiveGridRelLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="16">
    <TextView
        android:id="@+id/totalTxtvw"
        android:layout_weight="12"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/white_greyish"
        android:gravity="left"
        android:text="Total"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:id="@+id/totalTxtVwValue"
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:textStyle="bold"/>
    </LinearLayout>

</RelativeLayout>

grid_view_header.xml

<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=".5dp"
    android:weightSum="16">

    <TextView
        android:id="@+id/gridcell"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:id="@+id/gridcell1"
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:id="@+id/gridcell2"
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:id="@+id/gridcell3"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:id="@+id/gridcell4"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:id="@+id/gridcell5"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:id="@+id/gridcell6"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"/>

</LinearLayout>

WorkReportAdapter.java

<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=".5dp"
    android:weightSum="16">

    <TextView
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="S No."
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:layout_weight="4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="Beneficiary Name"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="Father Name"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="Gender"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="Date Of Service"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>
    <TextView
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:gravity="center"
        android:text="Center Claim"
        android:textStyle="bold"
        android:layout_marginRight="0.5dp"/>

    <TextView
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/white_greyish"
        android:text="State Claim"
        android:textStyle="bold"
        android:gravity="center"/>

</LinearLayout>

WorkGridAdapter.java

public class WorkReportAdapter extends RecyclerView.Adapter<WorkReportAdapter.WorkReportViewHolder>{
    private LinkedHashMap<Integer,ArrayList<AshaTransactions>> transactionsMap;
    private Context context;

    RecyclerView.LayoutManager manager;
    RecyclerView.Adapter adapter;

    public WorkReportAdapter(LinkedHashMap<Integer,ArrayList<AshaTransactions>> transactionsMap, Context context) {
        this.transactionsMap=transactionsMap;
        this.context=context;
    }

    @NonNull
    @Override
    public WorkReportViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_work_report, parent, false);
        return new WorkReportViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull WorkReportViewHolder holder, int position) {

        float incentiveGeneratedForCenter = 0;
        float incentiveGeneratedForState = 0;
        float totalIncentiveGenerated = 0;
        ArrayList<AshaTransactions> value = (ArrayList<AshaTransactions>) getEntry(position).getValue();
        WorkGridAdapter workGridAdapter = new WorkGridAdapter(value,context);
        for (AshaTransactions at:value){
            incentiveGeneratedForCenter=incentiveGeneratedForCenter+at.getIncentiveForActivity();
            incentiveGeneratedForState=incentiveGeneratedForState+at.getIncentiveForActivity();
        }
        totalIncentiveGenerated=totalIncentiveGenerated+incentiveGeneratedForCenter+incentiveGeneratedForState;
        holder.gridRecycler.setAdapter(workGridAdapter);
        holder.activityNameTxtVw.setText("Service: "+value.get(0).getServiceName());
        holder.totalTxtVwValue.setText(String.valueOf(totalIncentiveGenerated));
    }

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

    public class WorkReportViewHolder extends RecyclerView.ViewHolder {
        GridView gridRecycler;
        TextView activityNameTxtVw,totalTxtVwValue;

        public WorkReportViewHolder(@NonNull View itemView) {
            super(itemView);
            gridRecycler =(GridView) itemView.findViewById(R.id.gridview);
            //gridRecycler.setVerticalScrollBarEnabled(false);
            activityNameTxtVw=(TextView) itemView.findViewById(R.id.activityNameTxtVw);
            totalTxtVwValue=(TextView) itemView.findViewById(R.id.totalTxtVwValue);
            manager = new GridLayoutManager(context, 7, GridLayoutManager.VERTICAL, false);
        }
    }

    private Map.Entry getEntry(int id){
        Iterator iterator = transactionsMap.entrySet().iterator();
        int n = 0;
        while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            if(n == id){
                return entry;
            }
            n ++;
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

正如我所说,您可以使用嵌套的RecclerViwe

row_work_report.xml中用RecyclerView替换GridView

<android.support.v7.widget.RecyclerView
    android:id="@+id/secondary_recycler_view"
    android:layout_gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

使用此适配器

public class WorkReportAdapter  extends RecyclerView.Adapter<WorkReportAdapter.WorkReportViewHolder> {
  private Context context;

  RecyclerView.LayoutManager manager;
  RecyclerView.Adapter adapter;

  private RecyclerView mSecondaryRecyclerView;

  public WorkReportAdapter(Context context) {
    this.context=context;
  }

  @NonNull
  @Override
  public WorkReportViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_work_report, parent, false);
    return new WorkReportViewHolder(v);
  }

  @Override
  public void onBindViewHolder(@NonNull WorkReportViewHolder holder, int position) {


 //        float incentiveGeneratedForCenter = 0;
 //        float incentiveGeneratedForState = 0;
 //        float totalIncentiveGenerated = 0;
 //        ArrayList<AshaTransactions> value = (ArrayList<AshaTransactions>) getEntry(position).getValue();
 //        WorkGridAdapter workGridAdapter = new WorkGridAdapter(value,context);
 //        for (AshaTransactions at:value){
 //            incentiveGeneratedForCenter=incentiveGeneratedForCenter+at.getIncentiveForActivity();
 //            incentiveGeneratedForState=incentiveGeneratedForState+at.getIncentiveForActivity();
 //        }
 //         totalIncentiveGenerated=totalIncentiveGenerated+incentiveGeneratedForCenter+incentiveGeneratedForState;
 //        holder.gridRecycler.setAdapter(workGridAdapter);

    holder.activityNameTxtVw.setText("Service: "+"vbnbvng");
    holder.totalTxtVwValue.setText(String.valueOf("123"));

    GridLayoutManager linearLayoutManager = new GridLayoutManager(
            context,1);

    mSecondaryRecyclerView.setLayoutManager(linearLayoutManager);
    mSecondaryRecyclerView.setAdapter(new SecondaryAdapter());
  }

  @Override
  public int getItemCount() {
    return 10;
  }

  public class WorkReportViewHolder extends RecyclerView.ViewHolder {
    TextView activityNameTxtVw,totalTxtVwValue;

    public WorkReportViewHolder(@NonNull View itemView) {
        super(itemView);
        mSecondaryRecyclerView = (RecyclerView) itemView.findViewById(R.id.secondary_recycler_view);
        activityNameTxtVw=(TextView) itemView.findViewById(R.id.activityNameTxtVw);
        totalTxtVwValue=(TextView) itemView.findViewById(R.id.totalTxtVwValue);
        manager = new GridLayoutManager(context, 7, GridLayoutManager.VERTICAL, false);
    }
  }

  private class SecondaryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    TextView gridcell , gridcell1 , gridcell2,gridcell3 ,gridcell4 ,gridcell5,gridcell6;

    @Override
    public SecondaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View convertView = inflater.inflate(R.layout.work_report_grid_cell, parent, false);

         gridcell = convertView.findViewById(R.id.gridcell);
         gridcell1 = convertView.findViewById(R.id.gridcell1);
         gridcell2 = convertView.findViewById(R.id.gridcell2);
         gridcell3 = convertView.findViewById(R.id.gridcell3);
         gridcell4 = convertView.findViewById(R.id.gridcell4);
         gridcell5 = convertView.findViewById(R.id.gridcell5);
         gridcell6 = convertView.findViewById(R.id.gridcell6);

        return new SecondaryViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        for (int i = 0; i < 3; i++) {
            gridcell.setText(String.valueOf(position + 1));
            gridcell1.setText("dfdsfds");
            gridcell2.setText("adadsa");
            gridcell3.setText("M");
            gridcell4.setText("23432423");
            gridcell5.setText(String.valueOf("asdsad"));
            gridcell6.setText(String.valueOf("435dfs"));
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }
    private class SecondaryViewHolder extends RecyclerView.ViewHolder {
        public SecondaryViewHolder(View view) {
            super(view);
        }
    }
  }
}

请将chenage getItemCount更改为您的列表大小。