左のナビゲーション ドロワーを使用してアプリを開発しています。ナビゲーション ドロワーのすべてのセクションは、サーバーからの JSON データを含む RecyclerView リストを含む別のフラグメントを読み込みます。JSON データは Volley 経由で取得されますが、非同期タスクに時間がかかり、RecyclerView が空で表示されることがあります。ユーザーがビューをドラッグしたときにフラグメントをリロードできるように、フラグメントに SwypeRefreshLayout を実装したいと考えています。
私には2つの問題があります:
1) 小さな回転するボールのアニメーションが一瞬表示されてから消えます
2) 下にスワイプすると、ナビゲーション ドロワー セクションの他のすべてのフラグメントに、まったく同じフラグメントが表示されますが、これは更新されたものであり、それ自体のものではありません。
リロード アニメーションを適切に表示し、各セクションでフラグメントのリロードを個別にトリガーするにはどうすれば修正できますか?
フラグメントの XML コードは次のとおりです。
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/instancesSRL">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/instancesCV"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/accent"
android:elevation="100dp"
android:layout_marginBottom="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/instancesSection"
android:textColor="@android:color/white"
android:textSize="@dimen/abc_text_size_large_material"
android:textAlignment="center"
android:id="@+id/instancesTV"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="44dp"
android:layout_height="@dimen/abc_action_button_min_height_material"
android:background="@color/window_bg"
android:layout_alignEnd="@id/instancesTV">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/endpoint_icon"
android:src="@drawable/ic_stars_white_48dp"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/instancesRV"
android:layout_below="@+id/instancesCV"
android:clickable="true"
android:scrollbars="vertical"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/openstack_logo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="true"
android:layout_alignWithParentIfMissing="false"
android:alpha="0.1"/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/add_buttonInstances"
android:layout_width="@dimen/diameter"
android:layout_height="@dimen/diameter"
android:tint="@android:color/white"
android:layout_gravity="end|bottom"
android:layout_marginBottom="@dimen/add_button_margin"
android:layout_marginEnd="@dimen/card_height"
android:src="@android:drawable/ic_input_add"
android:clickable="true"
android:stateListAnimator="@drawable/button_elevation"
android:background="@drawable/fab_selector"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</FrameLayout>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
そして、ここにフラグメントコードがあります:
public class InstancesFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private OnFragmentInteractionListener mListener;
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static InstancesFragment newInstance(int sectionNumber) {
InstancesFragment fragment = new InstancesFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public InstancesFragment() {
}
public ArrayList<HashMap<String, String>> jsonList;
public RecyclerView recyclerView;
public SwipeRefreshLayout refreshLayout;
private Handler handler = new Handler();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle extras = getArguments();
Serializable parsedList = extras.getSerializable("NovaParsed");
jsonList = (ArrayList<HashMap<String, String>>)parsedList;
View rootView = inflater.inflate(R.layout.fragment_instances, container, false);
refreshLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.instancesSRL);
recyclerView = (RecyclerView)rootView.findViewById(R.id.instancesRV);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.supportsPredictiveItemAnimations();
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setClickable(true);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
NovaAdapter novaAdapter = new NovaAdapter(getActivity(),jsonList);
if (novaAdapter.getItemCount() != 0) {
recyclerView.setAdapter(novaAdapter);
}
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Fragment instancesFragment = getFragmentManager().findFragmentById(R.id.container);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.remove(instancesFragment);
fragmentTransaction.commit();
refreshLayout.measure(20, 20);
refreshLayout.setRefreshing(true);
}
});
refreshLayout.setColorSchemeColors(Color.RED, Color.GRAY);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((Stackerz) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
//try {
// mListener = (OnFragmentInteractionListener) activity;
//} catch (ClassCastException e) {
// throw new ClassCastException(activity.toString()
// + " must implement OnFragmentInteractionListener");
//}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
これは、Navigation Drawer を使用して MainActivity からすべてのフラグメントを呼び出す方法です。
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position))
.commit();
switch (position) {
case 0:
extras = authBundle();
OverviewFragment overviewFragment = new OverviewFragment();
overviewFragment.setArguments(extras);
fragmentManager.beginTransaction().add(R.id.container, OverviewFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container,overviewFragment).commit();
break;
case 1:
novaExtras = novaBundle();
InstancesFragment instancesFragment = new InstancesFragment();
instancesFragment.setArguments(novaExtras);
fragmentManager.beginTransaction().add(R.id.container, InstancesFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, instancesFragment).commit();
break;
case 2:
flavorsExtras = flavorsBundle();
FlavorsFragment flavorsFragment = new FlavorsFragment();
flavorsFragment.setArguments(flavorsExtras);
fragmentManager.beginTransaction().add(R.id.container, FlavorsFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, flavorsFragment).commit();
break;
case 3:
glanceExtras = glanceBundle();
ImagesFragment imagesFragment = new ImagesFragment();
imagesFragment.setArguments(glanceExtras);
fragmentManager.beginTransaction().add(R.id.container, ImagesFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, imagesFragment).commit();
break;
case 4:
networksExtras = networksBundle();
NetworksFragment networksFragment = new NetworksFragment();
networksFragment.setArguments(networksExtras);
fragmentManager.beginTransaction().add(R.id.container, NetworksFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, networksFragment).commit();
break;
case 5:
subnetsExtras = subnetsBundle();
SubnetsFragment subnetsFragment = new SubnetsFragment();
subnetsFragment.setArguments(subnetsExtras);
fragmentManager.beginTransaction().add(R.id.container, SubnetsFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, subnetsFragment).commit();
break;
case 6:
routersExtras = routersBundle();
RoutersFragment routersFragment = new RoutersFragment();
routersFragment.setArguments(routersExtras);
fragmentManager.beginTransaction().add(R.id.container, RoutersFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, routersFragment).commit();
break;
case 7:
securityExtras = securityBundle();
SecurityFragment securityFragment = new SecurityFragment();
securityFragment.setArguments(securityExtras);
fragmentManager.beginTransaction().add(R.id.container, SecurityFragment.newInstance(position)).commit();
fragmentManager.beginTransaction().replace(R.id.container, securityFragment).commit();
break;
default:
fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position)).commit();
break;
}
}