我有2个片段,都与数组适配器一起显示它们的类别。第一个片段是显示项目类别,第二个片段是显示作业类别。但是,它在两个片段中都显示作业类别。我不知道哪一部分出了问题。

这是我的ViewPager,用于在用户选择相应的选项卡时显示片段。从数据库接收数据时将调用setCategory()。

public class ViewPagerAdapter extends FragmentPagerAdapter {

private List<Category> categoryList = new ArrayList();
private DisplayItemFragment itemFragment;
private DisplayJobFragment jobFragment;

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Log.d("categoryList", categoryList.size()+" ");
    if (position ==0) {
        itemFragment = new DisplayItemFragment(categoryList);
        return itemFragment;
    }else{
        jobFragment = new DisplayJobFragment(categoryList);
        return jobFragment;
    }

}

@Override
public int getCount() {
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position){
        //
        //Your tab titles
        //
        case 0:return "Item";
        case 1:return "Job";
        default:return null;
    }
}

public void setCategory(List<Category> list){
    List<Category> tempList = new ArrayList();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("item")){
            tempList.add(list.get(i));
        }
    }
    itemFragment.update(tempList);
    Log.d("this.categoryListitem",tempList.size()+"");
    tempList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("job")){
            tempList.add(list.get(i));
        }
    }
    jobFragment.update(tempList);
    Log.d("this.categoryListjob",tempList.size()+"");
}
}


这是显示项目类别的片段

public class DisplayItemFragment extends Fragment {

private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private CategoryGridAdapter categoryAdapter;
private TextView text;

public DisplayItemFragment(List<Category> categoryList) {
    this.categoryList=categoryList;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_display, container, false);
    text = (TextView) rootView.findViewById(R.id.test);
    categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
    gridView = (GridView) rootView.findViewById(R.id.gridview);
    gridView.setAdapter(categoryAdapter);
    gridView.setTextFilterEnabled(true);

    //Log.d("category",categoryList.get(0).getName());
    return rootView;
}

public void update(List<Category> list) {
    this.categoryList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("item")){
            this.categoryList.add(list.get(i));
        }
    }
    text.setText("item refresh");
    for(int i=0;i<this.categoryList.size();i++){
        Log.d("categoryListitem",categoryList.get(i).getName());
    }
    }


这是显示作业类别的片段

public class DisplayJobFragment extends Fragment {

private View rootView;
private GridView gridView;
private List<Category> categoryList;
private DisplayFragmentPresenter presenter;
private TextView text;
private CategoryGridAdapter categoryAdapter;

public DisplayJobFragment(List<Category> categoryList) {
    this.categoryList=categoryList;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_display, container, false);
    text = (TextView) rootView.findViewById(R.id.test);
    categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, categoryList);
    gridView = (GridView) rootView.findViewById(R.id.gridview);
    gridView.setAdapter(categoryAdapter);
    gridView.setTextFilterEnabled(true);

    //Log.d("category",categoryList.get(0).getName());
    return rootView;
}

public void update(List<Category> list) {
    this.categoryList.clear();
    for(int i=0;i<list.size();i++){
        if(list.get(i).getType().equals("job")){
            this.categoryList.add(list.get(i));
        }
    }
    text.setText("job refresh");
    //categoryAdapter = new CategoryGridAdapter(getActivity(), R.layout.fragment_display, this.categoryList);
    for(int i=0;i<this.categoryList.size();i++){
        Log.d("categoryListjob",categoryList.get(i).getName());
    }

}
}


两个片段都将调用CategoryGridAdapter以在gridview上显示类别

public class CategoryGridAdapter extends ArrayAdapter<Category> {

private List<Category> categoryList;
private Context context;

public CategoryGridAdapter(Context context, int resource, List<Category> categoryList) {
    super(context, resource, categoryList);
    this.context = context;
    this.categoryList = categoryList;
}

@Override
public boolean isEnabled(int position) {
    return true;
}

@Override
public int getCount() {
    return ((null != categoryList) ?
            categoryList.size() : 0);
}

@Override
public Category getItem(int position) {
    return ((null != categoryList) ?
            categoryList.get(position) : null);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (null == view) {
        view = layoutInflater.inflate(R.layout.grid_category, null);
    }

    final Category category = categoryList.get(position);
    Log.d("categorygridlist",category.getName()+" "+position+ " " + categoryList.size());

    if (category != null) {
        final CardView categoryGridLayout = (CardView) view.findViewById(R.id.category_gridlayout);
        final TextView categoryName = (TextView) view.findViewById(R.id.category_name);
        final ImageView categoryIcon = (ImageView) view.findViewById(R.id.category_icon);

        categoryName.setText(category.getName());
        categoryName.setSelected(true);
        try {
            byte[] decodedString = Base64.decode(category.getImage(), Base64.DEFAULT);
            BitmapFactory.Options options=new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
            options.inPurgeable = true; // inPurgeable is used to free up memory while required
            Bitmap decodedByte1 = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);//Decode image, "thumbnail" is the object of image file
            Bitmap decodedByte = Bitmap.createScaledBitmap(decodedByte1, 50 , 50 , true);// convert decoded bitmap into well scalled Bitmap format.

            categoryIcon.setImageBitmap(decodedByte);
        } catch (Exception e) {
            e.printStackTrace();
        }

        categoryGridLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "worked", Toast.LENGTH_LONG).show();
            }
        });
    }

    return view;
}


我希望当用户选择项目选项卡时,它将在视图寻呼机上显示项目类别,而当用户选择作业选项卡时,它将在视图寻呼机上显示作业类别,但它仅显示作业类别。有人对此有解决方案吗?谢谢

最佳答案

ViewPagerAdapter类中,您要传递一个List<Category> categoryList到两个Fragment的构造函数中,并且每个Fragment都将与其数据集设置相同的列表。您在setCategory()update()方法中拥有的日志打印看起来只是正确的,因为更新是按顺序进行的,并且您要清除之间的列表。

不必让该List传递给每个Fragment,而是让每个创建自己的新ArrayList<Category>,并从每个构造函数中删除该参数。

10-08 15:30