简体   繁体   English

我如何从listadapter调用片段

[英]how can i call fragment from listadapter

Hi i am calling fragment from my custom adapter but its displaying error can any one help me here is the MyListAdapter snippet 嗨,我从我的自定义适配器调用片段,但是它的显示错误可以帮助我在这里的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;
}

and my SingleItemView.class 和我的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;

} } }}

and here is the error message 这是错误消息

enter image description here 在此处输入图片说明

You can not start Fragment by Intent . 您无法通过Intent启动Fragment Fragment have to be hosted by any Activity . Fragment必须由任何Activity托管。 You have to navigate to this Activity . 您必须导航到该Activity

If you want to start special Fragment in those Activity (for example any Fragment in ViewPager ) put any flag in your Intent . 如果要在这些Activity启动特殊的Fragment (例如ViewPager任何Fragment ),请在IntentViewPager任何标志。 By this flag in your Activity you can determine which Fragment should be started. 通过Activity此标志,您可以确定应该启动哪个Fragment

It is because you can't call Fragments via Intent, Fragment is a part of an FragmentActivity 这是因为您无法通过Intent调用Fragment,Fragment是FragmentActivity的一部分

All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Favourite) in that, and then call 总而言之,Fragment是一个内容而不是容器,因此您需要创建一个FragmentActivity并在其中添加Fragment(Favourite),然后调用

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

startActivity(intent1); startActivity(intent1);

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. 片段是可以放在活动中的应用程序用户界面或行为的一部分。 http://developer.android.com/reference/android/app/Fragment.html http://developer.android.com/reference/android/app/Fragment.html

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

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