Android - 在片段之间传递参数

时间:2014-03-17 08:52:54

标签: android android-actionbar actionbarsherlock

我开发了一个Android应用程序,如图所示, enter image description here

我使用sherlock操作栏进行搜索,唯一的问题是当用户在网页标签中搜索某些内容时,如果他/她想要查看图片结果,他应该在图片标签中再次输入和搜索,有没有办法将edittext参数传递给其他标签,并在用户更改标签时进行搜索?

MainActivity:

public class MainActivity extends SherlockFragmentActivity implements
    TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private com.actionbarsherlock.app.ActionBar actionBar;
private String[] tabs = { "Sound", "Image", "Web" };

@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);  
    actionBar.setDisplayShowHomeEnabled(false);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    actionBar.setHomeButtonEnabled(false);
    viewPager.setAdapter(mAdapter);
    viewPager.setOffscreenPageLimit(3);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.addTab(actionBar.newTab().setText(tabs[0])
            .setTabListener(this), false);
    actionBar.addTab(actionBar.newTab().setText(tabs[1])
            .setTabListener(this), false);
    actionBar.addTab(actionBar.newTab().setText(tabs[2])
            .setTabListener(this), true);

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

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

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
}
}

适配器:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        return new Sound();
    case 1:
        return new Image();
    case 2:
        return new Search();
    }

    return null;
}

@Override
public int getCount() {
    return 3;
}

}

Sound Frgment:

public class Sound extends Fragment {

private String URL_ADDRESS, Servlet;
private ListView listview;
private ListViewAdapter adapter;
private List<Spanned> numberlist = new ArrayList<Spanned>();
private List<Ava2> avaList = new ArrayList<Ava2>();
private Boolean isInternetPresent = false;
private ConnectionDetector detector;
private View rootView;
private RelativeLayout loading, layout;
private int counter = 10;
private EditText editText;
private String a = "http://***";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.ava, container, false);
    ImageButton voiceSearch = (ImageButton) rootView
            .findViewById(R.id.imageButton2);
    ImageButton buttonSearch = (ImageButton) rootView
            .findViewById(R.id.imageButton1);
    listview = (ListView) rootView.findViewById(R.id.listview);
    loading = (RelativeLayout) rootView.findViewById(R.id.loading);
    layout = (RelativeLayout) rootView.findViewById(R.id.layout);
    editText = (EditText) rootView.findViewById(R.id.editText1);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                search();
                return true;
            }
            return false;
        }

    });
    buttonSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            search();
        }
    });

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                int position, long l) {
                try {
                    Intent i = new Intent(getActivity(), Play.class);

                    startActivity(i);
                } catch (Exception e) {
                }
            }
        }
    });

    return rootView;
}

2 个答案:

答案 0 :(得分:1)

首先,您可以编写一个界面:

public interface OnProfileDataPass {
      public String getCurrentSearchword();
      public void setCurrentSearchword(String searchword);
}

然后在你的片段中:

public class WebFragment extends Fragment {
    private OnProfileDataPass dataPasser;

    @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        dataPasser = (OnProfileDataPass) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnProfileDataPass");
    }
    searchword = dataPasser.getCurrentSearchword();
    }
}

在您的活动中:

  public class MainActivity implements OnProfileDataPass {
     String currentSearchword;

 @Override
    public String getCurrentSearchword() {
        return currentSearchword;
    }

 @Override
    public String setCurrentSearchword(String searchword) {
        this.currentSearchword = searchword;
    }


}

这是我的解决方案。当您更改点击时,您可以听取正确的事件并使用getActivity.setCurrentSearchword(String newword)设置该字词,然后您就可以获得片段中的newword&#39}。 s dataPasser变量的方法getCurrentSearchword()来获取搜索词。我在我的应用程序中测试了它。希望能帮到你一点点。

答案 1 :(得分:0)

您应该在活动中保存editText的值,然后在调用片段时将其作为参数传递,使用与此处描述的技术相同的技术:http://developer.android.com/reference/android/app/Fragment.html