Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从列表适配器调用片段_Android_Android Fragments_Android Listfragment_Android Sliding - Fatal编程技术网

Android 从列表适配器调用片段

Android 从列表适配器调用片段,android,android-fragments,android-listfragment,android-sliding,Android,Android Fragments,Android Listfragment,Android Sliding,我在应用程序中使用滑动菜单/抽屉模式。因此,主活动有一个leftView,它是一个名为topicsFragment()的ListFragment,用于加载一组主题项。单击项目/主题时,它会通过调用FeedsFragment(标记)替换主视图上的片段。FeedsFragment使用arraylist适配器加载在每个列表项中具有各种可单击项的提要。当在列表项中单击某个项时,我想在feedsFragment(标记)上获取另一个实例 holder.contextView= (TextView)

我在应用程序中使用滑动菜单/抽屉模式。因此,主活动有一个leftView,它是一个名为topicsFragment()的ListFragment,用于加载一组主题项。单击项目/主题时,它会通过调用FeedsFragment(标记)替换主视图上的片段。FeedsFragment使用arraylist适配器加载在每个列表项中具有各种可单击项的提要。当在列表项中单击某个项时,我想在feedsFragment(标记)上获取另一个实例

    holder.contextView= (TextView) newsView.findViewById(R.id.arcHeader);
    if (item.hasArc()) {
        holder.contextView.setVisibility(View.VISIBLE);
        String arc;
        try {
            arc=item.getarc();
            holder.contextView.setText(arc);

            holder.contextView.setOnClickListener(new View.OnClickListener() {

                //currently it loads a class
                @Override
                public void onClick(View v) { 
                   Intent i = new Intent(context, SomeClass.class); 
                   i.putExtra("tag", arc);
                   context.startActivity(i);
                }
            });
        } catch (JSONException e) {

            e.printStackTrace();
        }

    } else {
        holder.contextView.setVisibility(View.GONE);
    }

当前它加载一个新类。我想定义一个片段,然后传递给主活动以替换为当前视图,但我不能在适配器类中使用getSupportFragmentManager(),而只能在片段或片段活动中使用。除了从适配器中扫描片段,还有什么替代方法呢?

我所做的是在我的主要活动中创建这个方法,并从其他类调用它来更改片段:

public void switchContent(Fragment fragment) {
        mContent = fragment;
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit();

        slidemenu.showContent();

    }

通过使用列表适配器中传递的上下文解决了此问题:

@Override
public void onClick(View v) {
    Fragment newFragment = new ListFragmentClass(tag);
    if (newFragment != null)
        switchFragment(newFragment);
}

private void switchFragment(Fragment newFragment) {
    if (context == null)
        return;
    if (context instanceof MainActivity) {
        MainActivity feeds = (MainActivity) context;
        feeds.switchContent(newFragment);
    }
}

这里,switchContent是在主活动中定义的用于切换/替换片段的方法,如Justin V的回答所示。

getFragmentManager()
作为适配器构造函数中的参数传递并使用它。

使用
接口
将侧抽屉
ListFragment
连接到主活动。例如:

public class LeftDrawer extends ListFragment{

    private DrawerCallback mCallback;

    public interface DrawerCallback{
        public void onListClick(String tag);
    }

    public void setCallback(DrawerCallback callback){
        mCallback = callback;
    }

} 
由于
Fragments
应该有一个空构造函数,因此在完成
FragmentTransaction
将其添加到抽屉之前,请在
FragmentTransaction
中使用公共方法设置回调。此时剩下的就是通知您的
片段
发生了单击。实际上,您应该直接捕获
ListFragment
中的单击,而不是将onClickListener添加到适配器中的每个视图中

 @Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    /*
     * Get item at the clicked position from your adapter and
     * get its string tag before triggering interface
     */
   mCallback.onListClick(tag);
}

使用onListItemClick方法执行此操作。您将获得单击的列表位置,然后可以轻松地从适配器中获取该项目,并获取其标记值以传递回主机活动。

此方法是我想从列表适配器类调用的方法,因此提出了这个问题。我通过使用传递给列表适配器的“应用程序上下文”解决了这个问题;而不是switchFragment(newContent);而且它非常wierd
switchContent
switchFragment
@佐亚应该坚持其中一个,而不是两个。