初学者,维护片段状态使用导航抽屉切换时

时间:2016-06-08 17:01:32

标签: java android android-fragments

我是java和Android开发的初学者,但设法创建了一个导航抽屉活动,它使用AsycTask在ListViews之间切换五个片段。当我在片段之间切换时,AsycTask再次启动。我在这里经历了很多帖子,但似乎都没有效果。任何帮助。

这是我的导航抽屉活动

public class Home extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {


private static String website = "https://www.website.com", fbPage = "https://www.facebook.com",
        twitter = "https://twitter.com",

private android.support.v4.app.FragmentManager fragmentManager;
private android.support.v4.app.FragmentTransaction fragmentTransaction;


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

        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_holder, new HomeFragment(), "Home Fragment")
                .commit();

    fragmentManager = getSupportFragmentManager();

}

private void customNavgation() {
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(0).setChecked(true);
}

}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {

        HomeFragment homeFragment = new HomeFragment();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_holder, homeFragment, "HomeFragment");
        fragmentTransaction.commit();

    } else if (id == R.id.nav_message) {

        MessageFragment messageFragment = new MessageFragment();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_holder, messageFragment, "MessageFragment");
            fragmentTransaction.commit();

    } else if (id == R.id.nav_word) {

        WordFragment wordFragment = new WordFragment();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_holder, wordFragment, "WordFragment");
        fragmentTransaction.commit();

    } else if (id == R.id.nav_events) {

        EventsFragment eventsFragment = new EventsFragment();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_holder, eventsFragment, "EventsFrangment");
            fragmentTransaction.commit();

    } else if (id == R.id.nav_service) {

        ServiceFragment serviceFragment = new ServiceFragment();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_holder, serviceFragment, "Service Fragment");
        fragmentTransaction.commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

这是我的片段类之一

public class EventsFragment extends Fragment {

ArrayList<EventsFields> eventsFieldsList;
private ListView eventlist;

static final String EVENT_LINK = "http:www.website.com";
private ProgressBar pb;
private View view;


public EventsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_events, container, false);

        eventlist = (ListView) view.findViewById(R.id.event_frag_listView);
        pb = (ProgressBar) view.findViewById(R.id.progressBar);
        pb.setVisibility(View.INVISIBLE);

        if (isOnline()) {
            requestData(EVENT_LINK);
        } else {
            Toast.makeText(EventsFragment.this.getActivity(), "No Internet Connection", Toast.LENGTH_LONG).show();
        }

    return view;
}


protected void updateDisplay() {
    EventsListAdapter adapter = new EventsListAdapter(EventsFragment.this.getActivity(), R.layout.event_list_layout, eventsFieldsList);
    eventlist.setAdapter(adapter);
    eventlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(EventsFragment.this.getActivity(), EventDetail.class);
            EventsFields selectedEvent = eventsFieldsList.get(position);
            intent.putExtra("selectedEvent", selectedEvent);
            startActivity(intent);
        }
    });
}


private void requestData(String uri) {
    MyTask task = new MyTask();
    task.execute(uri);
}


protected boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netinfo = cm.getActiveNetworkInfo();

    if (netinfo != null && netinfo.isConnectedOrConnecting()) {
        return true;
    } else {
        return false;
    }
}

public class MyTask extends AsyncTask<String, String, ArrayList<EventsFields>> {

    @Override
    protected void onPreExecute() {
        pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected ArrayList<EventsFields> doInBackground(String... params) {

        String data = HttpManager.getData(params[0]);
        eventsFieldsList = EventsJsonParser.parseFeed(data);

        return eventsFieldsList;
    }

    @Override
    protected void onPostExecute(ArrayList<EventsFields> results) {


        pb.setVisibility(View.INVISIBLE);

        if (results == null) {
            Toast.makeText(EventsFragment.this.getActivity(), "Web service not available", Toast.LENGTH_LONG).show();
            return;
        }
        eventsFieldsList = results;
        updateDisplay();
    }

}

JsonPaser Class

public class EventsJsonParser {

public static ArrayList<EventsFields> parseFeed(String content) {

    try {

        JSONArray ar = new JSONArray(content);
        ArrayList<EventsFields> eventList = new ArrayList<>();

        for (int i = 0; i < ar.length(); i++) {

            JSONObject obj = ar.getJSONObject(i);

            EventsFields events = new EventsFields(null);

            events.setId(obj.getInt("id"));
            events.setEventName(obj.getString("EventName"));
            events.setStartDate(obj.getString("StartDate"));
            events.setEventDesc(obj.getString("EventDesc"));
            events.setTime(obj.getString("Time"));
            events.setVenue(obj.getString("Venue"));
            eventList.add(events);
        }

        return eventList;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

由于

1 个答案:

答案 0 :(得分:0)

一种方法是保留数据,如果您已经收到数据,则不要调用AsyncTask

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList("eventsFieldsList", eventsFieldsList);
    super.onSaveInstanceState(outState);
}

然后在onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_events, container, false);

        eventlist = (ListView) view.findViewById(R.id.event_frag_listView);
        pb = (ProgressBar) view.findViewById(R.id.progressBar);
        pb.setVisibility(View.INVISIBLE);

        if (savedInstanceState != null) {
            eventsFieldsList = savedInstanceState.getParcelableArrayList("eventsFieldsList");
        }

        if (eventsFieldList == null) {
            if (isOnline()) {
                requestData(EVENT_LINK);
            } else {
                Toast.makeText(EventsFragment.this.getActivity(), "No Internet Connection", Toast.LENGTH_LONG).show();
            }
        }

    return view;
}

您需要制作EventsFields班级Parcelable。这里有很多在线教程和SO答案。如果您在实施Parcelable时遇到问题,请发布新问题。