Fragment交互,接口方式从一个Fragment传参到另一个Fragment

本文介绍了在Android应用中,如何在Fragment之间实现通讯,并通过界面切换展示不同内容。通过定义接口并实现在Activity中调用,实现了Fragment间的数据交互。展示了如何在不同Fragment之间传递数据,以及使用布局管理器实现界面的灵活展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在要传参的Fragment里面定义一个接口,接口里面有个方法,然后再activity里面实现接口的方法,先看效果图:

package com.example.myandroid.Fragment.Interaction1;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.myandroid.R;

public class ListsFragment extends Fragment {
	private ListView mListView;
	private String[] names = new String[] { "人类", "猴子", "猩猩", "老虎", "阿凡达",
			"德莱尼", "克林姆" };
    private onSetText  monsettext;
	public interface onSetText {
		public void showMessage(String message);
	}
    @Override
    public void onAttach(Activity activity) {
    	super.onAttach(activity);
    	monsettext=(onSetText) activity;
    }
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.lists_fragment_layout, container,
				false);
		mListView = (ListView) v.findViewById(R.id.lists_list);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_expandable_list_item_1, names);
		mListView.setAdapter(adapter);
		// mListView.setEmptyView(mProgressBar);//记住setEmptyView()方法,当没内容时进度条又显示出来
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
		       ArrayAdapter<String>adpter=(ArrayAdapter<String>) parent.getAdapter();
				String itemmessage=adpter.getItem(position);			
				monsettext.showMessage(itemmessage);
				
				
			}
		});
	}
}
package com.example.myandroid.Fragment.Interaction1;

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

import com.example.myandroid.R;

public class ContentsFragment extends Fragment {
    private static TextView mTextView;
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    	super.onActivityCreated(savedInstanceState);
    	
    	
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	View v=inflater.inflate(R.layout.contents_fragment,container,false);
    	mTextView=(TextView) v.findViewById(R.id.contents_fragment_text);
    	mTextView.setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
    	mTextView.setText("每项内容");
    	return v;
    }
    
    public static void setMessageToTextView(String message){
    	mTextView.setText(message);
	}
}
package com.example.myandroid.Fragment.Interaction1;

import com.example.myandroid.R;
import com.example.myandroid.Fragment.Interaction1.ListsFragment.onSetText;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;

public class FragmentActivity extends Activity implements onSetText {

	ContentsFragment contentsFragment;
	ListsFragment listsfragment;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fragment_interaction_layout);

	    contentsFragment = new ContentsFragment();
	    listsfragment=new ListsFragment();
		FragmentTransaction tran =getFragmentManager()
				.beginTransaction();
		tran.add(R.id.lists_fragment,listsfragment).commit();
//		getFragmentManager().beginTransaction()
//				.add(R.id.lists_fragment, new ListsFragment()).commit();

		getFragmentManager().beginTransaction()
				.add(R.id.contents_fragment, new ContentsFragment()).commit();

	}

	@Override
	public void showMessage(String message) {
		
//		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		contentsFragment.setMessageToTextView(message);
	}

}
<?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" >

    <ListView
        android:id="@+id/lists_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
<?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"
    >
    
    <TextView 
        android:id="@+id/contents_fragment_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        />
</LinearLayout>
<?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" >
    
       <FrameLayout 
           android:id="@+id/lists_fragment"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"/>
       <FrameLayout 
           android:id="@+id/contents_fragment"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="3"/>
</LinearLayout>





 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值