如何应用微光效果android swipeProgress?

问题描述

如何将我的 RSSfragment 连接到 Apply Shimmer Effect swipeProgress。当 RSSfragment 正在进行时,我需要向您展示一个微光布局。我试过了,lyt_shimmer 没有显示

swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshItems();
        }
    });

我需要在刷新时包含_shimmer_recipes_list

RSSFragment

    public class RSSFragment extends Fragment {
    // Added argument key for URL
    private static final String ARG_URL = "url_string";
    public static final int RECIPES_LIST_SMALL = 0;
    public static final int RECIPES_LIST_BIG = 1;
    public static final int RECIPES_GRID_2_COLUMN = 2;
    public static final int RECIPES_GRID_3_COLUMN = 3;
    private RSSFeed RSSFeed = null;
    private ArrayList<RSSItem> postsList;
    private RSSAdapter listadapter;

    private viewmodeUtils viewmodeUtils;
    private SwipeRefreshLayout swipeRefreshLayout;
    SharedPref sharedPref;
    private Activity mAct;
    private RelativeLayout ll;
    private String url;
    private AdView mAdView;
    private View rootView;
    private ShimmerFrameLayout lyt_shimmer;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        ll = (RelativeLayout) inflater.inflate(R.layout.fragment_list_refresh,container,false);
        return ll;
    }

    @Override
    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        setHasOptionsMenu(true);
        lyt_shimmer = ll.findViewById(R.id.shimmer_view_container);
        RecyclerView listView = ll.findViewById(R.id.list);
        postsList = new ArrayList<>();
        listadapter = new RSSAdapter(getContext(),postsList);
        listadapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
        listView.setAdapter(listadapter);
        listView.setLayoutManager(new linearlayoutmanager(getContext(),linearlayoutmanager.VERTICAL,false));

        swipeRefreshLayout = ll.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshItems();
                initShimmerLayout();
            }





            private void initShimmerLayout() {

                View lyt_shimmer_recipes_list_big = ll.findViewById(R.id.lyt_shimmer_recipes_list_big);


                if (sharedPref.getRecipesViewType() == RECIPES_LIST_SMALL) {

                    lyt_shimmer_recipes_list_big.setVisibility(View.GONE);


                } else if (sharedPref.getRecipesViewType() == RECIPES_LIST_BIG) {

                    lyt_shimmer_recipes_list_big.setVisibility(View.VISIBLE);


                } else if (sharedPref.getRecipesViewType() == RECIPES_GRID_2_COLUMN) {
        ;
                    lyt_shimmer_recipes_list_big.setVisibility(View.GONE);


                } else if (sharedPref.getRecipesViewType() == RECIPES_GRID_3_COLUMN) {

                    lyt_shimmer_recipes_list_big.setVisibility(View.GONE);


                }
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAct = getActivity();

// get URL from arguments
        url = RSSFragment.this.getArguments().getString(ARG_URL);
        refreshItems();
    }

    private class RSSTask extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
// Pass the URL string as parameter to URL class
                URL RSSUrl = new URL(url);
                SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
                SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
                XMLReader myXMLReader = mySAXParser.getXMLReader();
                RSSHandler myRSSHandler = new RSSHandler();
                myXMLReader.setContentHandler(myRSSHandler);
                InputSource myInputSource = new InputSource(RSSUrl.openStream());
                myXMLReader.parse(myInputSource);

                RSSFeed = myRSSHandler.getFeed();

            } catch (ParserConfigurationException | IOException | SAXException e) {
                Log.printstacktrace(e);
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (RSSFeed != null) {
                if (RSSFeed.getList().size() > 0) {
                    postsList.addAll(RSSFeed.getList());
                }

                listadapter.setHasMore(false);
                listadapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_LIST);
                swipeRefreshLayout.setRefreshing(false);

            } else {
                String message = null;
                if (!url.startsWith("http"))
                    message = "Debug info: '" + url + "' is most likely not a valid RSS url. Make sure the url entered in your configuration starts with 'http' and verify if it's valid XML using validator.w3.org/Feed";
                Helper.noConnection(mAct,message);

                listadapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_EMPTY);
                swipeRefreshLayout.setRefreshing(false);

            }

            super.onPostExecute(result);
        }

    }

    @Override
    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
        inflater.inflate(R.menu.RSS_menu,menu);
        viewmodeUtils = new viewmodeUtils(getContext(),getClass());
        viewmodeUtils.inflateOptionsMenu(menu,inflater);
        ThemeUtils.tintAllIcons(menu,mAct);
    }

    private void refreshItems() {
        postsList.clear();
        lyt_shimmer.setVisibility(View.GONE);
        lyt_shimmer.stopShimmer();
        listadapter.setModeAndNotify(InfiniteRecyclerViewAdapter.MODE_PROGRESS);
        new RSSTask().execute(


        );
    }

    private void swipeProgress(final boolean show) {
        if (!show) {
            swipeRefreshLayout.setRefreshing(show);
            lyt_shimmer.setVisibility(View.GONE);
            lyt_shimmer.stopShimmer();
            return;
        }
        swipeRefreshLayout.post(() -> {
            swipeRefreshLayout.setRefreshing(show);
            lyt_shimmer.setVisibility(View.VISIBLE);
            lyt_shimmer.startShimmer();
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        swipeProgress(false);

        lyt_shimmer.stopShimmer();
    }







    @Override
    public boolean onoptionsItemSelected(MenuItem item) {
        viewmodeUtils.handleSelection(item,new viewmodeUtils.changelistener() {
            @Override
            public void modeChanged() {
                listadapter.notifyDataSetChanged();
            }
        });
        switch (item.getItemId()) {
            case R.id.info:
//show information about the Feed in general in a dialog
                if (RSSFeed != null) {
                    String FeedTitle = (RSSFeed.getTitle());
                    String FeedDescription = (RSSFeed.getDescription());
//String FeedPubdate = (myRSSFeed.getPubdate()); most times not present
                    String FeedLink = (RSSFeed.getLink());

                    AlertDialog.Builder builder = new AlertDialog.Builder(mAct);

                    String titlevalue = getResources().getString(R.string.Feed_title_value);
                    String descriptionvalue = getResources().getString(R.string.Feed_description_value);
                    String linkvalue = getResources().getString(R.string.Feed_link_value);

                    if (FeedLink.equals("")) {
                        builder.setMessage(titlevalue + ": \n" + FeedTitle +
                                "\n\n" + descriptionvalue + ": \n" + FeedDescription);
                    } else {
                        builder.setMessage(titlevalue + ": \n" + FeedTitle +
                                "\n\n" + descriptionvalue + ": \n" + FeedDescription +
                                "\n\n" + linkvalue + ": \n" + FeedLink);
                    }

                    builder.setNegativeButton(getResources().getString(R.string.ok),null)
                            .setCancelable(true);
                    builder.create();
                    builder.show();

                }
                return true;
            default:
                return super.onoptionsItemSelected(item);
        }}

    // This is the factory to instantiate the RSSFragment instance with the url string as arguments
    public static RSSFragment newInstance(String url) {
        RSSFragment newInstance = new RSSFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_URL,url);
        newInstance.setArguments(bundle);
        return newInstance;
    }
}

fragment_list_refresh

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp">

            <com.Tamillive.newspaper.EnchantedViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="210dp" />

            <me.relex.circleindicator.CircleIndicator
                android:id="@+id/indicator_unselected_background"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="10dp"
                app:ci_drawable="@drawable/selecteditem_dot"
                app:ci_drawable_unselected="@drawable/nonselecteditem_dot"
                app:ci_height="6dp"
                app:ci_width="6dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@color/background_white"
            android:orientation="vertical">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/list"
                style="@style/listStylenopadding"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@null"
                android:focusable="false" />


        </LinearLayout>

    </LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmer_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:shimmer_duration="1000">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">



            <include
                android:id="@+id/lyt_shimmer_recipes_list_big"
                layout="@layout/include_shimmer_recipes_list_big" />





        </RelativeLayout>

    </com.facebook.shimmer.ShimmerFrameLayout>

</RelativeLayout>

include_shimmer_recipes_list

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="H,2:1.1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/bg_shimmer" />

            </RelativeLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="16dp"
                    android:layout_marginRight="100dp"
                    android:layout_marginEnd="100dp"
                    android:background="@drawable/bg_shimmer" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_marginTop="8dp"
                android:orientation="horizontal">

                <View
                    android:layout_width="100dp"
                    android:layout_height="12dp"
                    android:background="@drawable/bg_shimmer" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="16dp"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="H,2:1.1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/bg_shimmer" />

            </RelativeLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="16dp"
                    android:layout_marginRight="100dp"
                    android:layout_marginEnd="100dp"
                    android:background="@drawable/bg_shimmer" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:layout_marginTop="8dp"
                android:orientation="horizontal">

                <View
                    android:layout_width="100dp"
                    android:layout_height="12dp"
                    android:background="@drawable/bg_shimmer" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

这个布局应该在我有数据进展的时候出现。如果有人认识你,请帮帮我

解决方法

您可以使用来自 Facebook 的 ShimmerFrameLayout 库。

如果您想在加载 RecyclerView 项目时显示 Shimmer 效果,那么您可以按照此 link 在您的项目中实现它。

,

所以我从您的查询中了解到,一旦用户滑动到屏幕并开始显示进度,您就需要显示微光效果布局。

  • 您需要使用 onRefresh 对象实现 OnRefreshListenerSwipeRefreshLayout 方法。
  • 当用户滑动刷新屏幕时,上述方法中将有一个回调,您可以隐藏主布局并显示微光效果布局,一旦通过远程或本地数据源刷新数据,您就可以隐藏微光效果布局并在同一 RSS 片段中再次显示主要内容布局。

这是示例实现,也许对您有帮助:

val listener = object : SwipeRefreshLayout.OnRefreshListener {
        override fun onRefresh() {
            swipeRefreshLayout.visibility = View.GONE
            showShimmerAnimation()
        }
    }

swipeRefreshLayout.setOnRefreshListener(listener)

fun showShimmerAnimation() {
shimmer_view_container.apply {
       visibility = View.VISIBLE
       startAnimation()
   }
}

fun hideShimmerAnimation() {
shimmer_view_container.apply {
       visibility = View.GOE
       stopAnimation()
   }
}

fun onDataLoaded() {
    hideShimmerAnimation()
    swipeRefreshLayout.visibility = View.VISIBLE
}