fragments - how to use them with activities that require actions?

霸气de小男生 提交于 2019-12-02 07:48:53

to hide also ActionBar icons you can do like:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

To replace main fragment when you click an item from the drawable menu list i see you have used selectItem(position) method, however that method is never declared on your code. To do that also you can do something like:

private void selectItem(int position){
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 1:
            fragment = new TestFragment();
            break;
        case 2:
            fragment = new TestFragment2();
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        setTitle(navMenuTitles[position]);
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

I am giving you a example with multiple activities defined as fragment and called using MainActivity, Hope you will get your solution among it..

MainActivity.java

package com.example.fragmentdemo1;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements
        OnItemClickListener {

    MainActivity activity;
    private ListView lv;
    private ArrayList<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;
        lv = (ListView) findViewById(R.id.listView);
        list.add("First");
        list.add("Second");
        list.add("Third");
        list.add("Forth");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
                android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        switch (position) {
        case 0:
            Fragment1 f1 = new Fragment1();
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.addToBackStack(null);
            transaction.replace(R.id.container, f1).commit();
            break;
        case 1:
            Fragment2 f2 = new Fragment2();
            FragmentTransaction transaction2 = getSupportFragmentManager()
                    .beginTransaction();
            transaction2.addToBackStack(null);
            transaction2.replace(R.id.container, f2).commit();

            break;
        case 2:
            Toast.makeText(activity, "" + position, 1000).show();
            Fragment3 f3 = new Fragment3();
            FragmentTransaction transaction3 = getSupportFragmentManager()
                    .beginTransaction();
            transaction3.addToBackStack(null);
            transaction3.replace(R.id.container, f3).commit();
            break;
        case 3:
            Fragment4 f4 = new Fragment4();
            FragmentTransaction transaction4 = getSupportFragmentManager()
                    .beginTransaction();
            transaction4.addToBackStack(null);
            transaction4.replace(R.id.container, f4).commit();
            break;
        default:
            break;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch (id) {
        case android.R.id.home:

            finish();
            break;

        default:
            break;
        }

        return false;
    }

}

Fragment1.java

package com.example.fragmentdemo1;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class Fragment1 extends android.support.v4.app.Fragment{

    TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view,
            Bundle savedInstanceState) {
        tv =(TextView)view.findViewById(R.id.textView1);
        view = (ViewGroup) inflater.inflate(R.layout.fragment1, null);
        return view;
    }
}

Fragment2.java

package com.example.fragmentdemo1;

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

    public class Fragment2 extends Fragment {

        TextView tv;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup view,
                Bundle savedInstanceState) {
            tv =(TextView)view.findViewById(R.id.textView2);
            view = (ViewGroup) inflater.inflate(R.layout.fragment2, null);
            return view;
        }
    }

Fragment3.java

package com.example.fragmentdemo1;

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

    public class Fragment3 extends Fragment {
        TextView tv;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup view,
                Bundle savedInstanceState) {

            tv =(TextView)view.findViewById(R.id.textView3);
            view =(ViewGroup)inflater.inflate(R.layout.fragment3, null);
            return view;
        }
    }

Fragment4.java

package com.example.fragmentdemo1;

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

public class Fragment4 extends Fragment{

    TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup view,
            Bundle savedInstanceState) {
        tv =(TextView)view.findViewById(R.id.textView4);

        view = (ViewGroup)inflater.inflate(R.layout.fragment4, null);
        return view;
    }
}

NOTE : If you want to use method from Fragment class to MainActivity, then you can make it public static, and you can use that method directly by it's class name like Fragment1.countData().

This demo also apply for Navigation drawer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!