我在String.xml中有很多数据,并且我有一个活动,并在单击旨在用于另一个活动的主题时在其中使用RecyclerView(16个ImageView),并且在该活动中,我具有6个选项卡的Tab布局(片段) 。16 imageView的片段设计相同!但是数据不同... 如何使用一个Activity和6个Fragment和6个用于主题的数据模型类设置所有数据(textView中的String)。 Tnx
其他活动的意图代码在RecyclerView中单击imageView时
personalityTypeAdapter = new PersonalityTypeAdapter(PersonalityTypeActivity.this, getPersonalityPosts(), new PersonalityTypeAdapter.personalityTypeAdapterListener() {
@Override
public void onPostClicked(Personality personality) {
//Toast.makeText(getApplicationContext(), "Post clicked! " + personality.getImageUrl(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(PersonalityTypeActivity.this, PersonalityDetailsActivity.class);
startActivity(intent);
}
});
recyclerView.setAdapter(personalityTypeAdapter);
}
第六个片段之一
public class GeneralFragment extends Fragment {
private General general;
public GeneralFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentGeneralBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_general,
container, false);
//Just Test
View view = binding.getRoot();
general = new General(String.valueOf(Html.fromHtml(getString(R.string.text_content_about_mbti))),
"http://static.talentyab.com/upload/blog_images/ESFJ.png");
binding.setGeneral(general);
return view;
}
}
主题之一的数据模型
public class General {
private String general;
private String imageUrl;
public General(String general, String imageUrl) {
this.general = general;
this.imageUrl = imageUrl;
}
@BindingAdapter("imageUrl")
public static void loadImage(ImageView view, String imageUrl) {
Glide.with(view.getContext())
.load(imageUrl)
.apply(new RequestOptions().placeholder(R.drawable.image).error(R.drawable.image_broken_variant))
.into(view);
}
public String getGeneral() {
return general;
}
public void setGeneral(String general) {
this.general = general;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
我也使用DataBinding :) 如果您想要任何代码,我准备放置主题。 tnx
点击图片
转到带有6个标签的页面
答案 0 :(得分:0)
我找到了一个解决方案:)我不使用片段...只需更改ViewPagerAdapter。
ViewPagerAdapter.java
public class ViewPagerAdapter extends PagerAdapter {
private static final String TAG = "ViewPagerAdapter";
private Context context;
private List<PersonItem> personItemList;
public ViewPagerAdapter(Context context, List<PersonItem> personItemList) {
this.context = context;
this.personItemList = personItemList;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@Override
public int getCount() {
return personItemList.size();
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return personItemList.get(position).getToolbarTitle();
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_layout, container, false);
PersonItem personItem = personItemList.get(position);
ImageView imgPerson = layout.findViewById(R.id.img_person);
TextView txtPersonContent = layout.findViewById(R.id.txt_person_content);
if (personItem.getImageUrl() != null) {
Glide.with(context)
.load(personItem.getImageUrl())
.into(imgPerson);
} else {
imgPerson.setVisibility(View.GONE);
}
if (personItem.getContent() != null) {
txtPersonContent.setText(personItem.getContent());
}
container.addView(layout);
return layout;
}
}
然后在res中使用一个Layout.xml和一些字符串数组...,最后我在选项卡中设置了数据:)快乐的代码