Fragmentation fragmentation in Android

I am entry-level in Android programming, so I need your sincere help for this. Someone help me.

I am trying to create a SLIDING UI using fragments

therefore, my actual doubt is ... I have Fragment(say FragmentA), it has TextViewboth Buttonin it and another Fragment(say FragmentB). It is in it TextView. I need to show the contents Fragment A TextViewin FragmentB TextViewwhen I clicked Buttonin FRAGMENT A. I tried many ways, unfortunately I did not have the proper result. I am sure you know about it. Please help me.

thank

+5
source share
7 answers

, . .

:

package com.example;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.example.fragment.FragmentA;
import com.example.fragment.FragmentA.TextChangeListener;
import com.example.fragment.FragmentB;

public class ActivityAB extends FragmentActivity {

    FragmentA fragmentA;
    FragmentB fragmentB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ab);

        FragmentManager manager = getSupportFragmentManager();
        fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmentA);
        fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentB);

        fragmentA.setTextChangeListener(new TextChangeListener() {

            @Override
            public void onTextChange(CharSequence newText) {
                fragmentB.updateTextValue(newText);
            }
        });
    }

}

A, .

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.R;

public class FragmentA extends Fragment {

    TextChangeListener listener;

    public interface TextChangeListener {
        public void onTextChange(CharSequence newText);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        Button btn = (Button) view.findViewById(R.id.button1);
        final TextView textView = (TextView) view.findViewById(R.id.textView1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onTextChange(textView.getText());
                }

            }
        });
        return view;
    }

    public void setTextChangeListener(TextChangeListener listener) {
        this.listener = listener;
    }
}

B, :

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.R;

public class FragmentB extends Fragment {

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        textView = (TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void updateTextValue(CharSequence newText) {
        textView.setText(newText);
    }
}

ActivityAB xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.fragment.FragmentA"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.fragment.FragmentB"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show" />

</LinearLayout>

B xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="(here will be text)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
+14

, - : MainActivity w/FragmentA [] FragmentB []. / MainActivity. MainActivity / "" , . :

public interface FragmentCommunicator
{
    public void sendMessage ( String broadcastMessage );
    public void takeAction ( String name, int action, Fragment frag );
}

public class MainActivity extends Activity implements FragmentCommunicator
{
    Fragment fragA;
    Fragment fragB;
    ...
    @Override
    public void sendMessage ( String msg )
    {
        if ( msg.Contains("Send To fragA") )
            fragA.receiveMsg(msg);
        ...
    }
    @Override
    public void takeAction ( String name, int action, Fragment frag )
    {
    if ( action == CLOSE )
            removeFragment( name, frag );
        ...
    }
}

public class FragmentA extends Fragment
{
    ...
@Override
public void onClick(View v)
{   
     FragmentCommunicator inter = (FragmentCommunicator) getActivity();
     inter.sendMessage("the txt/information/etc you want to send to fragB");    
}
}

public class FragmentB extends Fragment
{
    ...
    public void receiveMsg ( String msg )
    {
        textView.setText(msg);
    }
}

FragmentA. , .

+2

, . , A B, . , B, B B...

0

, . , , , .

0

, :

  • , A B, , Java. JClass A ( A) JClass B ( B)

2. Frag A, JClass A, String, , : // onCreate.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.<name of your fragment A.xml file>,container,false);

    userInput = (EditText)view.findViewById(R.id.<Id that you gave to EditText field in which user enters text>);

    Button myButton = (Button)view.findViewById(R.id.<id of your Button>);
    myButton.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    JClassB.setText(userInput.getText().toString());/*Replace JClassB with the java class of Fragment B*/

                }
            }
    );

    return view;
}
  1. , , JClass B (java B):

    BottomSectionFrag {

    private static TextView userText;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentB, container, false);
        userText = (TextView)view.findViewById(R.id.userText);
        return view;
    }
    public static void setText(String text){
        userText .setText(text);
    
    }
    

    }

Voila!!

PS This is my first post on stackOverFlow: D

Peace.

-1
source

All Articles