Made fragments work properly.
As this was my first experience with fragments, I didn't respect their lifecycle properly. I think this has solved the issue I was having, where recreating the app list fragments after it was destroid by Android caused them to stuff up.
This commit is contained in:
parent
89facb24c6
commit
4eda47df4b
@ -29,6 +29,7 @@ import android.app.*;
|
||||
import android.os.Build;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.view.PagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
@ -92,6 +93,7 @@ public class FDroid extends FragmentActivity implements OnItemClickListener,
|
||||
|
||||
// Used by pre 3.0 devices which don't have an ActionBar...
|
||||
private TabHost tabHost;
|
||||
private AppListFragmentPageAdapter viewPageAdapter;
|
||||
|
||||
// The following getters
|
||||
// (availableAdapter/installedAdapter/canUpdateAdapter/categoriesAdapter)
|
||||
@ -149,8 +151,8 @@ public class FDroid extends FragmentActivity implements OnItemClickListener,
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
populateLists();
|
||||
super.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -281,7 +283,8 @@ public class FDroid extends FragmentActivity implements OnItemClickListener,
|
||||
|
||||
private void createViews() {
|
||||
viewPager = (ViewPager)findViewById(R.id.main_pager);
|
||||
viewPager.setAdapter(new AppListFragmentPageAdapter(this));
|
||||
viewPageAdapter = new AppListFragmentPageAdapter(this);
|
||||
viewPager.setAdapter(viewPageAdapter);
|
||||
viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener() {
|
||||
public void onPageSelected(int position) {
|
||||
selectTab(position);
|
||||
@ -593,13 +596,15 @@ public class FDroid extends FragmentActivity implements OnItemClickListener,
|
||||
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
|
||||
long arg3) {
|
||||
|
||||
Fragment fragment = ((AppListFragmentPageAdapter)viewPager.getAdapter()).getItem(viewPager.getCurrentItem());
|
||||
int currentItem = viewPager.getCurrentItem();
|
||||
Fragment fragment = viewPageAdapter.getItem(currentItem);
|
||||
|
||||
// The fragment.getView() returns a wrapper object which has the
|
||||
// actual view we're interested in inside:
|
||||
// http://stackoverflow.com/a/13684505
|
||||
AppListView view = ((AppListView)((ViewGroup)fragment.getView()).getChildAt(0));
|
||||
final DB.App app = (DB.App)view.getAppList().getAdapter().getItem(arg2);
|
||||
ViewGroup group = (ViewGroup)fragment.getView();
|
||||
AppListView view = (AppListView)group.getChildAt(0);
|
||||
final DB.App app = (DB.App)view.getAppList(). getAdapter().getItem(arg2);
|
||||
|
||||
Intent intent = new Intent(this, AppDetails.class);
|
||||
intent.putExtra("appid", app.id);
|
||||
|
@ -1,13 +1,18 @@
|
||||
package org.fdroid.fdroid.views;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentPagerAdapter;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import org.fdroid.fdroid.FDroid;
|
||||
import org.fdroid.fdroid.R;
|
||||
import org.fdroid.fdroid.views.fragments.AvailableAppsFragment;
|
||||
import org.fdroid.fdroid.views.fragments.CanUpdateAppsFragment;
|
||||
import org.fdroid.fdroid.views.fragments.InstalledAppsFragment;
|
||||
|
||||
/**
|
||||
* Used by the FDroid activity in conjunction with its ViewPager to support
|
||||
@ -19,28 +24,11 @@ public class AppListFragmentPageAdapter extends FragmentPagerAdapter {
|
||||
private Fragment[] fragments = new Fragment[3];
|
||||
|
||||
public AppListFragmentPageAdapter(FDroid parent) {
|
||||
|
||||
super(parent.getSupportFragmentManager());
|
||||
this.parent = parent;
|
||||
final AppListViewFactory viewFactory = new AppListViewFactory(parent);
|
||||
|
||||
fragments[0] = new Fragment() {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return viewFactory.createAvailableView();
|
||||
}
|
||||
};
|
||||
|
||||
fragments[1] = new Fragment() {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return viewFactory.createInstalledView();
|
||||
}
|
||||
};
|
||||
|
||||
fragments[2] = new Fragment() {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return viewFactory.createCanUpdateView();
|
||||
}
|
||||
};
|
||||
fragments[0] = new AvailableAppsFragment();
|
||||
fragments[1] = new InstalledAppsFragment();
|
||||
fragments[2] = new CanUpdateAppsFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.fdroid.fdroid.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ListView;
|
||||
|
||||
@ -22,6 +23,14 @@ public class AppListView extends LinearLayout {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public AppListView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public AppListView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
public void setAppList(ListView appList) {
|
||||
this.appList = appList;
|
||||
}
|
||||
|
14
src/org/fdroid/fdroid/views/fragments/AppListFragment.java
Normal file
14
src/org/fdroid/fdroid/views/fragments/AppListFragment.java
Normal file
@ -0,0 +1,14 @@
|
||||
package org.fdroid.fdroid.views.fragments;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import org.fdroid.fdroid.FDroid;
|
||||
import org.fdroid.fdroid.views.AppListViewFactory;
|
||||
|
||||
public class AppListFragment extends Fragment {
|
||||
|
||||
protected AppListViewFactory getViewFactory() {
|
||||
FDroid parent = (FDroid)getActivity();
|
||||
return new AppListViewFactory(parent);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.fdroid.fdroid.views.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class AvailableAppsFragment extends AppListFragment {
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return getViewFactory().createAvailableView();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.fdroid.fdroid.views.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class CanUpdateAppsFragment extends AppListFragment {
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return getViewFactory().createCanUpdateView();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package org.fdroid.fdroid.views.fragments;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class InstalledAppsFragment extends AppListFragment {
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return getViewFactory().createInstalledView();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user