Java Android Fragment ListView
I Have a fragment in which I have a list view this is my layout :
<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>
Next I create a fragment (activity) :
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment
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;
}
And this is my 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;
}
}
I have a fragment and an activity . I want to display name of tags(fragments) but I don't want to show name of application in actionBar : this is my activity : When I remove a ActionBarActivity I can not use a 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;
}
}
your are returning null in your getView()
method of the adapter
Change it to return the view
object
@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
}
To remove actionbar use:
// Getting actionbar
actionBar = getSupportActionBar();
actionBar.hide(); //add this
链接地址: http://www.djcxy.com/p/81418.html
上一篇: 媒体控制器分段