简体   繁体   中英

Android set clickable text to go one fragment to another fragment

I need to do something like this. Suppose I have 2 fragments A and B.There is a text which can be clickable in fragment A and when user click this text , he can go to fragment B. This example helped me to do it but I think it does not work for fragment. So please tell me a way to solve this problem.

public class myClaimsFragment extends Fragment {

   TextView requestNewClaim;

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

        View newClaimRequest = inflater.inflate(R.layout.activity_my_claims, container, false);


        SpannableString ss = new SpannableString("Request");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
            }
        };
        ss.setSpan(clickableSpan , 0,ss.length() , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        requestNewClaim =(TextView) newClaimRequest.findViewById(R.id.requestHere);
        requestNewClaim.setText(ss.toString());
        requestNewClaim.setMovementMethod(LinkMovementMethod.getInstance());

        return newClaimRequest;
    }

}

Layout XML

        <LinearLayout
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/requestHere"
                android:clickable="true"
                android:textSize="20dp"
                android:textStyle="bold"
                android:textColor="#000000"
                android:layout_marginLeft="20dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"/>

        </LinearLayout>

If LoginActivity is a fragment class then it would be okay if you use setOnClickListener on textview. But for fragment change you have to change Intent to fragmentTransaction,

Use something like,

textview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    getFragmentManager().beginTransaction().replace(R.id.container, new LoginActivity() ).addToBackStack("").commit();
});

But, if you want to use SpannableString then do like this,

ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            getFragmentManager().beginTransaction().replace(R.id.container, new LoginActivity() ).addToBackStack("").commit();
        }
    };

Here, R.id.container is the fragment of your main activity layout in which new view will be replaced.

You can not call Fragment via Intent . You need to replace your current fragment with new one.

you have to replace your fragment A to B, use this code

FragmentManager fm = getActivity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = new FragmentB();
ft.replace(R.id.activity_main_content_fragment,fragment);
ft.commit();

In This code replace R.id.youframelayoutid, then it workable

If its code useful so please mark me my answer. :)

As an usual manner you should put a FrameLayout in your Activity's layout XML file. This FrameLayout acts like a placeholder for your Fragments. In other words, Fragment A can be pasted there, so is for Fragment B.

Okay, suppose you've added a FrameLayout in you activity's layout file. Pasting fragments on it and also replacing fragments should be done by the FragmentManager . Hence, you should grab a reference to a FragmentManger in your activity class. For getting this done ...

  • If you use Android Support Libraries, you should get a reference to FragmentManger by getSupportFragmentManager()

  • Otherwise, getFragmentManager()

In Android adding fragments and also replacing them are done in the form of a transaction. Thus you should inform the fragment manager that you would like to do a transaction. This could be done via:

FragmentTransaction transaction = fragmentManger.beginTransaction();

Now, you can apply all what you want on this transaction object. For instance, Adding a fragment could be done like this:

transaction.add(R.id.placeholder, new FragmentA() , "tag-frag-A");

For replacing ...

transaction.replace(R.id.placeholder, new FragmentB(), "tag-frag-B");

After you're done, you commit that transaction by calling

transaction.commit();

Notes:

  • FragmentManager acts like a container for your added fragments. You can search through your added fragments by their tag.

  • Device rotation does not remove added fragments in the FragmentManager. Thus in your onCreate method take care you've added any fragments only once.

  • You can add a Transaction to the back stack. This means that whenever user clicks on the Android back button this fragment will be removed from the state stack and also will be rolled back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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