android-fragments 通过选项卡查看片段时出现问题

zlhcx6iw  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(173)

我试着贴了一个类似的问题,没有任何答案,虽然再次张贴。
我遇到的问题是通过选项卡查看片段,我在一个AppBar中有3个选项卡显示ViewPager和FragmentPagerAdapter。
问题是当Activity启动时,其中一个选项卡的行为方式不一致,在首次登录应用和Activity时,它将显示片段的内容(视频剪辑),但在应用程序中的Activity之间切换,并返回到包含3个选项卡的Activity期间,选择该特定选项卡不会显示任何内容,即使在Logcat中,我可以告诉它正在检索与应该显示的视频剪辑相关的数据,也可以返回到我在Logcat中看到的活动,即使我选择了不同的选项卡,(图片)也会检索其他2个选项卡中的数据!?
设置如下:
主要活动;

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

好吧,正如一个用户在帖子中指出的那样,问题可能来自于特定的片段本身,而不是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个片段的组织方式相同,但此片段的行为不一致?

r8xiu3jd

r8xiu3jd1#

这是一个带有视图夹的适配器版本,你说它可能会有帮助。你需要重构来适应你的情况。我已经把它拆了,主要是为了帮助只显示裸的交互。

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

    }

}

相关问题