向片段添加动态列表视图

时间:2014-05-20 15:13:10

标签: android listview android-fragments

在我的应用程序中,我需要创建一个动态列表视图,该视图由从db中提取的元素填充。

这本身并不是一个大问题(有很多关于它的教程,比如this one),问题是,我的活动分为标签,因此,我有一个{每个标签{1}}(代码基本上是this)。

有很多方法我无法在Fragment中使用,而且我真的很卡住。

如何在片段中实现动态布局解决方案?

编辑:代码为清晰起见:

片段:

Fragment

的活动:

package it.sii.mywaiter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AllerFragment extends Fragment {

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

        View rootView = inflater.inflate(R.layout.aller_layout, container, false);

        return rootView;
    }
}

适配器

package it.sii.mywaiter;

import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
//import android.widget.*;
import android.app.ActionBar;

public class UserPrefActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    private PrefPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Ingredienti preferiti", "Allergie e Intolleranze" };
    int tabsnum = tabs.length;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_pref_layout);

        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new PrefPagerAdapter(getSupportFragmentManager());
        mAdapter.setCount(tabsnum);

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }


}

片段的布局:

package it.sii.mywaiter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PrefPagerAdapter extends FragmentPagerAdapter {

    private int count = 0;

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

    public void setCount(int count){
        this.count = count;
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            return new PrefFragment();
        case 1:
            return new AllerFragment();
        }
        return null;
    }

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

}

1 个答案:

答案 0 :(得分:0)

正如你所说,你有碎片。每个Fragment都有自己的类,因此您可以创建CustomArrayAdapter并在Fragments onCreate方法中使用它。在Fragment中,使用findViewByID获取您在此片段的xml文件中定义的ListView,然后使用setAdapter(new YourCustomAdapterClass());设置其适配器,然后在片段中拥有自定义动态ListView。