美文网首页Android技术文程序员
Android实现上拉查看图文详情的一种想法

Android实现上拉查看图文详情的一种想法

作者: IAM四十二 | 来源:发表于2016-03-07 23:20 被阅读4159次

    在京东和淘宝的商品详情页都有这样一个上拉查看图文详情的操作,感觉很有意思,就用一种简单粗暴的方式简单实现了一下

    其实,第一次在手机上尝试这个功能的时候,想着这不就是一个类似于列表的上拉加载更多吗?于是就按照下拉刷新和上拉加载更多的思路进行了如下研究。这里借鉴PullToRefreshView 开源框架,对其中一些内容按需要做一些更改。

    • 首先看一下效果图
    这里写图片描述
    • 布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <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:orientation="vertical"
        tools:context="engineer.pulltomore.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <engineer.pulltomore.pullview.PullToRefreshViewUp
                android:id="@+id/RefreshUp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                       android:layout_height="match_parent"
                        android:orientation="vertical">
    
                        <ImageView
                            android:layout_width="match_parent"                        android:layout_height="match_parent"
                            android:scaleType="fitXY"
                            android:src="@drawable/im1" />
                    </LinearLayout>
                </ScrollView>
            </engineer.pulltomore.pullview.PullToRefreshViewUp>
    
            <engineer.pulltomore.pullview.PullToRefreshViewDown
                android:id="@+id/RefreshDown"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:visibility="gone">
    
                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
    
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:scaleType="fitXY"
                            android:src="@drawable/im2" />
                    </LinearLayout>
                </ScrollView>
            </engineer.pulltomore.pullview.PullToRefreshViewDown>
    
        </LinearLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:id="@+id/head_up"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#87cefa">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="商品信息"
                    android:padding="10dp"
                    android:textColor="#000000" />
            </LinearLayout>
    
            <LinearLayout
                android:visibility="gone"
                android:id="@+id/head_down"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#87cefa">
    
                <TextView
                    android:padding="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="图文详情"
                    android:textColor="#000000" />
            </LinearLayout>
    
        </FrameLayout>
    
    </RelativeLayout>
    
    

    这里的两个自定义view:PullToRefreshViewUpPullToRefreshViewDown,只是对开源框架PullToRefreshView内部就需要进行了一些修改。PullToRefreshViewUp去除了上拉时的动画效果,保留文字并作为查看图文详情的提示,PullToRefreshViewDown这个view只需要实现下拉刷新的效果即可,同样做了修改。

    最后,使用方法:

    public class MainActivity extends AppCompatActivity implements
            PullToRefreshViewUp.OnHeaderRefreshListener, PullToRefreshViewUp.OnFooterRefreshListener,
            PullToRefreshViewDown.OnHeaderRefreshListenerDown {
        PullToRefreshViewUp refreshUp;
        PullToRefreshViewDown refreshDown;
        LinearLayout headUp, headDown;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            refreshUp = (PullToRefreshViewUp) findViewById(R.id.RefreshUp);
            refreshUp.setOnHeaderRefreshListener(this);
            refreshUp.setOnFooterRefreshListener(this);
            refreshDown = (PullToRefreshViewDown) findViewById(R.id.RefreshDown);
            refreshDown.setOnHeaderRefreshListener(this);
            headUp = (LinearLayout) findViewById(R.id.head_up);
            headDown = (LinearLayout) findViewById(R.id.head_down);
        }
    
        @Override
        public void onHeaderRefresh(PullToRefreshViewUp view) {
            refreshUp.onHeaderRefreshComplete();
        }
    
        @Override
        public void onFooterRefresh(PullToRefreshViewUp view) {
            refreshUp.onFooterRefreshComplete();
            refreshUp.setVisibility(View.GONE);
            headUp.setVisibility(View.GONE);
            headDown.setVisibility(View.VISIBLE);
            refreshDown.setVisibility(View.VISIBLE);
    
        }
    
        @Override
        public void onHeaderRefresh(PullToRefreshViewDown view) {
            refreshDown.onHeaderRefreshComplete();
            refreshDown.setVisibility(View.GONE);
            headDown.setVisibility(View.GONE);
            refreshUp.setVisibility(View.VISIBLE);
            headUp.setVisibility(View.VISIBLE);
        }
    }
    

    好了,这就是整个实现方式。因为是通过隐藏显示两个view来实现,所以总体效果和京东淘宝仍有巨大差距,但这也只是一种思考,特此记录。

    相关文章

      网友评论

      • Carroliny:想用你的这两个控件PullToRefreshViewUp,PullToRefreshViewDown,请问有源码吗
      • psj_psj:楼主,对于商品详情的图文排序,有啥好的思路吗?
        IAM四十二: @psj_psj html+css 排好,用webview加载
      • SeanZ9:网上已经有这个Demo了,楼主可以找找啊
        TonyEasy:@lbaizxf613 如果下面那一页是ViewPager呢
        码无止境:@IAM四十二 两个scrollview搭配使用,效果还不错~
        IAM四十二: @SeanZ9 嗯,之前看过网上的是两个scrollview搭配,这里只是自己的一种思路。

      本文标题:Android实现上拉查看图文详情的一种想法

      本文链接:https://www.haomeiwen.com/subject/hrczkttx.html