通过选项卡查看片段

时间:2016-06-21 18:41:26

标签: android android-fragments tabs

我试图在没有任何答案的情况下发布类似的问题,但是再次发布。

我遇到的问题是通过Tabs查看Fragment,我在ViewPar&显示的AppBar中有3个Tabs。 FragmentPagerAdapter。

问题是当活动开始时,其中一个选项卡在登录到应用程序时没有以一致的方式运行。活动第一次它会显示片段的内容(视频剪辑),但在应用程序中的活动之间切换,并返回到包含3个选项卡的活动,选择特定的选项卡不显示任何内容,在Logcat中,我可以告诉它正在检索与应该显示的Vid Clip相关的数据,也回到我在Logcat中看到的Activity,即使我选择了不同的Tab,(图片)正在检索来自其他2个Tab的数据! ?

设置如下;

主要活动;

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

好吧,正如其中一位评论帖子的用户指出,问题可能来自特定的fragemnt本身而不是Tabs和Viewpager跟随的方式是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;
    }
}

}

代码的某些部分(与从firebase检索数据有关)被省略以缩短它。

虽然其他2个片段的组织方式相同,但这个片段的行为是不一致的?

1 个答案:

答案 0 :(得分:1)

以下是带有viewholder的适配器版本,我知道这对您说可能有帮助的recyclerview。您需要重构以根据您的情况进行调整。我主要是为了帮助展示裸露的互动而扼杀它。

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

    }


}