当前位置: 首页 > 知识库问答 >
问题:

如何应用微光效果AndroidSwipeProgress?

金阳曜
2023-03-14

如何连接我的rssfragment应用微光效果SwipeProgress。当rssfragment正在进行时,我需要向您展示一个shimmer布局。我试过了,但lyt_shimmer没有显示给我看

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshItems();
        }
    });
    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;
    }
}
<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>
    <?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
        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
        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
        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>

共有1个答案

丁沛
2023-03-14

你可以从Facebook上使用ShimmerFrameLayout库。

如果您想在RecyclerView项加载时显示Shimmer效果,那么您可以遵循此链接在项目中实现它。

 类似资料:
  • 我试图实现shimmer效果的高度为wrap_content,但图像没有加载,我知道为什么它没有加载图像,因为imageView有wrap_content,而shimmer也有wrap_content,但我希望高度应该是wrap_content,而不是固定的。 在shimmer中实现eg 200dp的固定高度后,它工作,但之后图像不加载 我想让它像Pinterest一样根据图像调整高度 post_

  • 本文向大家介绍Android ShimmerLayout实现微光效果解析,包括了Android ShimmerLayout实现微光效果解析的使用技巧和注意事项,需要的朋友参考一下 前阵子在github上看到一个很不错的动画效果,叫做ShimmerLayout,是一个用于实现内部视图微光效果的布局。 如何实现 通过使用PorterDuff,我们可以制造出微光效果。PorterDuff是canvas绘

  • 1.1.1. lumenflinger 1.1.1. lumenflinger lumenflinger为灯光服务程序,源码路径:robot\services\lumenflinger 包括以下几个部分: 服务端:RKLumenFlinger.cpp IRKLumen.cpp 客户端:LumenClient.cpp LumenLight.cpp LumenEffects.cpp 其中 LumenL

  • 主要内容:JOGL 灯光效果的光源,JOGL 灯光效果 重要方法,JOGL 灯光效果 示例使用JOGL,您可以通过编码为任何类型的对象提供各种灯光效果。这种效果使物体在视觉上更好。 JOGL 灯光效果的光源 以下是一些重要的光源: Ambient Lightning:它总是赋予物体特定的颜色。 Diffuse Lightning:它从不同角度将光散射到物体上。 Specular Lightning:它模拟物体上的亮点 JOGL 灯光效果 重要方法 这些是用于为物体提供灯光效果的一些重要

  • 关于效果 Illustrator 包含各种效果,可以对某个对象、组或图层应用这些效果,以更改其特征。 向对象应用一个效果后,该效果会显示在“外观”面板中。从“外观”面板中,可以编辑、移动、复制、删除该效果或将它存储为图形样式的一部分。当使用一种效果时,必须先扩展对象,然后才能访问新点。 “效果”菜单上半部分的效果是矢量效果。在“外观”面板中,只能将这些效果应用于矢量对象,或者某个位图对象的填色或描

  • 本文向大家介绍UnityShader3实现彩光效果,包括了UnityShader3实现彩光效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了UnityShader3实现彩光效果展示的具体代码,供大家参考,具体内容如下 参考链接: 【OpenGL】Shader实例分析(八)- 彩色光圈 效果图: 这里我把它分三部分实现:1.彩色 2.光圈 3.动画 1.先实现彩色效果。分析一下那张彩