嵌套的GridView内部RecyclerView未包裹的高度和仅示出单排

问题描述 投票:0回答:1

我想在我的代码的不同位置包裹的内容,但只显示内部GridView单个记录。在下面显示的图像应该有4行,但GridView是仅示出1行。如果我改变高度row_work_report.xml到match_parent才把更多的记录是可见的。请帮助我走过varoius链接走了,但找不到任何帮助。

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

activity_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>

row_work_report.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>

work_report_grid_cell.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>

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: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>

work report adapter.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;
    }
}

work grid adapter.Java

public class WorkGridAdapter extends BaseAdapter {
    private ArrayList<AshaTransactions> list = new ArrayList<>();
    private Context context;
    Queries queries;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public WorkGridAdapter(ArrayList<AshaTransactions> list, Context context) {
        this.list=list;
        this.context=context;
        queries=new Queries(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            grid = new View(context);
            grid = inflater.inflate(R.layout.work_report_grid_cell, null);
            TextView gridcell,gridcell1,gridcell2,gridcell3,gridcell4,gridcell5,gridcell6;
            gridcell = grid.findViewById(R.id.gridcell);
            gridcell1 = grid.findViewById(R.id.gridcell1);
            gridcell2 = grid.findViewById(R.id.gridcell2);
            gridcell3 = grid.findViewById(R.id.gridcell3);
            gridcell4 = grid.findViewById(R.id.gridcell4);
            gridcell5 = grid.findViewById(R.id.gridcell5);
            gridcell6 = grid.findViewById(R.id.gridcell6);

            if(list.size()!=0){
                ArrayList<Individual> individualObjList = queries.getIndividualByIdIndividual(Long.parseLong(String.valueOf(list.get(position).getIdIndividual())));
                Individual individualObj = individualObjList.get(0);
                gridcell.setText(String.valueOf(position+1));
                gridcell1.setText(individualObj.getName().toString());
                gridcell2.setText(individualObj.getFatherName());
                if(individualObj.getIdGender()==1){
                    gridcell3.setText("M");
                }else{
                    gridcell3.setText("F");
                }
                gridcell4.setText(sdf.format(list.get(position).getDateOfTransaction()));
                gridcell5.setText(String.valueOf(list.get(position).getIncentiveForActivity()));
                gridcell6.setText(String.valueOf(list.get(position).getIncentiveForActivity()));
            }
        } else {
            grid = (View) convertView;
        }

        return grid;
    }

}
android gridview android-recyclerview recycler-adapter
1个回答
1
投票

就像我说的,你可以使用嵌套RecclerViwe

与RecyclerView在row_work_report.xml更换的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);
        }
    }
  }
}

请更改qazxsw POI名单大小。

© www.soinside.com 2019 - 2024. All rights reserved.