草庐IT

android - fragment 的单独搜索 View

coder 2023-11-30 原文

我有一个简单的 Activity ,它包含一个标签栏来切换两个 fragment 。这两个 fragment 都是一个 listFragment 并实现了一个搜索 View ,以便在 listfragment 中进行搜索。搜索 View 始终显示在标签栏上方的操作栏中。

我遇到的问题是,一旦我切换选项卡(转到其他 fragment ),搜索 View 的输入不会重置。因此,第二个 fragment 从搜索 View 读取输入并相应地过滤列表 fragment ,这实际上读取了我还在 fragment 一时输入的输入。

我想要的是搜索 View 成为两个 fragment 的单独搜索 View 。有办法实现吗?

这是我的代码:

Activity

public class ActivityMainApp extends Activity implements ActionBar.TabListener {

private FragmentManager fragmentManager = getFragmentManager();

@Override
public void onCreate(Bundle icicle) {

    super.onCreate(icicle);
setContentView(R.layout.mainapp);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Add tabs
    ActionBar.Tab relatieTab = actionBar.newTab().setText("Relaties");
    ActionBar.Tab takenTab = actionBar.newTab().setText("Taken");
    //ActionBar.Tab urenTab = actionBar.newTab().setText("Uren");

    // Listeners 
    relatieTab.setTabListener(this);
    takenTab.setTabListener(this);

    // Tabs toevoegen aan actionbar
actionBar.addTab(relatieTab);
actionBar.addTab(takenTab);

    // Create fragmentmanager to switch fragments
    FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
    Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_content);

    if(fragment == null){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.fragment_content, new FragRelaties());
    }

}

public void onTabSelected(Tab tab, FragmentTransaction ft) {

    FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();

    if(tab.getText().equals("Taken")){
        FragTaken fragment = new FragTaken();
        fragmentTransaction.replace(R.id.fragment_content, fragment);
        fragmentTransaction.commit();
    }

    if(tab.getText().equals("Relaties")){
        FragRelaties fragment = new FragRelaties();
        fragmentTransaction.replace(R.id.fragment_content, fragment);
        fragmentTransaction.commit();
    }

}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {

}

public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

}

fragment 一

public class FragRelaties extends ListFragment implements SearchView.OnQueryTextListener {

private LayoutInflater inflater;
private ModelRelatie modelRelatie;
private AdapterRelatie relatieAdapter;
public ArrayList<Relatie> relaties;

public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        this.inflater = inflater;

        Activity activity = getActivity();
        final Context context = activity.getApplicationContext();

        setHasOptionsMenu(true);


        new AsyncTask<Void, Void, ArrayList<Relatie>>(){

            protected ArrayList<Relatie> doInBackground(Void... params) {

                // Get all my contacts
                modelRelatie = ModelRelatie.instantiate(context);
                ArrayList<Relatie> relaties = modelRelatie.getRelaties();

                return relaties;

            }
            protected void onPostExecute(ArrayList<Relatie> relaties) {

                // Initial input of objects in the list
                relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
                setListAdapter(relatieAdapter);

            }

        }.execute();

        View view = inflater.inflate(R.layout.relaties, container, false);

        return view;

}

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {

inflater.inflate(R.menu.relatie_menu, menu);

            // Get the searchview
    MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragrelatie);
    SearchView zoekview = (SearchView) zoekveld.getActionView();

    // Execute this when searching
    zoekview.setOnQueryTextListener(this);

    super.onCreateOptionsMenu(menu, inflater);

}

public void onListItemClick(ListView l, View v, int position, long id) {
   // Things that happen when i click on an item in the list
}

public boolean onQueryTextSubmit(String query) {
    return true;
}

public boolean onQueryTextChange(String zoekterm) {

    Activity activity = getActivity();
    Context context = activity.getApplicationContext();

    // We start searching for the name entered
    ModelRelatie modelRelatie = ModelRelatie.instantiate(context);
    ArrayList<Relatie> relaties = modelRelatie.zoekRelatie(zoekterm);

    // The returned objects are placed in the list
    this.relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
    setListAdapter(relatieAdapter);

    return true;

}

fragment 二

public class FragTaken extends ListFragment implements SearchView.OnQueryTextListener {

private LayoutInflater inflater;
private AdapterTaak adapterTaak;
private ModelTaken modelTaken;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        this.inflater = inflater;

        Activity activity = getActivity();
        final Context context = activity.getApplicationContext();

        setHasOptionsMenu(true);

        this.modelTaken = ModelTaken.instantiate(context);
        ArrayList<Taak> taken = modelTaken.getTaken();

        // Initial input for the list
        adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
        setListAdapter(adapterTaak);

        View view = inflater.inflate(R.layout.taken, container, false);
        return view;

}

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {

inflater.inflate(R.menu.taken_menu, menu);

    // get the searview
    MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragtaken);
    SearchView zoekview = (SearchView) zoekveld.getActionView();

    // Execute this when searching
    zoekview.setOnQueryTextListener(this);

    super.onCreateOptionsMenu(menu, inflater);

}

public boolean onQueryTextSubmit(String query) {
    return true;
}

public boolean onQueryTextChange(String zoekterm) {

    Activity activity = getActivity();
    Context context = activity.getApplicationContext();

    // Search the task by the inputed value
    ModelTaken modelTaken = ModelTaken.instantiate(context);
    ArrayList<Taak> taken = modelTaken.zoekTaak(zoekterm);

    // Return the found items to the list
    this.adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
    setListAdapter(adapterTaak);

    return true;

}

}

除了搜索部分外,这两个 fragment 几乎完全相同。

最佳答案

已经有一段时间了,但如果有人发现同样的问题, 这是我设法做到这一点的方法。

只需在您的每个 fragment 中实现 onPrepareOptionsMenu() 并且 在里面,调用 onQueryTextChange(""); 将“”作为查询字符串传递。

关于android - fragment 的单独搜索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493457/

有关android - fragment 的单独搜索 View的更多相关文章

  1. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  6. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  7. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  8. ruby-on-rails - 使用包含多个关联和单独的条件 - 2

    我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS

  9. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  10. ruby - 如何搜索有用的 ruby - 2

    寻找有用的ruby的好网站是什么? 最佳答案 AgileWebDevelopment列出插件(虽然不是ruby​​gems,我不确定为什么),并允许人们对它们进行评级。RubyToolbox按类别列出gem并比较它们的受欢迎程度。Rubygems有一个搜索框。StackOverflow对最有用的rails插件和ruby​​gems有疑问。 关于ruby-如何搜索有用的ruby,我们在StackOverflow上找到一个类似的问题: https://stacko

随机推荐