【问题标题】:Inflate fragment in specific RecyclerView CardView item在特定 RecyclerView CardView 项目中膨胀片段
【发布时间】:2016-11-28 15:02:29
【问题描述】:

我有一个 RecyclerView,其中包含与用户活动目标相关的卡片。在每张卡片的一部分中,我想为不同的片段充气,例如目标 1 可能是饼图,而目标 2 可能是折线图。下面是我的代码,我遇到的问题是目前片段只在列表中的第一项中膨胀。

行布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvLastSync"
        android:textSize="10dip"
        android:textColor="@android:color/darker_gray"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Last updated: 12:00">
    </TextView>

    <!--
    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/today_pie_chart"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="76dp"
        android:background="@android:color/transparent"
        android:layout_below="@+id/tvLastSync"/> -->

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_chart_container"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:layout_marginTop="76dp"
        android:background="@android:color/transparent"
        android:layout_below="@+id/tvLastSync"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvDetails"
        android:textSize="30dip"
        android:textColor="@android:color/darker_gray"
        android:paddingTop="5dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="9999/10000">
    </TextView>

    <TextView
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:id="@+id/tvInfo"
        android:textSize="20dip"
        android:textColor="@android:color/darker_gray"
        android:layout_alignParentRight="true"
        android:text="Some additional information"
        android:layout_below="@+id/tvDetails">
    </TextView>


</RelativeLayout>

Goal progress adapter
public class GoalProgressAdapter extends RecyclerView.Adapter<GoalProgressAdapter.ViewHolder> {

    private ArrayList<GoalStruct> goalList;
    private int rowLayout;
    private Context mContext;
    private LoadDataHelper loadDataHelper;

    public GoalProgressAdapter(ArrayList<GoalStruct> pGoalList, int pRowLayout, Context pContext){
        this.goalList = pGoalList;
        this.rowLayout = pRowLayout;
        this.mContext = pContext;
        this.loadDataHelper = new LoadDataHelper(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public int getItemCount() {
        return goalList == null ? 0 : goalList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Goal g = goalList.get(i).goal;

        // todo populate info with some text based on progress - e.g. lets get started
        viewHolder.tvInfo.setText(" a motivational message ");

        // when the adapter has been bound load data related to goal
        loadDataHelper.getDataFromCache(false, g, viewHolder.tvLastSync, viewHolder.tvDetails, viewHolder.chartHolder);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tvLastSync;
        public FrameLayout chartHolder;
        public  TextView tvDetails;
        public  TextView tvInfo;

        public ViewHolder(View itemView){
            super(itemView);
            tvLastSync = (TextView) itemView.findViewById(R.id.tvLastSync);
            chartHolder = (FrameLayout) itemView.findViewById(R.id.fragment_chart_container);
            tvDetails = (TextView) itemView.findViewById(R.id.tvDetails);
            tvInfo = (TextView) itemView.findViewById(R.id.tvInfo);
        }

    }



}

LoadDataHelper 的相关部分

FrameLayout fragmentContainer;    
    if (chartType == 1){
                // if using a pie chart
                 xVals.add("label 1");
                 xVals.add("label 2");
                 //yVals.add(new Entry(Float.parseFloat(goal.getTargetMeasurement()),0));
                 //yVals.add(new Entry(Float.parseFloat(es.value),1));
                 yVals.add(goal.getTargetMeasurement());
                 yVals.add(es.value);

                 Fragment_PieChart fpc = new Fragment_PieChart();
                 Bundle args = new Bundle();
                 args.putStringArrayList(Values.keyPieAxisLabels, xVals);
                 args.putStringArrayList(Values.keyPieAxisValues, yVals);
                 //args.putInt(Values.keyPieChart, fragmentContainer);
                 fpc.setArguments(args);

                 FragmentManager fm = ((Activity) mContext).getFragmentManager();
                 FragmentTransaction ft = fm.beginTransaction();
                 ft.add(fragmentContainer.getId(), fpc);
                 Log.d("LoadDataHelper: ", "Performing frag tran in " + fragmentContainer.getId());
                 ft.commit();

            }

Fragment_PieChart 的一部分

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_pie_chart, container, false);
        Log.d("Test", "Inflating Fragment_PieChart");

        Bundle b = getArguments();
        xVals = b.getStringArrayList(Values.keyPieAxisLabels);
        yVals = b.getStringArrayList(Values.keyPieAxisValues);

        pc = (PieChart) v.findViewById(R.id.today_pie_chart);

        drawPieChart(xVals, yVals);

        return v;
    }

任何帮助将不胜感激:-)

【问题讨论】:

  • 类名最重要的部分是? ... Recycler ...因此解决方案是在每个项目中添加每种类型的图表并隐藏它们或仅显示您需要的那个...或(更好)阅读有关 getItemViewType(int position)适配器...但是当然应该在膨胀时知道类型(onCreateViewHolder)...(如目标对象)...在第二种解决方案中,您必须为每种类型设置不同的布局
  • 您好塞尔文,非常感谢您的建议。最后,我避免在 RecyclerView 中使用片段,并按照您的建议为每种类型实现了具有不同布局的 getItemViewType(int position),干杯!

标签: android android-fragments android-cardview


【解决方案1】:

考虑使用getItemViewType(int position)RecyclerView.Adapter。示例:How to create RecyclerView with multiple view type?。它对我有用。我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您需要使用 RecyclerView.Adapter 中提供的 getItemViewType() 方法。我为我找到了this 链接,它对我有很大帮助。

    【讨论】:

      猜你喜欢
      • 2018-09-16
      • 2021-08-30
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      相关资源
      最近更新 更多