Android中带片段的前向导航
我想知道是否有人知道一种通过片段实现向前和向后导航的方法 我想从一个片段开始,当我按下next按钮时,必须创建一个新片段,当我按下back按钮时,必须显示上一个片段。我的代码就是这样做的,它保存了我对任何以前的片段所做的任何更改,因为它们保存在后堆栈中,但是,我希望前向导航也是如此,有什么方法可以实现这一点吗 因此,更准确地说,我想创建10个片段,所有片段都有一个文本框,我可以在其中输入一些数据,每次我输入数据并按下下一步按钮时,它必须导航到下一个片段,有一个空文本框,并将上一个片段加上输入的数据保存到后堆栈中(目前是这样)。因此,当我到达片段10时,我希望能够返回到片段1,然后返回到片段10,但是当我再次向前导航时,我最初输入的数据必须仍然存在。然后当我导航到片段11时,它一定是一个空文本框。所以它一定是一个前后向的导航系统 下面的代码从代码中动态生成片段,并动态地(在运行时)向其中添加视图。这是我目前正在使用的代码,用于倒退Android中带片段的前向导航,android,android-fragments,Android,Android Fragments,我想知道是否有人知道一种通过片段实现向前和向后导航的方法 我想从一个片段开始,当我按下next按钮时,必须创建一个新片段,当我按下back按钮时,必须显示上一个片段。我的代码就是这样做的,它保存了我对任何以前的片段所做的任何更改,因为它们保存在后堆栈中,但是,我希望前向导航也是如此,有什么方法可以实现这一点吗 因此,更准确地说,我想创建10个片段,所有片段都有一个文本框,我可以在其中输入一些数据,每次我输入数据并按下下一步按钮时,它必须导航到下一个片段,有一个空文本框,并将上一个片段加上输入的数
public class MainActivity extends Activity
{
int mStackLevel = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button IntegratedBiometricsButton = (Button) findViewById(R.id.integrated_biometrics_button);
IntegratedBiometricsButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent IBIntent = new Intent(MainActivity.this, SimpleScanActivity.class);
startActivity(IBIntent);
}
});
// Watch for button clicks.
Button button = (Button) findViewById(R.id.new_fragment);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
addFragmentToStack();
}
});
button = (Button) findViewById(R.id.delete_fragment);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
getFragmentManager().popBackStack();
if(mStackLevel > 0){mStackLevel--;}
}
});
if (savedInstanceState == null)
{
// Do first time initialization -- add initial fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
} else
{
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
void addFragmentToStack()
{
mStackLevel++;
// Instantiate a new fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
public static class CountingFragment extends Fragment
{
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static CountingFragment newInstance(int num)
{
CountingFragment f = new CountingFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
int ID = 0;
View v = inflater.inflate(R.layout.fragment_layout, container, false);
LinearLayout fragmentLayout = (LinearLayout) v.findViewById(R.id.infoLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(container.getContext());
tv.setId(ID);
ID++;
tv.setTextSize(16);
tv.setText("Fragment #" + mNum);
tv.setGravity(Gravity.CENTER);
tv.setLayoutParams(lp);
fragmentLayout.addView(tv);
EditText et = new EditText(container.getContext());
et.setId(ID);
ID++;
et.setHint("Text");
et.setGravity(Gravity.CENTER);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setLayoutParams(lp);
fragmentLayout.addView(et);
CheckBox cb = new CheckBox(container.getContext());
cb.setId(ID);
cb.setChecked(false);
cb.setGravity(Gravity.LEFT);
cb.setLayoutParams(lp);
fragmentLayout.addView(cb);
cb.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(((CheckBox) v).isChecked())
{
Toast.makeText(v.getContext(), "Yo", Toast.LENGTH_LONG).show();
}
}
});
ID++;
ImageView iv = new ImageView(container.getContext());
iv.setId(ID);
iv.setImageResource(R.drawable.output);
iv.setLayoutParams(lp);
fragmentLayout.addView(iv);
iv.setOnLongClickListener(new View.OnLongClickListener()
{
public boolean onLongClick(View v)
{
Toast.makeText(v.getContext(), "It works", Toast.LENGTH_LONG).show();
return true;
}
});
ID++;
//View tv = v.findViewById(R.id.text);
//((TextView) tv).setText("Fragment #" + mNum);
//tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}
}
如果你想要一个向导用户界面,为什么不使用开源,而不是使用你自己的?@commonware你是第一个向我指出正确方向的人,已经尝试了一段时间了。但是,我将如何向上面代码的onCreateView方法中添加自定义内容?您将按照所选库的说明进行操作。