Java Android Fragment ListView

我有一个片段,我有一个列表视图这是我的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dip"
        android:drawSelectorOnTop="false"
        android:visibility="visible" />

</FrameLayout>

接下来我创建一个片段(活动):

@Override public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){//膨胀这个片段的布局

View view = inflater.inflate(R.layout.activity_fragment_all_object, container,false);

listView = (ListView) view.findViewById(R.id.list);

Integer iloscObiektow = Singleton.getInstance().getListaODE().size();

int i, num_squares = iloscObiektow;
RowBean RowBean_data[] = new RowBean[num_squares];
for (i = 0; i < RowBean_data.length; i++) {
    RowBean_data[i] = new RowBean(Singleton.getInstance().getListaODE().get(i).visible, Singleton.getInstance().getListaODE().get(i).name);
}


AllObjectAdapter a = new AllObjectAdapter(getActivity(),RowBean_data);
listView.setAdapter(a);

return view;

}

这是我的AllObjectAdapter:

public class AllObjectAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private RowBean data[];

    public AllObjectAdapter(Context context, RowBean[] data) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        return data[position];
    }

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

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        final ViewHolder holder;
        RowBean rowBean = data[position];

        if(view == null){
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.all_object_holder, null);
            holder.name = (TextView) view.findViewById(R.id.showText);
            holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        holder.name.setText(rowBean.getTitle());

            return null;
    }

    public class ViewHolder {

        CheckBox checkBox;
        TextView name;
    }
}

我有一个片段和一个活动。 我想显示标签(片段)的名称,但我不想在actionBar中显示应用程序的名称:这是我的活动:当我删除一个ActionBarActivity时,我无法使用FragmentManager

public class ObjectListActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener  {

    private ViewPager viewPager;
    private android.support.v7.app.ActionBar actionBar;


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

        // View pager for showing many fragments over a single activity
        viewPager = (ViewPager) findViewById(R.id.pager);

        // Getting fragment manager to control fragments
        FragmentManager fragmnetManager = getSupportFragmentManager();

        // Setting adapter over view pager
        viewPager.setAdapter(new MyAdapter(fragmnetManager));

        // Implementing view pager pagechangelistener to navigate between tabs
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {

                // Setting navigation of tabs to actionbar
                actionBar.setSelectedNavigationItem(pos);
            }

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

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        // Getting actionbar
        actionBar = getSupportActionBar();

        // Setting navigation mode to actionbar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Now adding a new tab to action bar and setting title, icon and
        // implementing listener
        android.support.v7.app.ActionBar.Tab tab1 = actionBar.newTab();
        tab1.setText("Wszytskie");
        // tab1.setIcon(R.drawable.ic_launcher);
        tab1.setTabListener(this);

        android.support.v7.app.ActionBar.Tab tab2 = actionBar.newTab();
        tab2.setText("Grupy");
        tab2.setTabListener(this);

        // Now finally adding all tabs to actionbar
        actionBar.addTab(tab1);
        actionBar.addTab(tab2);

    }

    @Override
    public void onTabReselected(android.support.v7.app.ActionBar.Tab arg0,
                                FragmentTransaction arg1) {

    }

    @Override
    public void onTabSelected(android.support.v7.app.ActionBar.Tab tab,
                              FragmentTransaction arg1) {

        // Setting current position of tab to view pager
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(android.support.v7.app.ActionBar.Tab arg0,
                                FragmentTransaction arg1) {

    }
}



   class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int i) {

            // Getting fragments according to selected position
            Fragment fragment = null;
            if (i == 0) {
                fragment = new FragmentAllObjectActivity();
            }
            if (i == 1) {
                fragment = new FragmentGroupObjectsActivity();
            }

            // and finally returning fragments
            return fragment;
        }

        @Override
        public int getCount() {

            // Returning no. of counts of fragments
            return 2;
        }
    } 

您在适配器的getView()方法中返回null

将其更改为返回view对象

@Override
public View getView(int position, View view, ViewGroup parent) {

    final ViewHolder holder;
    RowBean rowBean = data[position];

    if(view == null){
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.all_object_holder, null);
        holder.name = (TextView) view.findViewById(R.id.showText);
        holder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
    }else {
        holder = (ViewHolder) view.getTag();
    }
    holder.name.setText(rowBean.getTitle());

    return view;  //change here
}

删除操作栏使用:

// Getting actionbar
    actionBar = getSupportActionBar();
    actionBar.hide();  //add this
链接地址: http://www.djcxy.com/p/81417.html

上一篇: Java Android Fragment ListView

下一篇: "Couldn't add widget" in Fragment