使用SwipeRefreshLayout重新加载/重新启动片段,其中包含带有CardViews的RecyclerView列表

时间:2014-12-03 01:41:51

标签: json android-fragments android-volley android-recyclerview swiperefreshlayout

我正在开发一个带有左导航抽屉的应用程序,导航抽屉的每个部分都会加载一个不同的片段,其中包含来自服务器的带有JSON数据的RecyclerView列表。 JSON数据是通过Volley获得的,但有时异步任务需要一段时间,而RecyclerView显示为空。我想在片段上实现SwypeRefreshLayout,以便在用户拖动视图时重新加载它们。

我有两个问题:

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);
    }
}

这就是我使用导航抽屉调用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;

        }

    }

0 个答案:

没有答案