2016-12-27 54 views
0

使用Tab键布局与滑动式浏览从片段至片段数据I创建的标签布局与滑动式视图。 我试图从片段 传递一个字符串片段预先感谢您 Details_customer.java发送Android中

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.net.Uri; 
import android.support.design.widget.TabLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.FragmentStatePagerAdapter; 
import android.support.v4.view.PagerAdapter; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 
public class Details_customer extends AppCompatActivity implements Cust_Details_basic.FragmentDataListener { 
SharedPreferences login_pref, IP; 
private ProgressDialog pDialog;//For Loading activity.. 
Bundle dataBundle; 
/** 
* The {@link PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link FragmentPagerAdapter} derivative, which will keep every 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 




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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 


@Override 
public void onFragmentDataUpdated(Bundle dataBundle) 
{ 
    this.dataBundle=dataBundle; 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_main, menu); 
    //getMenuInflater().inflate(R.menu.menu_details_customer, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.action_logout: 
      login_pref = getSharedPreferences("Login Pref", MODE_PRIVATE); 
      SharedPreferences.Editor editor = login_pref.edit(); 
      editor.putString("username", null); 
      Intent intent = new Intent(this, MainActivity.class); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      startActivity(intent); 
      Toast.makeText(getApplicationContext(), "Logout", Toast.LENGTH_SHORT).show(); 
      /*editor.clear();*/ 
      editor.commit(); 
      finish(); 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
public Action getIndexApiAction() { 
    Thing object = new Thing.Builder() 
      .setName("Details_customer Page") // TODO: Define a title for the content shown. 
      // TODO: Make sure this auto-generated URL is correct. 
      .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
      .build(); 
    return new Action.Builder(Action.TYPE_VIEW) 
      .setObject(object) 
      .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
      .build(); 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client.connect(); 
    AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
} 

@Override 
public void onStop() { 
    super.onStop(); 

    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
    client.disconnect(); 
} 

//deleted 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       Cust_Details_basic cust_basic = new Cust_Details_basic(); 
       cust_basic.setArguments(dataBundle); 
       return cust_basic; 
      case 1: 
       Cust_Details_address address = new Cust_Details_address(); 
       address.setArguments(dataBundle); 
       return address; 
      /* case 2: 
       Cust_Details_last details_last = new Cust_Details_last(); 
       return details_last;*/ 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     // Show 2 total pages. 
     return 2; 
    } 

    /*** 
    * new addition 
    ***/ 
    public void showFragment(String val) { 
     System.out.println(val); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "Basic Info"; 
      case 1: 
       return "Address"; 
      /* case 2: 
       return "Address";*/ 
     } 
     return null; 
    } 
}} 

Cust_Details_basic.java这里是我的EDITTEXT我想值发送到下一个片段

public class Cust_Details_basic extends Fragment { 

EditText finame; 
private ProgressDialog pDialog;//For Loading activity.. 

public interface FragmentDataListener{ 

    void onFragmentDataUpdated(Bundle dataBundle); 

} 
private FragmentDataListener mFragmentDataListener; 
@Override 

public void onAttach(Context context) 
{ 
    super.onAttach(context); 
    mFragmentDataListener=(FragmentDataListener)getActivity(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.details_customer_1, container, false); 
    finame = (EditText)view.findViewById(R.id.fname); 
    /**********************************************/ 
    Bundle bundle = new Bundle(); 
    bundle.putString("fname",finame.getText().toString()); 
    mFragmentDataListener.onFragmentDataUpdated(bundle); 
    /*********************************************/ 
    return view; 
}} 

这里是Cust_Details_address.java在这里,我想EDITTEXT值

public class Cust_Details_address extends Fragment implements View.OnClickListener{ 

Button btnsubmit; 
String fname,firstname; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.details_customer_2, container, false); 


    Bundle bundle = this.getArguments(); 

      if (bundle != null) { 
       firstname = bundle.getString("fname",fname); 

       Toast.makeText(getActivity().getApplicationContext(),"fname:"+firstname,Toast.LENGTH_SHORT).show(); 
       Log.d("First Name:","=======>"+firstname); 

      } 
      else{ 
       Toast.makeText(getActivity().getApplicationContext(),"Am Empty:",Toast.LENGTH_SHORT).show(); 
      } 


    btnsubmit = (Button)view.findViewById(R.id.btnsubmit); 
    btnsubmit.setOnClickListener(this); 

    return view; 
}} 
+0

需要更多的代码才能分析情况,你使用的是'Viewpager'吗? 'FragmentManager'?哪个机制加载碎片? 。请注意,他可能被标记为与[传递片段之间的数据]这样的主题相关的副本(http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments)。 – Talha

+0

我使用Viewpager – Pash

+0

起初我误解了,看你需要把bundle传回activity,然后当Viewpager加载新的fragment时,它会附上调用setArguments()的那个bundle,你可以实现一个接口会很快显示你的代码。 – Talha

回答

0

在Cust_Details_address.java我重写这样

方法
@Override 
public void onStart(){ 
    super.onStart(); 

    btnsubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     String firstname = ((EditText)getActivity().findViewById(R.id.fname)).getText().toString(); 
     Toast.makeText(getActivity().getApplicationContext(),"Hi "+firstname ,Toast.LENGTH_SHORT).show(); 
     } }); 

我得到了我接受的结果。非常感谢你的“Talha”为你的需要帮助。

0

在你的活动定义接口,包对象,然后调用活动实例的接口方法与你的碎片数据:

你这样做Details_customer活动,

public class Details_customer extends AppCompatActivity 
    implements Cust_Details_basic.FragmentDataListener { 

     SharedPreferences login_pref,IP; 
     private ProgressDialog pDialog;//For Loading activity.. 
     Bundle dataBundle; 

     ..... other code 

    // override method of interface 

    @override void onFragmentDataUpdated(Bundle dataBundle) 
    { 

     this.dataBundle=dataBundle; 
    } 
    ...... other code 
    // in SectionsPagerAdapter adapter 
      @Override public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        Cust_Details_basic cust_basic = new Cust_Details_basic(); 
        cust_basic.setArguments(dataBundle); // remember to update 
                 //bundle object as per requirement 
        return cust_basic; 
       case 1: 
        Cust_Details_address address = new Cust_Details_address(); 
        address.setArguments(dataBundle); 
        return address; 


    /* case 2: 
         Cust_Details_last details_last = new Cust_Details_last(); 

         return details_last;*/ 
       } 
       return null; 
      } 

在你Cus_Details_basic片段,宣布接口:

public class Cust_Details_basic extends Fragment { 

    public interface FragmentDataListener{ 

    void onFragmentDataUpdated(Bundle dataBundle); 

    } 

private FragmentDataListener mFragmentDataListener; 


// create fragment object and initialize it in onAttach() 

@Override public void onAttach(Context context) 
    { 
     super.onAttach(context); 


     mFragmentDataListener=(FragmentDataListener)getActivity(); 


    } 

// then update your onCreate as: 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.details_customer_1, container, false); 
    Fragment fragment = new Fragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("fname",fname); 
    //fragment.setArguments(bundle); 
    mFragmentDataListener.onFragmentDataUpdated(bundle); 
    // this will update dataBundle object in your activity 
return view; 
} 

这将叫onFragmentDataUpdated()你的Details_customer类,它将更新它的dataBundle对象,现在每当Viewpager发生变化时,你应该可以用setArguments()这个Bundle对象,最后在onCreate的那个片段就能够检索到数据。

相关链接:

Communicating between Fragments.

How To Communicate Between Fragments and Activities in Android.

+0

评论是不适合扩展讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/131672/discussion-on-answer-by-talha-send-data-from-fragment-to-fragment-using-tab- LAYO)。 –

0

回调(接口)捆绑被推荐的方式,但是这是一个快速的解决方案:

YourActivity:

public void randomMethod(String val){ 
System.out.println(val); 
} 

片段1:

onClick(){ 
((YourActivity) getActivity()).randomMethod(yourString1); 
} 

Fragment2:

onClick(){ 
((YourActivity) getActivity()).randomMethod(yourString2); 
} 

您还可以从活动的任何片段的方法,并使用更新后的值。

+0

我没有得到你所说的。 – Pash

+0

在您的活动中写入randomMethod,然后从碎片中更新该方法的值并使用它。 – Spartan

+0

能够在我的代码中解释..? – Pash

0

由于所有的片段中,考虑到寻呼机,这意味着它们共享相同的activity.So只是把价值的意图内,让它在该视图寻呼机的任何片段,无需单独通过数据捆绑。

在你Cust_Details_basic片段(保存像这样)

getActivity().getIntent().putExtra("name",fname); 

Cust_Details_address片段(获取它好像是这样)

String fname = getActivity().getIntent().getStringExtra("name"); 
+0

如何发送文本框输入值..? ();} – Pash

+0

'String fname = editText.getText()。toString();' 'getActivity()。getIntent()。putExtra(“name”,fname);' –

+0

finame =(EditText)view.findViewById(R.id。 FNAME); – Pash