【问题标题】:calling childview from fragment onclick listener从片段 onclick 监听器调用 childview
【发布时间】:2013-10-21 11:02:23
【问题描述】:

我正在开发一个应用程序,我需要在同一活动中调用多个 ListFragments。它包含主父 ListFragment,其中有 10 行。 通过单击每一行,我需要在父 ListView 旁边打开新的子 ListView,就像这样。我的编码是

public class FragmentListArray extends Activity {


     static String[] TITLES = 
            {
                    "Henry IV (1)",   
                    "Henry V",
                    "Henry VIII",       
                    "Richard II",
                    "Richard III",
                    "Merchant of Venice",  
                    "Othello",
                    "King Lear"
            };

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

        // Create the list fragment and add it as our sole content.
        if (getFragmentManager().findFragmentById(android.R.id.content) == null) {
            ArrayListFragment list = new ArrayListFragment();
            getFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
        }
    }

    public static class ArrayListFragment extends ListFragment {

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, FragmentListArray.TITLES));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
          // here i need to achieve my childview click listener..
        }
    }
}

我不知道如何设置 onitem 点击监听器。帮助我实现上述目标。提前致谢。

【问题讨论】:

  • 我认为您可以使用下拉菜单来设置显示第二个菜单。如果您愿意,可以查看此链接:codeofaninja.com/2013/04/…
  • @ArshadParwez 感谢您的回复。我想使用 listfragment 并单击我需要出现我的子列表视图。
  • 您可以在下拉菜单中传递自定义列表视图。它会显示为子列表
  • @ArshadParwez 我的父列表视图需要是静态的,通过单击我需要隐藏/显示我的子视图。通过使用下拉菜单,我的父视图将不稳定..
  • 没有。您的父视图也将可见,下拉列表将显示在您点击的列表项下方。

标签: android android-listview android-listfragment


【解决方案1】:

您可以使用getActivity() 并将结果转换为您的活动的类以获取包含片段的活动的实例。
定义一些将接受项目 id 的函数,具体取决于您需要在第二个列表 Fragment 中加载数据。应该是这样的:

YourActivity activity = (YourActivity)getActivity();
activity.fillSecondFragment(clickedItemId);

【讨论】:

    【解决方案2】:

    您要实现的目标与关于片段的 android 开发人员示例非常相似,而不是在单击列表项时使用您的 listfragmnet 的普通片段(实际上是一个片段)

    http://developer.android.com/guide/components/fragments.html

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多