gpt4 book ai didi

android - 在特定的 RecyclerView CardView 项目中膨胀 fragment

转载 作者:行者123 更新时间:2023-11-29 00:09:36 25 4
gpt4 key购买 nike

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

行布局

<?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;
}

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

最佳答案

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

关于android - 在特定的 RecyclerView CardView 项目中膨胀 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com