开源项目PullToZoomView使用

public class MainActivity extends AppCompatActivity {
    private List<String> datas;
    private RecyclerView recyclerView;
    private CustomScrollView scrollView;
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recview);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_title);
        initData();
        loadViewForCode();
        CustomAdapter adapter = new CustomAdapter(this,datas);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        scrollView.setOnScrollViewChangedListener(new CustomScrollView.OnScrollViewChangedListener() {
            @Override
            public void onScrollChanged(int x, int y, int oldx, int oldy) {
                if(y>80){
                    relativeLayout.setVisibility(View.VISIBLE);

                }else{
                    relativeLayout.setVisibility(View.GONE);
                }
            }
        });

    }

    public void initData() {
        scrollView.smoothScrollTo(0,0);
        datas = new ArrayList<>();
        for (int i = 'A'; i < 'Z'; i++) {
            datas.add(""+(char)i);
        }
    }
    private void loadViewForCode() {
        PullToZoomScrollViewEx scrollView = (PullToZoomScrollViewEx) findViewById(R.id.scroll_view);
        View headView = LayoutInflater.from(this).inflate(R.layout.activity_headview, null, false);
       relativeLayout = (RelativeLayout) headView.findViewById(R.id.rl_title);
        View zoomView = LayoutInflater.from(this).inflate(R.layout.activity_zoomview, null, false);
        View contentView = LayoutInflater.from(this).inflate(R.layout.activity_content, null, false);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.recview);
        scrollView.setHeaderView(headView);
        scrollView.setZoomView(zoomView);
        scrollView.setScrollContentView(contentView);
    }

}
public class CustomScrollView extends ScrollView {

    public interface OnScrollViewChangedListener {
        void onScrollChanged(int l, int t, int oldl, int oldt);
    }

    public OnScrollViewChangedListener mListener;

    public void setOnScrollViewChangedListener(OnScrollViewChangedListener listener){
        mListener = listener;
    }

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mListener!=null){
            mListener.onScrollChanged(l, t, oldl, oldt);
        }
    }
}

主页面布局文件

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

    <com.ecloud.pulltozoomview.PullToZoomScrollViewEx
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

头布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@drawable/ym2"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="蝙蝠侠大战超人"
    android:textSize="26sp"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginBottom="40dp"
    android:textColor="#ffffff"
    />
    <RelativeLayout
        android:id="@+id/rl_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorAccent"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蝙蝠侠大战超人"
            android:textSize="20sp"
            android:textColor="#ffffff"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
</FrameLayout>

缩放控件

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/iv_zoom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ym2"
    android:scaleType="centerCrop"
    android:layout_gravity="center_horizontal"
    android:contentDescription="@string/app_name" />

内容布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    private Context context;
    private List<String> data;
    public CustomAdapter(Context context, List<String> data){
        this.context = context;
        this.data = data;

    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.item_view, null);
        MyViewHolder holder = new MyViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(data.get(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值