gpt4 book ai didi

android - 通过 tabs 发出查看 Fragment

转载 作者:行者123 更新时间:2023-11-30 01:15:10 25 4
gpt4 key购买 nike

我试过发布类似的问题但没有任何答案,尽管再次发布。

我遇到的问题是通过选项卡查看 fragment ,我在 AppBar 中显示了 3 个选项卡,并显示了 ViewPager 和 FragmentPagerAdapter。

问题是当 Activity 开始时,其中一个选项卡的行为方式不一致,在第一次登录到应用程序和 Activity 时它会显示 fragment 的内容(视频剪辑)但在切换期间在应用程序中的 Activity 之间,然后返回到包含 3 个选项卡的 Activity ,选择特定选项卡不会显示任何内容,尽管在 Logcat 中,我可以看出它正在检索与应该显示的视频剪辑相关的数据,也将继续返回我在 Logcat 中看到的那个 Activity ,即使我选择了不同的选项卡,(图片)正在检索其他 2 个选项卡中的数据!?

设置如下;

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

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

最佳答案

这是我为 recyclerview 准备的带有 viewholder 的适配器版本,您说这可能会有帮助。您需要重构以适应您的情况。我主要是为了帮助展示最基本的互动。

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

}


}

关于android - 通过 tabs 发出查看 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37952200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com