为了更加了解fragments
的工作原理,我创建了一个mainactivity
,其布局有一个<fragment>
。我创建了两个java类extend fragment
,每个类都有自己的布局。
在扩展fragment
的java类中,我在textview
内初始化我的视图onActivityCreated()
,如下所示,在同一个类的代码中,我创建了两个方法{{1 }和setbtnclicks(int clicks)
。从getbtnclicks()
i指定按钮的点击次数到mainactivity
,我尝试通过调用{setbtnclicks()
来显示扩展textview
的类fragment
的点击次数来自getbtnclicks
内的{1}}。但问题是,此方法始终显示为零,就好像点击次数没有增加一样。
MainActivtity:
onActivivtyCreated()
Java_Class“Fragment00”
//global variable
private int i = 0;
...
...
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment mSelectedFragment;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
switch (v.getId()) {
case R.id.btn00:
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
i++;
break;
答案 0 :(得分:0)
在片段事务
之前增加iswitch (v.getId()) {
case R.id.btn00:
i++;
mSelectedFragment = new Fragment00();
mFragmentManager = getFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.fragment00ID, mSelectedFragment);
mFragmentTransaction.commit();
mFragment00.setBtnClicks(i);
break;
答案 1 :(得分:0)
一旦我实现了一个或多或少与你相同的程序。目标是增加两个片段中的按钮点击次数。这是代码。希望它有所帮助:
<强> MainActivity.java 强>
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
public class MainActivity extends Activity implements OnClickListener{
Button btn1,btn2;
int click1=0,click2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Fragment fragment = null;
if (v == btn1) {
// do stuff for button left
click1++;
Bundle bundle=new Bundle();
String clickstring1=Integer.toString(click1);
bundle.putString("name1",clickstring1 );
fragment = new FragButton1();
fragment.setArguments(bundle);
}
if (v == btn2) {
// do stuff for button right
click2++;
Bundle bundle=new Bundle();
String clickstring2=Integer.toString(click2);
bundle.putString("name2",clickstring2 );
fragment = new FragButton2();
fragment.setArguments(bundle);
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
else{
Log.e("MainActivity", "Error in creating fragment");
}
}
}
<强> FragButton1.java 强>
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragButton1 extends Fragment{
TextView tv;
public FragButton1(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton1, container, false);
String strtext=getArguments().getString("name1");
tv=(TextView)rootView.findViewById(R.id.tv_times1);
tv.setText(strtext);
return rootView;
}
}
<强> FragButton2.java 强>
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@SuppressLint("NewApi")
public class FragButton2 extends Fragment{
TextView tv2;
public FragButton2(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragbutton2, container, false);
tv2=(TextView)rootView.findViewById(R.id.tv_times2);
String strtext=getArguments().getString("name2");
tv2.setText(strtext);
return rootView;
}
}
这里有2个按钮,只需点击一个按钮即可启动新片段,片段将显示在文本视图中点击按钮的次数。