繁体   English   中英

我如何从listadapter调用片段

[英]how can i call fragment from listadapter

嗨,我从我的自定义适配器调用片段,但是它的显示错误可以帮助我在这里的MyListAdapter片段

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final TextView view = new TextView(parent.getContext());
    view.setText(values.get(position));

    view.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view1) {

            // Get the position
            Log.w(MyListAdapter.LOG_KEY, "MyListAdapter Lable Clicked");

            Intent intent= new Intent(context, SingleItemView.class);
            context.startActivity(intent);
        }
    });

    return view;
}

和我的SingleItemView.class

public class SingleItemView extends Fragment{

LinearLayout linearLayout;

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

    super.onCreate(savedInstanceState);

    linearLayout = (LinearLayout) inflater.inflate(R.layout.listview_item, container, false);

setHasOptionsMenu(true);

return linearLayout;

}}

这是错误消息

在此处输入图片说明

您无法通过Intent启动Fragment Fragment必须由任何Activity托管。 您必须导航到该Activity

如果要在这些Activity启动特殊的Fragment (例如ViewPager任何Fragment ),请在IntentViewPager任何标志。 通过Activity此标志,您可以确定应该启动哪个Fragment

这是因为您无法通过Intent调用Fragment,Fragment是FragmentActivity的一部分

总而言之,Fragment是一个内容而不是容器,因此您需要创建一个FragmentActivity并在其中添加Fragment(Favourite),然后调用

Intent intent1 = new Intent(context,SomeFragmentActivity.class);

startActivity(intent1);

片段是可以放在活动中的应用程序用户界面或行为的一部分。 http://developer.android.com/reference/android/app/Fragment.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM