为Android选项卡操作栏重用Fragment和layout.xml

时间:2012-07-19 21:33:49

标签: android android-listview android-fragments android-actionbar android-tabs

我正在编写我的第一个Android应用程序并遇到问题:(我正在使用Eclipse,该应用程序旨在从Android 4.0.3运行。

我在视图顶部有一个标签操作栏,由4个标签组成。其中三个具有相同的结构:一个带有两个radiobuttons的radiogroup和一个listView。这些选项卡基于相同的数据,将用于过滤数据。 radioButton的文本取决于选项卡,并将进一步过滤数据。

最后一个标签具有完全不同的布局和功能。

我制作了两个片段和两个布局(一个用于前三个,一个用于最后一个选项卡)。第一个片段是管理无线电组和列表视图。我还有一个适配器来设置数据模型,具体取决于选择的选项卡和按钮。但不知何故,我无法让它正常工作。无线电按钮不会刷新,列表仅显示第一次加载活动。没有例外。

我的方法有误吗?

这是我的主要活动:

    @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);

    appContext = getApplicationContext();


    dataholder = (P1DataHolder) getIntent().getSerializableExtra(
            "data"); //Hold all the data

    final ActionBar mainBar = getActionBar();
    mainBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mainBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    // create the fragments with the name of parent tab
    Fragment aFragment = new ListFragment(
            MyApplication.TAB_A);
    Fragment bFragment = new ListFragment(
            MyApplication.TAB_B);
    Fragment cFragment = new ListFragment(
            MyApplication.TAB_C);
    Fragment settingFragment = new P1SettingFragment();

    // Fragment A
    ActionBar.Tab tabA = mainBar.newTab().setText(
            MyApplication.TAB_A);
    tabA.setTabListener(new TabsListener(aFragment));

    // Fragment B
    ActionBar.Tab tabB = mainBar.newTab().setText(
            MyApplication.TAB_B);
    tabB.setTabListener(new TabsListener(bFragment));

    // Fragment C
    ActionBar.Tab tabC = mainBar.newTab().setText(
            MyApplication.TAB_C);
    tabC.setTabListener(new TabsListener(cFragment));

    // Settings
    ActionBar.Tab tabSetting = mainBar.newTab().setText(
            MyApplication.TAB_SETTING);
    tabSetting.setTabListener(new TabsListener(settingFragment));

    // Adding Tabs to actionbar
    mainBar.addTab(tabA);
    mainBar.addTab(tabB);
    mainBar.addTab(tabC);
    mainBar.addTab(tabSetting);

}

编辑:框架的代码

public class ListFragment extends Fragment {

private ArrayList<Recipe> listAll;
private ArrayList<Recipe> listNotCookedYet;
private CustomListView ls;
private SectionComposerAdapter adapter;
private Activity parent;
private String tabTag; // Tab state from Parent
private View view;
private SegmentedRadioGroup segmentText;
private RadioButton rbAll;
private RadioButton rbNotCookedYet;


public ListFragment(String tabTag, ArrayList<Recipe> listAll, ArrayList<Recipe> listNTY) {
    this.tabTag = tabTag;
    this.listAll = listAll;
    this.listNotCookedYet = listNTY;
}

public String getTabTag(){
    return this.tabTag;
}

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

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.list_with_section, container,
            false);
    parent = getActivity();

    // get Elements
    segmentText = (SegmentedRadioGroup) view
            .findViewById(R.id.segment_text);
    rbAll = (RadioButton) view.findViewById(R.id.button_list_all);
    rbNotCookedYet = (RadioButton) view
            .findViewById(R.id.button_list_notcookedyet);

    // Set Content
    if (parent != null && parent instanceof ListMainActivity) {

        ListDataHolder holder = ((ListMainActivity) parent)
                .getDataHolder();

        adapter = new SectionComposerAdapter(parent, holder);
        adapter.getCount();
        initialize(adapter);

        System.out.println("ANZAHL ALL: " + listAll.size());
        System.out.println("ANZAHL UNCOOKED: " + listNotCookedYet.size());

        //The default text is "all (*)" and "not cooked yet (*)"
        rbAll.setText(rbAll.getText().toString()
                .replace("*", listAll.size() + ""));
        rbNotCookedYet.setText(rbNotCookedYet.getText().toString()
                .replace("*", listNotCookedYet.size() + ""));

        segmentText
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(RadioGroup group,
                            int checkedId) {

                        if (rbAll.getId() == checkedId) {
                            adapter.setDataModel(tabTag, true);
                            System.out.println("CHECKED: All");

                        }

                        else if (rbNotCookedYet.getId() == checkedId) {
                            adapter.setDataModel(tabTag, false);
                            System.out.println("CHECKED: Not Yet");
                        }


                    }
                });

    }
    return view;
}

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

}

private void initialize(SectionComposerAdapter adapter) {
    ls = (CustomListView) view
            .findViewById(R.id.list_services);
    ls.setPinnedHeaderView(LayoutInflater.from(parent).inflate(
            R.layout.item_composer_header, ls, false));
    ls.setAdapter(adapter);
    ls.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(view.getContext(),
                    RecipeActivity.class);
            startActivityForResult(intent, 0);

        }
    });
}

ListDataHolder按字母顺序对数据进行了分区,并将它们作为单独的Arraylist保存为每个选项卡和每个单选按钮(因此有6个arraylists)。

这是我的适配器:

public class SectionComposerAdapter extends CustomListAdapter {

private Activity activity;
private ListDataHolder dataholder;
private static LayoutInflater inflater = null;
private ImageLoader imageLoader;
private List<Pair<String, ArrayList<Recipe>>> data;

public SectionComposerAdapter(Activity a, ListDataHolder holder) {
    activity = a;
    this.dataholder = holder;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
    data = dataholder.getAllData(dataholder.getAZRecipeAll());
}

public void setDataModel(String tab, boolean all) {
    if (MyApplication.TAB_A.equals(tab)) {
        if (all) {
            data = dataholder.getAllData(dataholder.getAZRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getAZRecipeNotCookedYet());
        }
    } else if (MyApplication.TAB_B.equals(tab)) {
        if (all) {
            data = dataholder.getAllData(dataholder.getMeatRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getMeatRecipeNotCookedYet());
        }
    } else if (MyApplication.TAB_C.equals(tab)) {
        if (all) {
            data = dataholder
                    .getAllData(dataholder.getVegetarianRecipeAll());
        } else {
            data = dataholder.getAllData(dataholder
                    .getVegetarianRecipeNotCookedYet());
        }
    }


}

@Override
public int getCount() {
    int res = 0;
    for (int i = 0; i < data.size(); i++) {
        res += data.get(i).second.size();
    }
    return res;
}

@Override
public Recipe getItem(int position) {
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (position >= c && position < c + data.get(i).second.size()) {
            return data.get(i).second.get(position - c);
        }
        c += data.get(i).second.size();
    }
    return null;
}

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

@Override
protected void onNextPageRequested(int page) {
}

@Override
protected void bindSectionHeader(View view, int position,
        boolean displaySectionHeader) {
    if (displaySectionHeader) {
        view.findViewById(R.id.list_section_header).setVisibility(
                View.VISIBLE);
        TextView lSectionTitle = (TextView) view
                .findViewById(R.id.list_section_header);
        lSectionTitle
                .setText(getSections()[getSectionForPosition(position)]);
    } else {
        TextView lSectionTitle = (TextView) view
                .findViewById(R.id.list_section_header);
        lSectionTitle
                .setText(getSections()[getSectionForPosition(position)]);
        view.findViewById(R.id.list_section_header)
                .setVisibility(View.GONE);
    }
}

@Override
public View getAmazingView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (vi == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView recipename = (TextView) vi.findViewById(R.id.recipe_name);
    TextView recipevip = (TextView) vi.findViewById(R.id.recipe_fav_mark);
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);

    Recipe recipe = new Recipe();
    recipe = getItem(position);

    // Setting all values in listview
    recipename.setText(recipe.getTitle);
    recipevip.setText(recipe.isFav() ? "FAV" : "");
    imageLoader.DisplayImage(recipe.getImage_thumbnail(), thumb_image);

    return vi;
}

@Override
public void configurePinnedHeader(View header, int position, int alpha) {
    TextView lSectionHeader = (TextView) header;
    lSectionHeader.setText(getSections()[getSectionForPosition(position)]);
    // lSectionHeader.setBackgroundColor(alpha << 24 | (0xbbffbb));
    // lSectionHeader.setTextColor(alpha << 24 | (0x000000));
}

@Override
public int getPositionForSection(int section) {
    if (section < 0)
        section = 0;
    if (section >= data.size())
        section = data.size() - 1;
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (section == i) {
            return c;
        }
        c += data.get(i).second.size();
    }
    return 0;
}

@Override
public int getSectionForPosition(int position) {
    int c = 0;
    for (int i = 0; i < data.size(); i++) {
        if (position >= c && position < c + data.get(i).second.size()) {
            return i;
        }
        c += data.get(i).second.size();
    }
    return -1;
}

@Override
public String[] getSections() {
    String[] res = new String[data.size()];
    for (int i = 0; i < data.size(); i++) {
        res[i] = data.get(i).first;
    }
    return res;
}

没有标签和单选按钮,列表工作正常。听众也工作正常,我看到日志中的标签和单选按钮的变化。我无法让他们正确地合作。

提前感谢您,我希望得到一些提示;)

黛安娜

1 个答案:

答案 0 :(得分:0)

抱歉!