我正在项目中使用Tablayout里面的片段,我有三个标签,他们从网络服务中获取数据,这部分应用程序显示问题,我们有三个类别一个用于最近,第二个用于未答复第三个用于所有问题观点
这是我的情况我的Web开发人员设计我的Web服务返回问题项,它们在返回的项目中共享相同的结构,但数据根据需求而变化。
public class Questions_Fragment extends Fragment {
private TabLayout mTablayout;
private ViewPager mViewpager;
public static Questions_Fragment newInstance() {
Questions_Fragment fragment = new Questions_Fragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.questions_layout, null);
mViewpager = (ViewPager) v.findViewById(R.id.viewPager);
setupViewPager(mViewpager);
mTablayout = (TabLayout) v.findViewById(R.id.tabs);
mTablayout.setupWithViewPager(mViewpager);
return v;
}
private void setupViewPager(ViewPager mViewpager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFrag( QuestionsFragModel.newInstance(VolleyRequestHandler.RECENT_INT) , getResources().getString(R.string.newest));
adapter.addFrag( QuestionsFragModel.newInstance(VolleyRequestHandler.UNANSWER_INT) , getResources().getString(R.string.notanswered));
adapter.addFrag( QuestionsFragModel.newInstance(VolleyRequestHandler.ALLPOSTS_INT) , getResources().getString(R.string.allposts));
mViewpager.setAdapter(adapter);
}
我的视图寻呼机适配器类我扩展FragmentStatePagerAdapter
数据可能会在每次用户打开标签时发生变化
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList= new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fra , String title){
mFragmentList.add(fra);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
但我看到它给了我错误的观点,它显示有问题的图像没有图像,它给出了未答复的问题的答案
对不起我的长问题,但我想知道我的代码有什么问题?
我的列表适配器
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Context _Context;
private List<QuestionItem> Questions = new ArrayList<>();
private API api;
private int num;
private int lastPosition = -1;
public static final String BASE_POST_URl = "url....";
public ListViewAdapter(Context context, List<QuestionItem> Questions) {
this._Context = context;
this.Questions = Questions;
api = new API(context);
}
public ListViewAdapter(Context context, List<QuestionItem> Questions,
int num) {
this._Context = context;
this.Questions = Questions;
this.num = num;
api = new API(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(_Context);
View itemView = inflater.inflate(R.layout.question_row, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Typeface tf = Typeface.createFromAsset(_Context.getAssets(),
"fonts/GE_SS_Two_Bold.otf");
Typeface tfreg = Typeface.createFromAsset(_Context.getAssets(),
"fonts/GE_SS_Two_Light.otf");
ImageLoader _ImageLoader = MySingleton.getInstance(_Context).getmImageLoader();
if (Questions.get(position).user.userimageURL != null) {
if (!Questions.get(position).user.getUserimageURL().isEmpty()) {
//holder.userimageprog.setVisibility(View.VISIBLE);
holder.imageView.setImageUrl(Profile_Fragment.BASE_URL + Questions.get(position).user.getUserimageURL(), _ImageLoader);
}
}
if(Questions.get(position).Que_image_url != null){
if (!Questions.get(position).getQue_image_url().isEmpty()) {
holder._FrameLayout.setVisibility(View.VISIBLE);
holder.post_img.setVisibility(View.VISIBLE);
//holder.post_img_pro.setVisibility(View.VISIBLE);
holder.post_img.setImageUrl(BASE_POST_URl + Questions.get(position).getQue_image_url(), _ImageLoader);
}
}
holder.imageView.setDefaultImageResId(R.drawable.profile);
holder.username.setTypeface(tf);
holder.desc.setTypeface(tfreg);
holder.Date.setTypeface(tfreg);
holder.AnswersCount.setTypeface(tfreg);
if(Questions.get(position).user.username != null){
holder.username.setText(Questions.get(position).user.username);
}
if(Questions.get(position).Que_Content != null){
holder.desc.setText(Questions.get(position).Que_Content);
}
if(Questions.get(position).category != null){
holder.Category.setText(Questions.get(position).category);
}
if(Questions.get(position).Que_Date != null){
holder.Date.setText(Questions.get(position).Que_Date);
}
if (!Questions.get(position).getComment().isEmpty()) {
holder.arrow.setVisibility(View.VISIBLE);
holder.AnswersCount.setVisibility(View.VISIBLE);
holder.AnswersCount.setText(_Context.getResources().getString(R.string.youhaveanswer));
}
holder.arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (_Context , ViewAnswers.class);
intent.putExtra("passedobject", (Parcelable) Questions.get(position));
_Context.startActivity(intent);
}
});
// Animation animation = null;
// animation = AnimationUtils.loadAnimation(_Context,
// R.anim.up_from_bottom);
// animation.setDuration(500);
// view.setAnimation(animation);
// animation = null;
//
// Animation animation = AnimationUtils.loadAnimation(_Context,
// (pos > lastPosition) ? R.anim.up_from_bottom
// : R.anim.down_from_top);
// view.startAnimation(animation);
// lastPosition = pos;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return Questions.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected NetworkImageView imageView, post_img;
protected TextView desc, Category, username, Date, AnswersCount;
protected ProgressBar userimageprog, post_img_pro;
protected ImageView arrow;
protected FrameLayout _FrameLayout;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.ques_user_img);
desc = (TextView) itemView.findViewById(R.id.ques_content);
Category = (TextView) itemView.findViewById(R.id.category);
post_img = (NetworkImageView) itemView.findViewById(R.id.post_img);
userimageprog = (ProgressBar) itemView.findViewById(R.id.userimgae_prog);
post_img_pro = (ProgressBar) itemView.findViewById(R.id.post_prog);
username = (TextView) itemView.findViewById(R.id.username_ques);
Date = (TextView) itemView.findViewById(R.id.date);
AnswersCount = (TextView) itemView.findViewById(R.id.answers_con);
arrow = (ImageView) itemView.findViewById(R.id.imageView1);
_FrameLayout = (FrameLayout)itemView.findViewById(R.id.frameLayout2);
}
}
}
QuestionsFragModel视图
public class QuestionsFragModel extends Fragment implements OnClickListener {
private RecyclerView Ques_list;
private ProgressBar list_prog;
private ListViewAdapter adapter;
private List<QuestionItem> Questions = new ArrayList<>();
private TextView reload;
private VolleyRequestHandler _VolleyRequestHandler;
private int CONST_TYPE = 0;
public static final String PASSED_INT = "passed_number";
private CoordinatorLayout _coordiatelayout;
static QuestionsFragModel newInstance(int type) {
QuestionsFragModel f = new QuestionsFragModel();
Bundle bundle = new Bundle();
bundle.putInt(PASSED_INT , type);
f.setArguments(bundle);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.question_layout, container, false);
CONST_TYPE = getArguments() != null ? getArguments().getInt(PASSED_INT) : 7;
_VolleyRequestHandler = new VolleyRequestHandler(getActivity());
_coordiatelayout = (CoordinatorLayout) v.findViewById(R.id.coordiatelayout);
Ques_list = (RecyclerView) v.findViewById(R.id.questionlist);
Ques_list.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new ListViewAdapter(getActivity(), Questions);
Ques_list.setAdapter(adapter);
list_prog = (ProgressBar) v.findViewById(R.id.progressbar);
reload = (TextView) v.findViewById(R.id.reload);
getQuestationsPosts();
reload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View position) {
getQuestationsPosts();
}
});
final FloatingActionButton floatingView = (FloatingActionButton) v.findViewById(R.id.FloatingView);
if (floatingView != null) {
floatingView.setOnClickListener(this);
}
return v;
}
public void getQuestationsPosts() {
API api = new API(getActivity());
if (api.isNetworkConnected()) {
list_prog.setVisibility(View.VISIBLE);
reload.setVisibility(View.GONE);
FetchQuestationsFromWeb();
} else {
Snackbar.make(_coordiatelayout, getResources().getString(R.string.nointernet), Snackbar.LENGTH_LONG).show();
Ques_list.setVisibility(View.GONE);
reload.setVisibility(View.VISIBLE);
}
}
private void onSuccessResponse(Object response){
list_prog.setVisibility(View.GONE);
reload.setVisibility(View.GONE);
Ques_list.setVisibility(View.VISIBLE);
if(Questions != null){
Questions.clear();
}
Questions = (List < QuestionItem>) response;
adapter = new ListViewAdapter(getActivity() ,Questions);
Ques_list.setAdapter(adapter);
}
private void onErrorResponse(String error){
list_prog.setVisibility(View.GONE);
reload.setVisibility(View.VISIBLE);
Snackbar.make(_coordiatelayout , error , Snackbar.LENGTH_LONG).show();
}
private void FetchQuestationsFromWeb() {
_VolleyRequestHandler.HandleRequests(Request.Method.POST , CONST_TYPE, null, new VolleyResponseCallBack() {
@Override
public void RequestSuccess(Object response) {
onSuccessResponse(response);
}
@Override
public void RequestFailed(String error) {
onErrorResponse(error);
}
});
}
@Override
public void onClick(View arg0) {
startActivity(new Intent(getActivity(), Add_Question.class));
}
}
答案 0 :(得分:1)
当您只有3个片段时,我不知道为什么您使用FragmentStatePagerAdapter
代替FragmentPagerAdapter
。
如果您有大量碎片,AFAIK FragmentStatePagerAdapter
会提高性能,因为它不会将整个碎片存储在内存中。
无论如何,FragmentStatePagerAdapter
会在被销毁时从savedInstanceState中保存你的片段的Bundle。当用户导航回来时,将使用片段的状态恢复新片段。
所以你需要先检查savedInstanceState
:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.question_layout, container, false);
if (savedInstanceState!=null){
// restore your state from "savedInstanceState"
} else{
CONST_TYPE = getArguments() != null ? getArguments().getInt(PASSED_INT) : 7;
}
}
答案 1 :(得分:1)
如果有人遇到某些问题,我会在适配器中使用if语句,只是你可以使用if if without else
示例I仅在
时使用if (Questions.get(position).user.userimageURL != null) {
if (!Questions.get(position).user.getUserimageURL().isEmpty()) {
//holder.userimageprog.setVisibility(View.VISIBLE);
holder.imageView.setImageUrl(Profile_Fragment.BASE_URL + Questions.get(position).user.getUserimageURL(), _ImageLoader);
}
} else{
// I should use else statement as well
}