android按钮切换frangment,Android用另一个fragmen替换当前片段

我只是从HoneyComb的片段设计开始。 我创建了两个片段。 当我单击左侧片段中的按钮时,将在右侧创建一个新片段。 同时,当我单击右侧片段中的按钮时(即,下面代码中的DetialsFragment应该替换为另一个片段。main.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/titles" android:layout_weight="1"

android:layout_width="0px"

android:layout_height="match_parent" />

android:layout_width="0px"

android:layout_height="match_parent" />

FragmentExample.java

public class FragmentExample extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

Titles.java

public class Titles extends Fragment {

public FragmentTransaction ft;

@Override

public View onCreateView(LayoutInflater inflater,

ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.main1, null);

Button button1 = (Button)v.findViewById(R.id.button1);

button1.setText("santhosh");

button1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

DetailsFragment details = (DetailsFragment)

getFragmentManager().findFragmentById(R.id.details);

if (details == null || details.getShownIndex() != 1) {

// Make new fragment to show this selection.

details = DetailsFragment.newInstance(1);

// Execute a transaction, replacing any existing

// fragment with this one inside the frame.

ft

= getFragmentManager().beginTransaction();

ft.add(R.id.details, details, "detail");

ft.setTransition(

FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.commit();

}

}

});

return v;

}

}

DetailsFragment.java

public class DetailsFragment extends Fragment {

/**

* Create a new instance of DetailsFragment, initialized to

* show the text at 'index'.

*/

Titles title = new Titles();

String[] titles = {"Title1", "Title2", "Title3", "Title4"};

public static DetailsFragment newInstance(int index) {

DetailsFragment f = new DetailsFragment();

// Supply index input as an argument.

Bundle args = new Bundle();

args.putInt("index", index);

f.setArguments(args);

return f;

}

public int getShownIndex() {

return getArguments().getInt("index", 0);

}

@Override

public View onCreateView(LayoutInflater inflater,

ViewGroup container, Bundle savedInstanceState) {

if (container == null) {

// Currently in a layout without a container, so no

// reason to create our view.

return null;

}

Button button = new Button(getActivity());

button.setText("Next");

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

}

});

return button;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值