简体   繁体   English

当我单击TextView ID时,如何将一个片段转到另一个片段?

[英]How to go one Fragment to another when I will click on TextView Id?

How to go one Fragment to another when I will click on TextView Id? 当我单击TextView ID时,如何将一个Fragment转到另一个Fragment

Error shows: 错误显示:

java.lang.ClassCastException: com.example.tripigator.Overview cannot be cast to android.app.Activity.

and error comes from these lines : 错误来自这些行:

TextView overview = (TextView) findViewById(R.id.txt_overview);
            overview.setOnClickListener(new View.OnClickListener() {                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ProjectOverview.this, Overview.class);
                    startActivity(intent);              
                }
            });

So please anybody help me how to move from one fragment to another by clicking text item. 因此,请任何人通过单击文本项帮助我如何从一个fragment移动到另一个fragment

ProjectOverview class : ProjectOverview类:

public class ProjectOverview extends FragmentActivity {     
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.project_overview);
         initialingpaging();             
        TextView overview = (TextView) findViewById(R.id.txt_overview);
        overview.setOnClickListener(new View.OnClickListener() {                
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProjectOverview.this, Overview.class);
                startActivity(intent);              
            }
        });
    }

     private void initialingpaging() {
            // TODO Auto-generated method stub
            List<Fragment> fragments = new Vector<Fragment>();
            fragments.add(Fragment.instantiate(this, Overview.class.getName()));
            fragments.add(Fragment.instantiate(this, Highlight.class.getName()));               
            mPageAdapter = new PageAdapter(this.getSupportFragmentManager(), fragments);            
            ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
            pager.setAdapter(mPageAdapter);     

        }
}

Two Fragment Classes: 两个片段类:

public class Overview extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) 
    {    
        if(container == null)
        {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.overview,container,false);
    }
}


public class Highlight extends Fragment {       
    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) {              
        if(container == null)
        {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.highlight,container,false);
    }
}

Layout Interface: 布局界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
        <LinearLayout
            android:id="@+id/project_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="65dp"          
            android:gravity="center" >

            <TextView
                android:id="@+id/txt_overview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_marginLeft="12dp"
                android:text="Overview" />

            <TextView
                android:id="@+id/txt_highlite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_marginLeft="12dp"
                android:text="Highlight" />
        </LinearLayout>      
    </RelativeLayout>  
</RelativeLayout>

you should not use intent in fragment to move another fragment 您不应该在片段中使用intent来移动另一个片段

and check this for whole example http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ use this. 并检查整个示例 http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/使用此示例

((yourfragmentActivity) getActivity()).setCurrentItem(1,//desired activity 0=first 1= second and so on
                        true);

insted 插入

Intent intent = new Intent(ProjectOverview.this, Overview.class);
                startActivity(intent);

You can't initiate the fragment using Intent.Try this it helps. 您无法使用Intent来启动片段。尝试这样做会有所帮助。

Fragment fragment = null;
FragmentManager frgManager;
FragmentTransaction ft;

fragment = new Overview();
ft = frgManager.beginTransaction();
ft.replace(R.id.frame_content, fragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();

If you use View-Pager 如果您使用View-Pager

your_ViewPager.setCurrentItem(position)

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

相关问题 当我转到另一个片段时片段重复 - Fragment duplicates when I go to another Fragment 单击textView Fragment类时如何获取消息中的数字 - How to get the number in a message when click in the textView Fragment class 在TextView上继续进行其他活动和/或片段,请单击 - Proceeding to another activity and/or fragment on TextView click 当我单击工具栏后按时,我如何 go 从片段 B 回到 A? - How can i go back to A from fragment B when I click on the toolbar back press? 单击另一个片段中的按钮后,在片段中添加新的文本视图 - Add new textview in fragment after click on button in another fragment 如何通过单击ListView中的项目转到另一个片段? - How can I go to another fragment by clicking on an item in a ListView? 在另一个片段中更改数据时如何刷新一个片段中的RecyclerView - How to refresh RecyclerView in one fragment when data changed in another fragment 如何在ListView项目上用另一个片段替换一个片段单击Android - How to replace one fragment with another on ListView item click android 如何从另一个片段更新DialogFragment的Textview - How to update Textview of an DialogFragment from another Fragment 当我单击 CommandButton 时不会转到另一个页面 - when I click in CommandButton doesnt go to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM