开源项目PullToZoomView使用
  mrMLeSzj1mp0 2023年11月02日 51 0


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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  b1UHV4WKBb2S   2023年11月13日   40   0   0 ide抗锯齿
  iD7FikcuyaVi   2023年11月30日   26   0   0 MacWindowsandroid
  b1UHV4WKBb2S   2023年11月13日   34   0   0 裁剪ideflutter
  zSWNgACtCQuP   2023年11月13日   32   0   0 ide
mrMLeSzj1mp0