madnomad10011
madnomad10011

Reputation: 341

Issue viewing Fragment through tabs

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 ?

Upvotes: 1

Views: 569

Answers (1)

user3331142
user3331142

Reputation: 1262

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

    }


}

Upvotes: 1

Related Questions