實際上,我創建了一個導航抽屜,其中有三個項目,並且在點擊每個項目時它會轉到其顯示特定類型產品的片段,因此我有一個包含三個片段的Activity他們的每種類型。只加載一個片段並僅替換它的內容
如果我想添加另一種產品類型,我將不得不創建它的片段。 所以,我的問題是,是否有任何方法只能製作一個片段,每次只有一個片段中的數據被更改/替換,而不是用另一個片段替換整個片段?
編輯我的主要活動:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
RecyclerView recyclerView;
String navTitles[];
private NavigationView navigationView;
TypedArray navIcons;
RecyclerViewAdapter recyclerViewAdapter;
ActionBarDrawerToggle drawerToggle;
Fragment[] fFragments = new Fragment[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Let's first set up toolbar
setupToolbar();
//Initialize Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);
//Setup Titles and Icons of Navigation Drawer
navTitles = getResources().getStringArray(R.array.navDrawerItems);
navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);
recyclerViewAdapter = new RecyclerViewAdapter(navTitles, navIcons, this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setClickedListener(new RecyclerViewAdapter.ClickListerner() {
@Override
public void onItemlistener(int index) {
updateUIWithIndex(index);
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Finally setup ActionBarDrawerToggle
setupDrawerToggle();
//Add the Very First Fragment to the Container
updateUIWithIndex(1);
}
// on click update fragment
private void updateUIWithIndex(int index) {
drawerLayout.closeDrawers();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment fFragment = null;
if (fFragments[index - 1] == null) {
switch (index) {
case 1:
fFragment = new FirstFragment();
break;
case 2:
fFragment = new SecondFragment();
break;
case 3:
fFragment = new ThirdFragment();
break;
}
fFragments[index - 1] = fFragment;
} else {
fFragment = fFragments[index - 1];
}
fragmentTransaction.replace(R.id.containerView, fFragment);
fragmentTransaction.commit();
}
void setupToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
drawerToggle.syncState();
}
}
我的片段:
public class FirstFragment extends Fragment implements ClickListner {
private final String LOG_TAG = FirstFragment.class.getSimpleName();
private DisplayAdapter recyclerViewAdapter;
private RecyclerView recyclView;
private ArrayList<Products> pProduct = null;
private List<Products> prods = null;
ProductDbHelper pDB;
ProgressDialog mJsonDialog;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.all_products, container, false);
pDB = new ProductDbHelper(getActivity());
mJsonDialog = new ProgressDialog(getActivity());
mJsonDialog.setIndeterminate(true);
if (pDB.isDataAvailable() == 0) {
mJsonDialog.setMessage("Parsing JSON feed...");
mJsonDialog.show();
getFeed();
} else {
new FetchDatabaseTask().execute();
}
return myView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclView = (RecyclerView) view.findViewById(R.id.RecycleList);
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclView.setLayoutManager(layoutManager);
recyclerViewAdapter = new DisplayAdapter(getActivity(), new ArrayList<Products>());
recyclView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.setClickListener(this);
}
@Override
public void itemClicked(View view, Parcelable product) {
Intent intent = new Intent(getActivity(), DetailActivity.class);
intent.putExtra("P", product);
startActivity(intent);
}
public void getFeed() {
RestInterface interfaces = Client.getClient().create(RestInterface.class);
Call<List<Products>> call = interfaces.getProductsReport();
call.enqueue(new Callback<List<Products>>() {
@Override
public void onResponse(Call<List<Products>> call, Response<List<Products>> response) {
prods = response.body();
for (int i = 0; i < prods.size(); i++) {
pDB.addShop(prods.get(i));
}
new FetchDatabaseTask().execute();
if (mJsonDialog.isShowing())
mJsonDialog.dismiss();
}
@Override
public void onFailure(Call<List<Products>> call, Throwable t) {
Log.e(LOG_TAG, "FFFF" + t.toString());
}
});
}
public class FetchDatabaseTask extends AsyncTask<Void, Void, List<Products>> {
protected void onPreExecute() {
mJsonDialog.setMessage("Reading from internal storage...");
mJsonDialog.show();
}
@Override
protected List<Products> doInBackground(Void... voids) {
// get all the shop's products
List<Products> lProduct = pDB.getAllShops();
// in the second fragment , sort the products' price in ascending order
List<Products> lProduct = pDB.sortShopsAscend();
// in the third fragment sort the products descendingly
List<Products> lProduct = pDB.sortShopsDescend();
return lProduct;
}
protected void onPostExecute(List<Products> shops) {
super.onPostExecute(shops);
if (shops != null) {
if (recyclerViewAdapter != null) {
recyclerViewAdapter.setData(shops);
} else {
pProduct = new ArrayList<>();
pProduct.addAll(shops);
}
}
if (mJsonDialog.isShowing())
mJsonDialog.dismiss();
}
}
}
每個片段的佈局是否完全相同,並且只顯示不同的內容? –
是的,佈局是一樣的 – Suzy
不一定是重複的,但一些好主意。基本上和下面的答案一樣。 http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment –