在片段中使用毕加索
Using picasso in fragment
我有 RecyclerView,我想发送 URL 和 Google 静态地图,并由 Picasso 设置这张地图。现在我只尝试在 RecyclerView 中的每个元素上设置任何静态映射,但 Picasso.with(x) 没有上下文。怎么做?
也许我可以在 FragmentHistory 中生成图像并将其作为参数发送?
FragmentHistory.xml 只有 RecyclerView;
我的片段历史:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout());
RecyclerView recyclerView = (RecyclerView) historyRecyclerView;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
从BD获取数据的函数:
private List<FragmentHistorySimpleViewModel> generateSimpleListAllWorkout() {
List<FragmentHistorySimpleViewModel> simpleViewModelList = new ArrayList<>();
DataBaseMain bd = new DataBaseMain(getActivity());
Cursor kurs = bd.giveAllWorkout();
if (kurs.getCount() != 0) {
kurs.moveToLast();
do {
// Pobranie wartości z kolumn (0-3)
String nr = kurs.getString(0);
String time = kurs.getString(1);
String workoutTime = kurs.getString(2);
String distance = kurs.getString(3);
String avgSpeed = kurs.getString(4);
String avgPace = kurs.getString(5);
String kcal = kurs.getString(6);
String LatLng = kurs.getString(7);
String dane = "ID: " + nr + "\n"
+ "Time: " + time + "\n"
+ "Workout time: " + workoutTime + "\n"
+ "Distance: " + distance + " km" + "\n"
+ "Average speed: " + avgSpeed + " km/h" + "\n"
+ "Average pace: " + avgPace + " min/km" + "\n"
+ "Calories: " + kcal;
simpleViewModelList.add(new FragmentHistorySimpleViewModel(dane, "OK"));
// Dopóki kursor może poruszać się w dół (po wynikach)
} while (kurs.moveToPrevious());
}
return simpleViewModelList;
}
我的 fragment_history_simple_item.xml 只有 2x Text View 和 ImageView
FragmentHistorySimpleAdapter:
public class FragmentHistorySimpleAdapter extends RecyclerView.Adapter
{
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels)
{
if (viewModels != null)
{
this.models.addAll(viewModels);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new FragmentHistorySimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position)
{
((FragmentHistorySimpleViewHolder) holder).bindData(models.get(position));
}
@Override
public int getItemCount()
{
return models.size();
}
@Override
public int getItemViewType(final int position)
{
return R.layout.fragment_history_simple_item;
}
}
FragmentHistorySimpleViewModel:
public class FragmentHistorySimpleViewModel
{
private String simpleText;
private String simpleText2;
public FragmentHistorySimpleViewModel(@NonNull final String simpleText, @NonNull final String simpleText2)
{
setSimpleText(simpleText, simpleText2);
}
@NonNull
public String getSimpleText()
{
return simpleText;
}
@NonNull
public String getSimpleText2()
{
return simpleText2;
}
public void setSimpleText(@NonNull final String simpleText, @NonNull final String simpleText2)
{
this.simpleText = simpleText;
this.simpleText2 = simpleText2;
}
}
FragmentHistorySimpleViewHolder:
public class FragmentHistorySimpleViewHolder extends RecyclerView.ViewHolder
{
private TextView simpleTextView;
private TextView simpleTextView2;
private ImageView imageView;
public FragmentHistorySimpleViewHolder(final View itemView)
{
super(itemView);
simpleTextView = (TextView) itemView.findViewById(R.id.simple_text);
simpleTextView2 = (TextView) itemView.findViewById(R.id.simple_text2);
imageView = itemView.findViewById(R.id.imageView);
}
public void bindData(final FragmentHistorySimpleViewModel viewModel)
{
simpleTextView.setText(viewModel.getSimpleText());
simpleTextView2.setText(viewModel.getSimpleText2());
String latitudeURL = "42";
String longitudeURL = "122";
String url ="https://maps.googleapis.com/maps/api/staticmap?";
url+="&zoom=13";
url+="&size=600x300";
url+="&maptype=roadmap";
url+="&markers=color:green%7Clabel:G%7C" + latitudeURL + "," + longitudeURL;
url+="&key=" + "AIzaSyCj80x6EGndGx07Hy7zuaLx_KFsHKlogV0";
Picasso.with(??????)
.load(url)
.into(imageView);
}
}
我怀疑这个问题不会在某个地方找到答案,但我相信您始终可以使用 imageView.getContext() 作为 Picasso 的上下文。然后你会得到类似 Picasso.with(imageView.getContext()).load(url).into(imageView);
的东西
此外,您的 Picasso 库可能是旧版本之一?最新版本有不同 API。类似于 Picasso.get().load(url).into(imageView);
首先,你可以使用Glide库代替Picasso。因为 Glide 可以很容易地加载任何类型的图像,无论是位图还是 url 等等,并且比 picasso 更具缩放比例。
现在你的问题是如何获得这里的上下文:
您可以在创建 FragmentHistorySimpleAdapter 对象时专门发送片段上下文,并从 FragmentHistorySimpleAdapter 构造函数获取上下文并在任何地方使用此上下文。
我的片段历史:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
Context context = container.context;
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout(), context);
return view;
}
FragmentHistorySimpleAdapter:
private Context context;
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels, Context context) {
if (viewModels != null) {
this.models.addAll(viewModels);
this.context = context;
}
}
}
我有 RecyclerView,我想发送 URL 和 Google 静态地图,并由 Picasso 设置这张地图。现在我只尝试在 RecyclerView 中的每个元素上设置任何静态映射,但 Picasso.with(x) 没有上下文。怎么做?
也许我可以在 FragmentHistory 中生成图像并将其作为参数发送?
FragmentHistory.xml 只有 RecyclerView;
我的片段历史:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout());
RecyclerView recyclerView = (RecyclerView) historyRecyclerView;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
从BD获取数据的函数:
private List<FragmentHistorySimpleViewModel> generateSimpleListAllWorkout() {
List<FragmentHistorySimpleViewModel> simpleViewModelList = new ArrayList<>();
DataBaseMain bd = new DataBaseMain(getActivity());
Cursor kurs = bd.giveAllWorkout();
if (kurs.getCount() != 0) {
kurs.moveToLast();
do {
// Pobranie wartości z kolumn (0-3)
String nr = kurs.getString(0);
String time = kurs.getString(1);
String workoutTime = kurs.getString(2);
String distance = kurs.getString(3);
String avgSpeed = kurs.getString(4);
String avgPace = kurs.getString(5);
String kcal = kurs.getString(6);
String LatLng = kurs.getString(7);
String dane = "ID: " + nr + "\n"
+ "Time: " + time + "\n"
+ "Workout time: " + workoutTime + "\n"
+ "Distance: " + distance + " km" + "\n"
+ "Average speed: " + avgSpeed + " km/h" + "\n"
+ "Average pace: " + avgPace + " min/km" + "\n"
+ "Calories: " + kcal;
simpleViewModelList.add(new FragmentHistorySimpleViewModel(dane, "OK"));
// Dopóki kursor może poruszać się w dół (po wynikach)
} while (kurs.moveToPrevious());
}
return simpleViewModelList;
}
我的 fragment_history_simple_item.xml 只有 2x Text View 和 ImageView
FragmentHistorySimpleAdapter:
public class FragmentHistorySimpleAdapter extends RecyclerView.Adapter
{
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels)
{
if (viewModels != null)
{
this.models.addAll(viewModels);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new FragmentHistorySimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position)
{
((FragmentHistorySimpleViewHolder) holder).bindData(models.get(position));
}
@Override
public int getItemCount()
{
return models.size();
}
@Override
public int getItemViewType(final int position)
{
return R.layout.fragment_history_simple_item;
}
}
FragmentHistorySimpleViewModel:
public class FragmentHistorySimpleViewModel
{
private String simpleText;
private String simpleText2;
public FragmentHistorySimpleViewModel(@NonNull final String simpleText, @NonNull final String simpleText2)
{
setSimpleText(simpleText, simpleText2);
}
@NonNull
public String getSimpleText()
{
return simpleText;
}
@NonNull
public String getSimpleText2()
{
return simpleText2;
}
public void setSimpleText(@NonNull final String simpleText, @NonNull final String simpleText2)
{
this.simpleText = simpleText;
this.simpleText2 = simpleText2;
}
}
FragmentHistorySimpleViewHolder:
public class FragmentHistorySimpleViewHolder extends RecyclerView.ViewHolder
{
private TextView simpleTextView;
private TextView simpleTextView2;
private ImageView imageView;
public FragmentHistorySimpleViewHolder(final View itemView)
{
super(itemView);
simpleTextView = (TextView) itemView.findViewById(R.id.simple_text);
simpleTextView2 = (TextView) itemView.findViewById(R.id.simple_text2);
imageView = itemView.findViewById(R.id.imageView);
}
public void bindData(final FragmentHistorySimpleViewModel viewModel)
{
simpleTextView.setText(viewModel.getSimpleText());
simpleTextView2.setText(viewModel.getSimpleText2());
String latitudeURL = "42";
String longitudeURL = "122";
String url ="https://maps.googleapis.com/maps/api/staticmap?";
url+="&zoom=13";
url+="&size=600x300";
url+="&maptype=roadmap";
url+="&markers=color:green%7Clabel:G%7C" + latitudeURL + "," + longitudeURL;
url+="&key=" + "AIzaSyCj80x6EGndGx07Hy7zuaLx_KFsHKlogV0";
Picasso.with(??????)
.load(url)
.into(imageView);
}
}
我怀疑这个问题不会在某个地方找到答案,但我相信您始终可以使用 imageView.getContext() 作为 Picasso 的上下文。然后你会得到类似 Picasso.with(imageView.getContext()).load(url).into(imageView);
此外,您的 Picasso 库可能是旧版本之一?最新版本有不同 API。类似于 Picasso.get().load(url).into(imageView);
首先,你可以使用Glide库代替Picasso。因为 Glide 可以很容易地加载任何类型的图像,无论是位图还是 url 等等,并且比 picasso 更具缩放比例。
现在你的问题是如何获得这里的上下文: 您可以在创建 FragmentHistorySimpleAdapter 对象时专门发送片段上下文,并从 FragmentHistorySimpleAdapter 构造函数获取上下文并在任何地方使用此上下文。
我的片段历史:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
Context context = container.context;
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout(), context);
return view;
}
FragmentHistorySimpleAdapter:
private Context context;
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels, Context context) {
if (viewModels != null) {
this.models.addAll(viewModels);
this.context = context;
}
}
}