我想在循环View Cell中显示一组图像。在ViewHolder中,我创建了ViewPager的瞬间。在onBindViewHolder中我设置了ViewPager的适配器。但图像未显示。请一些人提出一些解决方案,以便我能解决这个问题。我正在发送我的代码。
plt.plot(sHat.real, 'blue')
plt.plot(s, 'red')
}
====== onCreate内的MainActivity ===========
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android_versions;
private Context context;
public DataAdapter(Context context,ArrayList<AndroidVersion> android_versions) {
this.context = context;
this.android_versions = android_versions;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.tv_android.setText(android_versions.get(i).getAndroid_version_name());
viewHolder.img_android.setImageResource(R.drawable.image1);
int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
MyCustomPagerAdapter myCustomPagerAdapter = new MyCustomPagerAdapter(this.context, images);
viewHolder.viewPager.setAdapter(myCustomPagerAdapter);
}
@Override
public int getItemCount() {
return android_versions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tv_android;
ImageView img_android;
ViewPager viewPager;
public ViewHolder(View view) {
super(view);
tv_android = (TextView)view.findViewById(R.id.tv_android);
img_android = (ImageView)view.findViewById(R.id.img_android);
viewPager = (ViewPager) view.findViewById(R.id.viewPager);
}
}
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
ArrayList androidVersions = prepareData();
DataAdapter adapter = new DataAdapter(getApplicationContext(),androidVersions);
recyclerView.setAdapter(adapter);
答案 0 :(得分:0)
这段代码你可以根据你的需要改变我提供的代码一步一步跟随.. make adapter将片段添加到viewpager ..
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
mFragmentList = null;
mFragmentTitleList = null;
container.removeAllViews();
super.destroyItem(container, position, object);
}
}
public void opendSearchFriendFragment() {
FragmentController.addSearchFriendFragment(getActivity());
}
}
然后在make片段后显示viewpager并将片段添加到适配器中然后将适配器添加到viewpager中..
public class FollowersFragment extends Fragment {
private TabLayout mTbFollowers;
private ViewPager mVPFollowers;
private ViewPagerAdapter adapter;
public static FollowersFragment newInstance() {
return new FollowersFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.followers_fragment, container, false);
initViews(root);
return root;
}
private void initViews(View root) {
mTbFollowers = (TabLayout) root.findViewById(R.id.ffTbFollower);
mVPFollowers = (ViewPager) root.findViewById(R.id.ffVpFollowers);
setupViewPager(mVPFollowers);
mTbFollowers.post(new Runnable() {
@Override
public void run() {
mTbFollowers.setupWithViewPager(mVPFollowers);
}
});
mTbFollowers.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab p_tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab p_tab) {
}
@Override
public void onTabReselected(TabLayout.Tab p_tab) {
}
});
mVPFollowers.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTbFollowers));
mVPFollowers.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Fragment mFragment = adapter.getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(FollowerListFragment.newInstance(Constants.FOLLOWERS), Constants.FOLLOWERS);
adapter.addFragment(FollowerListFragment.newInstance(Constants.FOLLOWING), Constants.FOLLOWING);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(1);
}
}
然后在make recycler视图适配器之后如下..
public class FollowersAdapter extends RecyclerView.Adapter<FollowersAdapter.ItemViewHolder> {
private List<UserData> mFollowersList = new ArrayList<UserData>();
private Context context;
private LayoutInflater layoutInflater;
private String mSelectedTab;
public FollowersAdapter(Context context, List<UserData> mFollowersList, String mSelectedTab) {
this.context = context;
this.mFollowersList = mFollowersList;
this.mSelectedTab = mSelectedTab;
this.layoutInflater = LayoutInflater.from(context);
}
public interface OnRecyclerItemClickListener {
void onViewItemClicked(UserData userData);
}
OnRecyclerItemClickListener onRecyclerItemClickListener;
public void setOnRecyclerItemListener(OnRecyclerItemClickListener _onRecyclerItemClickListener) {
this.onRecyclerItemClickListener = _onRecyclerItemClickListener;
}
@Override
public int getItemCount() {
return (null != mFollowersList ? mFollowersList.size() : 0);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.follower_row_layout, parent, false);
return new ItemViewHolder(v);
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
final UserData userData = mFollowersList.get(position);
if (mSelectedTab.equals(Constants.FOLLOWERS) && userData.isFollower()) {
holder.itemView.setVisibility(View.VISIBLE);
} else if (mSelectedTab.equals(Constants.FOLLOWING) && !userData.isFollower()) {
holder.itemView.setVisibility(View.VISIBLE);
} else {
holder.itemView.setVisibility(View.GONE);
}
holder.mTvFollowerName.setText(userData.getUserName());
Glide.with(context)
.load(userData.getProfilePicPath())
.dontAnimate()
.placeholder(ContextCompat.getDrawable(context, R.drawable.placeholder01))
.error(ContextCompat.getDrawable(context, R.drawable.placeholder01))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.mIvFollowerImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onRecyclerItemClickListener != null)
onRecyclerItemClickListener.onViewItemClicked(userData);
}
});
}
class ItemViewHolder extends RecyclerView.ViewHolder {
private ImageView mIvFollowerImage;
private TextView mTvFollowerName;
public ItemViewHolder(View view) {
super(view);
mTvFollowerName = (TextView) view.findViewById(R.id.frlTvFollowerName);
mIvFollowerImage = (ImageView) view.findViewById(R.id.frlIvFollowersImage);
}
}
}
然后将此适配器实现为片段类,添加到视图寻呼机中,如下所示。 将recycleler视图和其他东西定义为片段,然后对下面的适配器方法进行一些更改。
private void setAdapter(ArrayList<UserData> userDataArrayList) {
if (followersAdapter == null) {
if (!userDataArrayList.isEmpty()) {
mTvEmpty.setVisibility(View.GONE);
followersAdapter = new FollowersAdapter(getActivity(), userDataArrayList, mSelectedTab);
mRvFollowers.setAdapter(followersAdapter);
followersAdapter.setOnRecyclerItemListener(new FollowersAdapter.OnRecyclerItemClickListener() {
@Override
public void onViewItemClicked(UserData userData) {
FragmentController.addOtherUserProfileFragment(getActivity(), mUserId, "" + userData.getUserID());
}
});
} else {
mTvEmpty.setVisibility(View.VISIBLE);
}
} else {
followersAdapter.notifyItemRangeChanged(0, userDataArrayList.size());
}
}