PagerSlidingTabStrip:如何在当前标签页在运行时刷新内部片段ListView和停止加载数据的下一个标签 [英] PagerSlidingTabStrip: How refresh inner fragment Listview in current tab at run time and stop the loading data for next tab

查看:524
本文介绍了PagerSlidingTabStrip:如何在当前标签页在运行时刷新内部片段ListView和停止加载数据的下一个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困与内片段insideNavigation抽屉页的滑动片,带viewpager标签使用列表视图的问题,在的这个混帐枢纽例子

I got stuck into the problem with listview used with inner fragment insideNavigation-drawer-page-sliding-tab-strip viewpager tabs, that given in This git hub example.

我使用的是同一个例子,有一个列表视图不同的ArrayList设置有适配器,所有4个标签。

i am using the same example and All 4 tabs having a list view with different arraylist set to there adapters.

我只用一个片段和标签位置基地我加载不同的列表数据内片段arrayAdapter。另外我有两个按钮,一个是删除,另一种是添加。

I am using only one fragment and bases of tabs position I am loading different list data to inner fragment arrayAdapter. Also I have two buttons one is delete and another one is Add.

我要什么:如果我preSS Add按钮,那么就应该新数据添加到数组列表(基于标签的位置增加新加入到各自的ArrayList),并刷新列表视图的数据。

What I want: If I press the add button then it should add new data to the array list(based on the tab position add the new add to respective arrayList) and refresh the listview data.

在我的code其刷新当前查看选项卡,但在接下来的选项卡的列表视图也令人耳目一新,并采取previous选项卡中的数据。请帮我在这,任何帮助,将AP preciated。

In my code its refreshing the current viewing tab but in next tab listview also refreshing and taking the data of previous tab. Please help me on this, any help would be appreciated.

我有一个类文件这一点。如果我的做法是错误的,请告诉我们正确的做法。

I have single class file for this. If my approach is wrong please let me know correct approach.

公共类PageSlidingTabStripFragment扩展片段{

public class PageSlidingTabStripFragment extends 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的学习

Android Learner

就为时已晚了答案,但可能帮助别人

it too late for answer but may be helps others

用户点击以下链接来解决这样的实现。

user below link for solving such implementation.

<一个href="http://stackoverflow.com/questions/17845641/alternative-for-the-onresume-during-fragment-switching">Alternative为onResume()片段期间切换

这篇关于PagerSlidingTabStrip:如何在当前标签页在运行时刷新内部片段ListView和停止加载数据的下一个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆