草庐IT

android - PagerSlidingTabStrip:如何在运行时刷新当前选项卡中的内部 fragment Listview 并停止为下一个选项卡加载数据

coder 2023-12-03 原文

我陷入了与内部 fragment insideNavigation-drawer-page-sliding-tab-strip viewpager 选项卡一起使用的 ListView 的问题,在 This git hub example 中给出。 .

我正在使用相同的示例,所有 4 个选项卡都有一个 ListView ,其中不同的数组列表设置为那里的适配器。

我只使用一个 fragment 和选项卡位置的基础我正在将不同的列表数据加载到内部 fragment arrayAdapter。我还有两个按钮,一个是删除,另一个是添加。

我想要的:如果我按下添加按钮,那么它应该将新数据添加到数组列表(基于选项卡位置将新添加添加到相应的数组列表)并刷新 ListView 数据。

在我的代码中,它刷新了当前查看选项卡,但在下一个选项卡 ListView 中也刷新并获取了前一个选项卡的数据。请帮助我解决这个问题,我们将不胜感激。

我有一个类文件。如果我的方法有误,请告诉我正确的方法。

公共(public)类 PageSlidingTabStripFragment 扩展 fragment {

public static final String TAG = PageSlidingTabStripFragment.class
        .getSimpleName();

private MyListAdapter myAdapter;

private boolean isDeleteBtnClicked = false;
private int tabType = 0;
private ListView myListView;

public static PageSlidingTabStripFragment newInstance() {
    return new PageSlidingTabStripFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.pager, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view
            .findViewById(R.id.tabs);
    ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
    MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
    pager.setAdapter(adapter);
    tabs.setViewPager(pager);

}

public class MyPagerAdapter extends FragmentPagerAdapter {

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

    private final String[] TITLES = { "Categories", "Home", "Top Paid",
    "Top Free" };

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public SherlockFragment getItem(int position) {

        return new SuperAwesomeCardFragment().newInstance(position); // here I am calling the fragment by sending the position
    }

}
private ArrayList<String> categoriesList = new ArrayList<String>(); 
private ArrayList<String> homeList = new ArrayList<String>();
private ArrayList<String> topPaidList = new ArrayList<String>();
private ArrayList<String> topFreeList = new ArrayList<String>();

@SuppressLint("ValidFragment")
public class SuperAwesomeCardFragment extends SherlockFragment{

    private final String ARG_POSITION = "position";

    private int position;

    private Button deleteBtn;
    private Button addBtn;

    public SuperAwesomeCardFragment newInstance(int position) {
        SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    }

    public SuperAwesomeCardFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        categoriesList.clear();
        // adding element to Categories arraylist
        categoriesList.add("categories -1");
        categoriesList.add("categories -2");
        categoriesList.add("categories -3");
        categoriesList.add("categories -4");

        homeList.clear();
        // adding element to Home arraylist
        homeList.add("home -1");
        homeList.add("home -2");
        homeList.add("home -3");
        homeList.add("home -4");

        topPaidList.clear();
        // adding element to TopPaid arraylist
        topPaidList.add("topPaid -1");
        topPaidList.add("topPaid -2");
        topPaidList.add("topPaid -3");  
        topPaidList.add("topPaid -4");

        topFreeList.clear();
        // adding element to TopFree arraylist
        topFreeList.add("topFree -1");
        topFreeList.add("topFree -2");
        topFreeList.add("topFree -3");
        topFreeList.add("topFree -4"); 

        position = getArguments().getInt(ARG_POSITION); // here I get the position of tab/view pager
    }

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

        View root = inflater.inflate(R.layout.main_pager_body, container, false);

        myListView = (ListView) root.findViewById(R.id.my_list_view);
        deleteBtn = (Button) root.findViewById(R.id.delete_btn);
        addBtn = (Button) root.findViewById(R.id.add_btn);

        switch (position) {
        // Categories position
        case 0:
            setListView(categoriesList);
            break;
            // Home position
        case 1:
            setListView(homeList);
            break;
            // TopPaid position
        case 2:
            setListView(topPaidList);
            break;
            // TopFree position
        case 3:
            setListView(topFreeList);
            break; 
        } // here I am setting the list view based on the tab/view pager position.

        deleteBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "deleteBtn Button clicked", Toast.LENGTH_SHORT).show();

            }
        });

        addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), " addBtn Button clicked", Toast.LENGTH_SHORT).show();

                switch (position) {
                // Categories position
                case 0:
                    categoriesList.add("categories -5");
                    setListView(categoriesList);
                    break;
                    // Home position
                case 1:
                    homeList.add("home -5");
                    setListView(homeList);
                    break;
                    // TopPaid position
                case 2:
                    topPaidList.add("topPaid -5");
                    setListView(topPaidList);
                    break;
                    // TopFree position
                case 3:
                    topFreeList.add("topFree -5");
                    setListView(topFreeList);
                    break;
                }

            }
        });

        return root;
    }

    public void setListView(ArrayList<String> myList){
        myAdapter = new MyListAdapter(getActivity().getApplicationContext(), R.layout.list_row, myList);
        myListView.setAdapter(myAdapter);
        myListView.setItemsCanFocus(true);
        myAdapter.notifyDataSetChanged();
    }
}

public class MyListAdapter extends ArrayAdapter<String>
{
    ArrayList<String> myList;
    public MyListAdapter(Context context, int textViewResourceId,
            ArrayList<String> myList) {
        super(context, textViewResourceId, myList);

        this.myList = myList;
    }

    public class ViewHolder {
        private TextView listElementTV;
        private RelativeLayout buttonContains;
        private Button deleteItemBtn;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        ViewHolder holder = null;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // get the view for assign data to list view
            v = vi.inflate(R.layout.list_row, null);

            TextView listElementTV = (TextView) v.findViewById(R.id.list_element);

            RelativeLayout buttonContains = (RelativeLayout) v.findViewById(R.id.button_contains);
            Button deleteItemBtn = (Button) v.findViewById(R.id.delete_item);

            holder = new ViewHolder();

            holder.listElementTV = listElementTV;
            holder.buttonContains = buttonContains;
            holder.deleteItemBtn = deleteItemBtn;

            v.setTag(holder);
        } else
            holder = (ViewHolder) v.getTag();

        // get list of hospitalityInfo using position
        String listElement = this.myList.get(position); 

        if(listElement != null){
            holder.listElementTV.setText(listElement);
        }

        return v;
    }

}

最佳答案

Android 学习者

现在回答太晚了,但可能对其他人有帮助

用户在下面的链接中解决了这样的实现。

Alternative for the onResume() during Fragment switching

关于android - PagerSlidingTabStrip:如何在运行时刷新当前选项卡中的内部 fragment Listview 并停止为下一个选项卡加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979687/

有关android - PagerSlidingTabStrip:如何在运行时刷新当前选项卡中的内部 fragment Listview 并停止为下一个选项卡加载数据的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  8. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  9. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  10. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

随机推荐