我正在制作一个有标签的Android应用程序。在我的一个标签中,我将从片段(比如片段3)转到活动。这工作正常,完成后的这个活动必须到达所需的标签片段2.现在这是我现在面临的问题。经过大量的文件记录后,我发现了一件事,但仍然没有得到理想的想法,因为我是首发。链接如下:
Transactions form fragment to activity
我的MainActivity.java包含每个标签的标签和我的片段。现在我要做的是我从Fragment GroupEditFragment.java转到另一个名为AddAdminActivity.java的活动这对我来说很好。现在,在完成AddAdminActivity.java后,我有了一个按钮。此按钮必须返回MainActivity.java选项卡内的片段,该选项卡名为AddFragment.java。我不知道如何执行此任务,因为我知道如何进入上一页。
case androd.R.id.home
finish();
return true;
但这不是我想要的。我想回到另一个名为AddFragment.java的片段。
这是我可以给你的流程图:
MainActivity.java有标签 - >从此选项卡中可以看到GroupEditFragment.java - > AddAdminActivity.java->再次在MainActivity.java选项卡中添加AddFragment.java
这是我想要进入AddFragment.java的代码
1。 AddAdminActivity.java
public class AddAdminActivity extends AppCompatActivity {
private static final int DIALOGUE_ALERT = 10;
private ListView mListView;
private Button mButton;
//EditText input;
private String[] items;
MyListAdapter adapter;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_admin);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mButton = (Button) findViewById(R.id.addAdminGroup);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Used intent but it is giving me an error, need to implement something to come back to the desired fragment.
}
}); }
2.AddFragment.java是这样的:
public class AddFragment extends Fragment {
private ImageButton imageButton;
private GridView gridView;
ArrayList<File> list = new ArrayList<>();
public AddFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add, container, false);
imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
gridView = (GridView) view.findViewById(R.id.grid_view);
videoReader(Environment.getExternalStorageDirectory());
gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
gridView.setAdapter(new ImageAdapter(getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//for making the button visible as ssonas the item gets selected
imageButton.setVisibility(view.VISIBLE);
}
});
return view;
}
这是我要去AddAdminActivity.java的片段
3.GroupEditFragment.java
public class GroupEditFragment extends Fragment {
private Button addButton;
public GroupEditFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_group_edit, container, false);
addButton = (Button) view.findViewById(R.id.addGroup);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(),AddAdminActivity.class);
startActivity(intent);
}
});
return view;
} }
编辑: 为了实现同样的问题,我已经在我的活动中从我移动到片段中完成了这个:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(AddAdminActivity.this,MainActivity.class);
intent.putExtra("change",1);
startActivity(intent);
}
});
这是我的MainActivity.java:
1.MainActivity.java
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager simpleViewPager;
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
simpleViewPager = (ViewPager) findViewById(R.id.simpleViewPager);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
final TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setIcon(R.drawable.home);
tabLayout.addTab(firstTab, true);
final TabLayout.Tab seconTab = tabLayout.newTab();
seconTab.setIcon(R.drawable.plus);
tabLayout.addTab(seconTab, true);
final TabLayout.Tab thirdTab = tabLayout.newTab();
thirdTab.setIcon(R.drawable.videocall);
tabLayout.addTab(thirdTab, true);
final TabLayout.Tab fourthTab = tabLayout.newTab();
fourthTab.setIcon(R.drawable.downloads);
tabLayout.addTab(fourthTab, true);
final TabLayout.Tab fifthTab = tabLayout.newTab();
fifthTab.setIcon(R.drawable.groupwhite);
tabLayout.addTab(fifthTab, true);
final TabLayout.Tab sixthTab = tabLayout.newTab();
sixthTab.setIcon(R.drawable.addshoppingcart);
tabLayout.addTab(sixthTab, true);
final MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount());
simpleViewPager.setAdapter(myPagerAdapter);
int change = getIntent().getIntExtra("change",0);
if(change==1){
tabLayout.getTabAt(0).getIcon().clearColorFilter();
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.parseColor("#00E676"), PorterDuff.Mode.SRC_IN);
simpleViewPager.setCurrentItem(1);
}
//for color code changing with the tabs
tabLayout.getTabAt(0).getIcon().setColorFilter(Color.parseColor("#00E676"), PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(1).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(2).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(3).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(4).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
tabLayout.getTabAt(5).getIcon().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
simpleViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(tabListener);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
}
设置适配器后,我已经提到我的if条件返回到同一个片段。但颜色仍然存在于我的第一个位置图标而不是第二个位置。但是我的tabIndicator就在它上面。第二个。我的if条件也做了一些改变,但问题仍然存在。
答案 0 :(得分:1)
@Alok这就是我目前在我的应用中所做的事情。您可以使用startActivityForResult调用AddAdminActivity.java,当用户单击AddAdminActivity.java中的按钮时,您可以使用setResult。之后在MainActivity.java中的onActivityResult中,您可以使用AddFragment替换当前片段。