根据字符串数组项,将相同项显示到下一个片段 [英] Based on String Array item, show same item to next fragment

查看:92
本文介绍了根据字符串数组项,将相同项显示到下一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航抽屉中有两个片段ListNav和SwipeNav。 ListNav显示来自String数组的数据的列表视图。 SwipeNav将数据显示为字符串数组中相同数据的滑动视图。我可以将片段从ListNav替换为SwipeNav。字符串数组数据显示 A到Z。

I have two fragments ListNav and SwipeNav in Navigation Drawer. ListNav shows list view of data from String array. SwipeNav shows data as swipe views of the same data from string array. I am able to replace fragment from ListNav to SwipeNav. String array data shows 'A to Z'.

我的问题是,当我单击ListNav中的列表时(假设B),然后SwipeNav(已替换的片段)从开始显示滑动视图(意味着A)。假设,如果我通过片段替换在ListNav中单击 D,则SwipeNav将显示 D。

My problem is when I click on list in ListNav (Suppose B)then SwipeNav(replaced Fragment) shows swipe views from start(means A). Suppose, if I click on 'D' in ListNav through Fragment replace, SwipeNav will show 'D'. Plz suggest me how I will implement this.

字符串数组:

<string-array name="tab_titles">
    <item>A</item>
    <item>B</item>
    <item>C</item>
    <item>D</item>
    <item>E</item>
    <item>F</item>
    <item>G</item>
    <item>H</item>
    <item>I</item>
    ........
</string-array>

我ListNav我在下面使用OnItemClickListener替换为SwipeNav:

I ListNav I use below to replace to SwipeNav with OnItemClickListener:

        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(((ViewGroup)(getView().getParent())).getId(), new SwipeNav());
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();

SwipeNav通过字符串数组通过PagerAdapter将数据显示为滑动视图:

SwipeNav shows Data as Swipe Views through PagerAdapter from String Array:

tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);


推荐答案

我想你在那儿缺少了一步,那就是您需要在替换滑动片段时以String形式发送数据。

Like。

I thing you are missing one step there and that is you need to send data in String form when swipe fragment is replacing .
Like.

Bundle bundle = new Bundle();
bundle.putString("TAG", "D");
// set Fragmentclass Arguments
SwipeNav fragobj = new SwipeNav();
fragobj.setArguments(bundle);
  FragmentManager fm=getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(((ViewGroup)(getView().getParent())).getId(), fragobj);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();

现在在您的滑动片段上,请确保您收到创建时传递的字符串。 >
,因此在 onCreateView 方法中。

now on your swipe fragment make sure you are receiving that string that you are passing when creating .
so in onCreateViewmethod .

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String passingString = getArguments().getString("TAG");    
    return inflater.inflate(R.layout.fragment, container, false);
}

此处TAG仅用于识别您正在通过的内容。

Here TAG just identifying what you are passing .

这篇关于根据字符串数组项,将相同项显示到下一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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