如上图所示,我开发了一个android应用程序,


我使用了sherlock操作栏进行搜索,唯一的问题是,如果用户要在Web选项卡中搜索某项内容,如果他/她想查看图像结果,则他应该在“图像”选项卡中键入并再次搜索,是否有任何方法可以传递edittext参数设置为其他标签,并且在用户更改标签时进行搜索?

主要活动:

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;
}

}


声音片段:

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;
}

最佳答案

首先,您可以编写一个接口:

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变量的方法dataPasser中获取getCurrentSearchword()来获取搜索词。我已经在我的应用程序中对其进行了测试。希望对您有所帮助。

10-07 19:15