I want to add ViewPager with tablayout in my RecyclerView row. But fragment I add in ViewPager just show in firts row, from second row, just TabLayout show, fragment in ViewPager not show. I read and found that I need set ID for each ViewPager. ViewPager does not allow sharing of the id in the same fragment, even if it is part of a recyclerview context. But I don'nt know how to do that. Anyone can show me what exactly step I need to do or another method ? Thanks you very much.
Code my RecyclerView Adapter
public class ListArtifactsAdapter extends
RecyclerView.Adapter<ListArtifactsAdapter.ListArtifactsViewHolder>
{
private List<Artifact> artifacts;
private Context context;
public static class ListArtifactsViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ImageView mImageView;
public ViewPager mViewPager;
public TabLayout mTabLayout;
public ListArtifactsViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.titleTextView);
mImageView = (ImageView)itemView.findViewById(R.id.imageView);
View v = itemView.findViewById(R.id.function_tab);
mViewPager = (ViewPager)v.findViewById(R.id.pager);
mTabLayout = (TabLayout)v.findViewById(R.id.tab_layout);
}
}
public ListArtifactsAdapter(List<Artifact> artifacts, Context context){
this.artifacts = artifacts;
this.context = context;
}
#Override
public ListArtifactsAdapter.ListArtifactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ListArtifactsViewHolder vh = new ListArtifactsViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_demo, parent, false));
return vh;
}
#Override
public void onBindViewHolder(ListArtifactsAdapter.ListArtifactsViewHolder holder, int position) {
Artifact artifact = artifacts.get(position);
holder.mTextView.setText(artifact.getName());
holder.mImageView.setImageResource(R.drawable.add);
holder.mViewPager.setAdapter(new ArtifactInformationAdapter(((AppCompatActivity)context).getSupportFragmentManager(), artifact.getAudio(), artifact.getTranscript()));
holder.mTabLayout.setupWithViewPager(holder.mViewPager);
}
#Override
public int getItemCount() {
return artifacts.size();
}
}
My ViewPager Adapter
public class ArtifactInformationAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ArtifactInformationAdapter(FragmentManager fragmentManager,String audio, String transcript) {
super(fragmentManager);
mFragmentList.add(AudioFragment.newInstance(audio));
mFragmentList.add(TranscriptFragment.newInstance(transcript));
mFragmentTitleList.add("Audio");
mFragmentTitleList.add("Transcript");
}
#Override
public int getCount() {
return mFragmentList.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Related
I have a Viewpager with two tabs inside. The tabs are Cards and Favorite cards. I am using same fragment inside both tabs. The fragment contains a RecyclerView which I update dynamically. I add elements to both tabs one by one. I first completely add elements to first tab(Cards tab), after finishing that I add elements to second tab(Favorite card tab). My problem is when I add elements to the second tab, my RecyclerView inside first tab also get updated. Now the elements in first tab RecyclerView is the sum of existing elements plus the elements I add in the second tab. I set two different ArrayList in both the ViewPager tab. But when getItemCount() called in first tab RecyclerView, the ArrayList has the element that I added in the second tab.
I am using the same fragment with a RecyclerView in both tabs. When I want to update a single tab, I call notifyDatasetChanged() on the ViewPager. So each time RecyclerView inside the fragment is reinitialized.
This is my Viewpager adapter
public class ViewPagerWebCardsAdapter extends FragmentStatePagerAdapter {
private ArrayList<WebCardBaseResponseModel> webCardDetailList;
private ArrayList<WebCardBaseResponseModel> followingWebCardList;
private Context context;
private boolean isEmptyFavoriteWebcardList;
private boolean isEmptyWebcardsList;
public ViewPagerWebCardsAdapter(FragmentManager fm, ArrayList<WebCardBaseResponseModel> webCardDetailList,
ArrayList<WebCardBaseResponseModel> followingWebCardList,
Context context) {
super(fm);
this.webCardDetailList = webCardDetailList;
this.followingWebCardList = followingWebCardList;
this.context = context;
}
public void updateWebCardLists( ArrayList<WebCardBaseResponseModel> webCardDetailList,
ArrayList<WebCardBaseResponseModel> followingWebCardList) {
this.webCardDetailList = webCardDetailList;
this.followingWebCardList = followingWebCardList;
notifyDataSetChanged();
}
public void setEmptyFollowingCardsFragment() {
isEmptyFavoriteWebcardList = true;
notifyDataSetChanged();
}
public void setEmptyWebCardsFragment() {
isEmptyWebcardsList = true;
notifyDataSetChanged();
}
#Override
public Fragment getItem(int position) {
if(position == 0) {
WebCardListFragment webCardListFragment = new WebCardListFragment();
webCardListFragment.setWebCardDetailList(webCardDetailList);
webCardListFragment.setEmptyListFragment(isEmptyWebcardsList);
return webCardListFragment;
} else {
WebCardListFragment webCardListFragment = new WebCardListFragment();
webCardListFragment.setWebCardDetailList(followingWebCardList);
webCardListFragment.setEmptyListFragment(isEmptyFavoriteWebcardList);
webCardListFragment.setAsFavoritesList();
return webCardListFragment;
}
}
#Override
public int getCount() {
return Constants.NUMBER_TWO;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public CharSequence getPageTitle(int position) {
if(position == 0) {
return context.getString(R.string.webcards);
} else {
return context.getString(R.string.favorite);
}
}
}
This is my Card fragment
public class WebCardListFragment extends Fragment {
#BindView(R.id.web_card_recycler_view)
RecyclerView mWebCardDetailRecyclerView;
#BindView(R.id.tv_description)
TextView tvDescription;
#BindView(R.id.progress_bar)
public ProgressBar progressBar;
private WebCardListAdapter mWebCardListAdapter;
ArrayList<WebCardBaseResponseModel> mWebCardDetailList;
WebCardActivity activity;
private boolean isFavorites;
private boolean isEmptyListFragment;
#Override
public void onAttach(Context context) {
super.onAttach(context);
activity = (WebCardActivity) context;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setWebCardDetailList(ArrayList<WebCardBaseResponseModel> mWebCardDetailList) {
this.mWebCardDetailList = mWebCardDetailList;
}
public void setAsFavoritesList() {
this.isFavorites = true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webcard_list, container, false);
ButterKnife.bind(this, view);
initView();
return view;
}
private void initView() {
if(isFavorites) {
tvDescription.setText(getString(R.string.select_webcard_to_share));
}
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity);
mWebCardDetailRecyclerView.setLayoutManager(mLayoutManager);
mWebCardDetailRecyclerView.setHasFixedSize(true);
mWebCardDetailRecyclerView.setItemAnimator(new DefaultItemAnimator());
if(mWebCardDetailList != null && mWebCardDetailList.size() > 0) {
progressBar.setVisibility(View.GONE);
tvDescription.setVisibility(View.VISIBLE);
mWebCardListAdapter = new WebCardListAdapter(activity, mWebCardDetailList, isFavorites);
}
if(isEmptyListFragment) {
progressBar.setVisibility(View.GONE);
tvDescription.setVisibility(View.VISIBLE);
tvDescription.setText(getString(R.string.no_webcards_to_list));
}
mWebCardDetailRecyclerView.setAdapter(mWebCardListAdapter);
}
public void setEmptyListFragment(boolean isEmptyList) {
isEmptyListFragment = isEmptyList;
}
}
Finally this is my RecyclerViewadapter I use inside the fragment
public class WebCardListAdapter extends RecyclerView.Adapter<WebCardListAdapter.ViewHolder> {
private Context mContext;
private ArrayList<WebCardBaseResponseModel> mWebCardDetailList;
private boolean isFavorites;
public WebCardListAdapter(Context context, ArrayList<WebCardBaseResponseModel> webCardDetailList, boolean isFavorites) {
mContext = context;
mWebCardDetailList = webCardDetailList;
this.isFavorites = isFavorites;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_webcard_detail_layout, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
setListItemView(holder, position);
setListItemClickListener(holder);
}
#Override
public int getItemCount() {
return mWebCardDetailList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView mCardNameTextView;
private LinearLayout mWeCardLinearLayout;
#BindView(R.id.tv_phone)
TextView phone;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
mCardNameTextView = (TextView) itemView.findViewById(R.id.webcard_name_textview);
mWeCardLinearLayout = (LinearLayout) itemView.findViewById(R.id.wed_card_title_linear_layout);
}
}
}
I want the ListView inside both tabs to be independent. When I add elements to the second tab RecyclerView, that should not reflect in the first tab RecyclerView.
The Basic difference
FragmentPagerAdapter
Good for a limited (fixed) number of items (Fragments). Why? Because it never removes a fragment instance from FragmentManager once it’s created (unless that Activity is finished). It only detaches the Views from Fragments which are currently not visible. onDestroyView() will be called on your Fragment once it’s out of reach and later onCreateView() will be called once you go back to this Fragment.
FragmentStatePagerAdapter
A FragmentStatePagerAdapter is more memory savvy. It completely removes Fragment instances from the FragmentManager once they are out of reach. The state of the removed Fragments is stored inside the FragmentStatePagerAdapter. The Fragment instance is recreated once you return back to an existing item and the state is restored. This adapter is suitable for lists with an unknown count or for lists where the items change a lot.
So FragmentPagerAdapter will solve this
I call notifyDatasetChanged() on the view pager. So each time recycler view inside the fragment is reinitialised.
Remove the override function "getItemPosition" from vew pager adapter class. Hope it will help.
Thanks for the answers, anyway this issue has got nothing to do with Recyclerview or Viewpager, it happened because of my misunderstanding on the variable reference. I change the ArrayList somewhere else inside the activity. That affects the list in recyclerview.
I have tried a lot to implement a CardView which inside has a ViewPager (with fragments) and a TabLayout but RecyclerView shows only the first item and not the others.
Here is my ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int tabCount;
public ViewPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return tabCount;
}
}
Here is my TimeSheetAdapter:
public class TimeSheetAdapter extends RecyclerView.Adapter<TimeSheetAdapter.ViewHolder> {
Context context;
ArrayList<TimeSheet> timeSheetArrayList;
public TimeSheetAdapter(Context context, ArrayList<TimeSheet> timeSheetArrayList) {
this.context = context;
this.timeSheetArrayList = timeSheetArrayList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
public ViewHolder(View itemView) {
super(itemView);
tabLayout = (TabLayout) itemView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) itemView.findViewById(R.id.pager);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setupWithViewPager(viewPager);
viewPager.setOffscreenPageLimit(2);
tabLayout.setupWithViewPager(viewPager);
}
}
public TimeSheetAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View viewHolder = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_sheet_row, parent, false);
return new TimeSheetAdapter.ViewHolder(viewHolder);
}
#Override
public void onBindViewHolder(TimeSheetAdapter.ViewHolder holder, int position) {
ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity) context).getSupportFragmentManager(), holder.tabLayout.getTabCount());
holder. viewPager.setAdapter(adapter);
if (holder.tabLayout != null) {
for (int i = 0; i < holder.tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = holder.tabLayout.getTabAt(i);
if (tab != null)
tab.setText("Tab " + Integer.toString(i + 1));
}
}
holder.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(holder.tabLayout));
}
#Override
public int getItemCount() {
return timeSheetArrayList.size();
}
}
The key is to assign unique ids to each ViewPager using setId() method, even they are inside rows of the RecyclerView.
Update ViewPagerAdapter as follows:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private String[] tabTitles = new String[] {"Tab 1", "Tab 2"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return tabTitles.length;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
Update TimeSheetAdapter as follows:
public class TimeSheetAdapter extends RecyclerView.Adapter<TimeSheetAdapter.ViewHolder> {
private Context context;
private ArrayList<TimeSheet> timeSheetArrayList;
public TimeSheetAdapter(Context context, ArrayList<TimeSheet> timeSheetArrayList) {
this.context = context;
this.timeSheetArrayList = timeSheetArrayList;
}
public TimeSheetAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View viewHolder = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_sheet_row, parent, false);
return new TimeSheetAdapter.ViewHolder(viewHolder);
}
#Override
public void onBindViewHolder(TimeSheetAdapter.ViewHolder holder, int position) {
ViewPagerAdapter adapter = new ViewPagerAdapter(((AppCompatActivity) context).getSupportFragmentManager());
holder.viewPager.setAdapter(adapter);
holder.tabLayout.setupWithViewPager(holder.viewPager);
holder.viewPager.setOffscreenPageLimit(2);
}
#Override
public int getItemCount() {
return timeSheetArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TabLayout tabLayout;
ViewPager viewPager;
public ViewHolder(View itemView) {
super(itemView);
tabLayout = itemView.findViewById(R.id.tabLayout);
viewPager = itemView.findViewById(R.id.pager);
// Assign a unique id so that ViewPager is shown.
// Following method is an example and can be unreliable.
viewPager.setId((int) (Math.random() * 10000));
}
}
}
And MainActivity should be as follows:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<TimeSheet> timeSheetArrayList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
TimeSheetAdapter adapter = new TimeSheetAdapter(this, timeSheetArrayList);
recyclerView.setAdapter(adapter);
fetchData(); // Populate timeSheetArrayList
}
...
}
I implemented the recyclerview with layoutmanager in horizontal format in android studio. Everything was going well until android studio crashed. I managed to recover all my code but this time whenever I scroll, my textview items disappear. I have tried rebuilding using different API versions but the problem still persists. this is my code below:
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String LOGTAG="kwik";
SalesDataSource salesDS;
SalesAdapter adapter;
Sales sales;
List<Sales> transaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
// Give the PagerSlidingTabStrip the ViewPager
PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
// Attach the view pager to the tab strip
tabsStrip.setViewPager(viewPager);
}
}
MovieAdapter
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder>{
private List<MenuItem> menuItemList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView menuItem;
public MyViewHolder(View view) {
super(view);
menuItem = (TextView) view.findViewById(R.id.tvMenuItem);
}
}
public MovieAdapter(List<MenuItem> menuItemList) {
this.menuItemList = menuItemList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_item_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
MenuItem menu = menuItemList.get(position);
holder.menuItem.setText(menu.getMenuItem());
}
#Override
public int getItemCount() {
return menuItemList.size();
}
}
try with
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
MenuItem menu = menuItemList.get(position);
holder.menuItem.setText(menu.getMenuItem());
}
final int position will give informations that textView inside cannot be changed
I've tried to post a similar questions without any answer, though of posting again.
The issue I am having is with viewing Fragment through Tabs, I have 3 Tabs in an AppBar displayed with ViewPager & FragmentPagerAdapter.
The problem is when the activity start, one of the tabs does not behave in a consistent way, upon logging in to the app & activity for the first time it would display the content of the Fragment ( Video clips ) but during switching between activities in the app, and going back to the Activity containing the 3 Tabs, choosing that specific Tab does not display anything, althou in the Logcat, i could tell it is retrieving Data related to the Vid Clip that should be displayed, also going back that Activity i see in the Logcat that even when i choose a different Tab, ( pictures ) Data from the 2 others Tab is being retrieved !?
The setup is as follows;
Main activity;
public class Member extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.member);
Firebase.setAndroidContext(this);
ButterKnife.bind(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new PicContentMMFragment(), "Pictures");
adapter.addFragment(new VidContentFragment(), "Videos");
adapter.addFragment(new LocationsContentFragment(), "Locations");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(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);
}
}
Well, as one of the users commented on the post pointed out that the problem could be from the specific fragemnt itself and not the way Tabs and Viewpager following is the VidContentFragment;
public class VidContentFragment extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
Log.i("MyTag_onCreate","vidContentFragment_Loaded");
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public VideoView video;
public TextView authorName;
public TextView ratingValue;
public TextView locationValue;
public RatingBar ratingB;
public Button submitRating;
public LinearLayout placeNameHolder;
public int newRating;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.item_card_vid, parent, false));
authorName = (TextView) itemView.findViewById(R.id.card_title);
ratingValue = (TextView) itemView.findViewById(R.id.ratingValue);
locationValue = (TextView) itemView.findViewById(R.id.locationValue);
video = (VideoView) itemView.findViewById(R.id.placeVid);
ratingB = (RatingBar)itemView.findViewById(R.id.ratingBarMM);
ratingB.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
newRating = (int) ratingB.getRating();
}
});
submitRating = (Button) itemView.findViewById(R.id.submitRatingMM);
submitRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
public_id = publicID[getPosition()];
Log.i("MyTag_Rating",public_id);
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.authorName.setText(author[position]);
holder.ratingValue.setText(ratingV[position]);
holder.locationValue.setText(locationV[position]);
Uri video = Uri.parse(mVideos[position]);
holder.video.setVideoURI(video);
holder.video.setMediaController(new MediaController(mContext));
holder.video.requestFocus();
holder.video.seekTo(1000);
holder.video.pause();
try{
String url1 = mVideos[position];
URL ulrn = new URL(url1);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int bgColor = palette.getVibrantColor(mContext.getResources().getColor(android.R.color.black));
holder.placeNameHolder.setBackgroundColor(bgColor);
}
});
else
Log.e("MyTag_BMP","The Bitmap is NULL");
}catch (Exception e){
}
}
#Override
public int getItemCount() {
return LENGTH;
}
}
}
Some part of the code (related to retrieving data from firebase) is left out to shorten it.
Although the other 2 fragments are organized the same way, the behavior of this fragment is inconsistent ?
Here is the version of the adapter with viewholder that I have for a recyclerview you said would likely be helpful. You'll need to refactor to adjust for your situation. I have gutted it mostly to help show just the bare interactions.
public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalCardViewHolder>{
private List<Animals> animalList;
public AnimalAdapter adapter = this;
Context context;
public AnimalAdapter(List<Animals> animalList, Context context) {
this.animalList = animalList;
this.context = context;
this.setInterface((iAdapterInterface) context);
}
#Override
public int getItemCount() {
return animalList.size();
}
#Override
public void onBindViewHolder(AnimalCardViewHolder holder, int position) {
Animals animal = animalList.get(position);
holder.txtName.setText(animal.getName());
holder.txtType.setText(animal.getType());
}
#Override
public AnimalCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.animal_card_view, parent, false);
return new AnimalCardViewHolder(itemView);
}
public class AnimalCardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView txtName;
protected TextView txtType;
public AnimalCardViewHolder(final View itemView) {
super(itemView);
txtName = (TextView) itemView.findViewById(R.id.txtName);
txtType = (TextView) itemView.findViewById(R.id.txtType);
imgClose = (ImageView) itemView.findViewById(R.id.txtRemove);
listItem = itemView.findViewById(R.id.list_item);
listItem.setOnClickListener(this);
imgClose.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtRemove:
break;
case R.id.list_item:
break;
}
}
}
}
I am using FragmentStatePagerAdapter to load fragment with viewpager with tab layout. I am passing different id's to load same fragment with different data in getItem() but it is always returning fragment with same data on first time and when I swipe through next screens and came back to first screen then it will showing correct data.
Any solution or info is very helpful.
I am calling adapter like this in my onCreate() and it always return same data for position 0 and 1.
MyPagerAdapter adapter = new MyPagerAdapter (getSupportFragmentManager(),bundle);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
My Adapter
static class MyPagerAdapter extends FragmentStatePagerAdapter {
private Bundle b;
public MyPagerAdapter (FragmentManager fm,Bundle b) {
super(fm);
this.b=b;
}
#Override
public Fragment getItem(int position) {
ContentListFragment fragment = new ContentListFragment();
b.putString("id", tabList.get(position).get("id"));
fragment.setArguments(b);
return fragment;
}
#Override
public int getCount() {
return tabList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabList.get(position).get("title");
}
}
Edit
ContentListFragment.java
public class ContentListFragment extends Fragment{
private static final String TAG="ContentListFragment";
private String id,type;
private TextView norecord;
private View rootView;
private List<AppReading> appReadingList;
private RecyclerView recyclerView;
private ContentRecyclerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d(TAG, "onCreateView");
rootView = inflater.inflate(R.layout.content_list, container,false);
initView();
initData();
return rootView;
}
private void initView() {
// TODO Auto-generated method stub
norecord=(TextView)rootView.findViewById(R.id.norecord);
recyclerView=(RecyclerView) rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
private void initData() {
id=getArguments().getString("id");
getContentList(category);
}
// Get records by category
private void getContentList(String id){
Log.d(TAG, "getContentList: "+id);
Dao dao=new Dao(getActivity());
dao.open();
appReadingList=dao.getContentList(id);
dao.close();
//Set adapter if data available
if(appReadingList.size()>0){
adapter=new ContentRecyclerAdapter();
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),R.drawable.divider));
recyclerView.setItemAnimator(new FadeInLeftAnimator());
recyclerView.setAdapter(new SlideInLeftAnimationAdapter(adapter));
}else{
norecord.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
}
}
private class ContentRecyclerAdapter extends RecyclerView.Adapter<ContentRecyclerAdapter.ViewHolder>{
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem, parent, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder,final int position) {
holder.itemContent.setText(appReadingList.get(position).getTitle());
holder.itemDate.setText(appReadingList.get(position).getAddDate());
}
#Override
public int getItemCount() {
return appReadingList.size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView itemContent, itemDate ;
RelativeLayout listItem;
ViewHolder(View itemView){
super(itemView);
itemContent = (TextView) itemView.findViewById(R.id.itemcontent);
itemDate = (TextView) itemView.findViewById(R.id.itemdate);
listItem=(RelativeLayout) itemView.findViewById(R.id.rlListItem);
}
}
}