NestedScrollView嵌套ListView,RecyclerView
来源:互联网 发布:剑三的正太捏脸数据 编辑:程序博客网 时间:2024/04/30 05:07
NestedScrollView嵌套ListView,RecyclerView加上CollapsingToolbarLayout,
布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="#30469b"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/test"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:title="1111111111"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/ScrollView_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.cjf.animation.NestedListView
android:id="@+id/listview_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout_animation"></com.cjf.animation.NestedListView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_animation"></android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity代码如下:
package com.cjf.animation;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
public class LayoutAnimationActivity extends AppCompatActivity {
private NestedListView mListView;
private ArrayList<String> dataList;
private NestedScrollView ScrollView_test;
private RecyclerView recycleView_test;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_layoutanimation);
initView();
initData();
}
private void initView() {
mListView = (NestedListView) findViewById(R.id.listview_test);
recycleView_test= (RecyclerView) findViewById(R.id.recycleView_test);
ScrollView_test = (NestedScrollView) findViewById(R.id.ScrollView_test);
//解决recycleview与NestedScrollView滑动冲突,注意support:appcompat-v7,要23以上
LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
mLinearLayoutManager.setSmoothScrollbarEnabled(true);
mLinearLayoutManager.setAutoMeasureEnabled(true);
recycleView_test.setLayoutManager(mLinearLayoutManager);
recycleView_test.setHasFixedSize(true);
recycleView_test.setNestedScrollingEnabled(false);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上,设置到Toolbar上则不会显示
CollapsingToolbarLayout mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
mCollapsingToolbarLayout.setTitle("CollapsingToolbarLayout");
//通过CollapsingToolbarLayout修改字体颜色
mCollapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//设置还没收缩时状态下字体颜色
mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);//设置收缩后Toolbar上字体的颜色
}
private void initData() {
dataList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
dataList.add("这是测试数据" + i);
}
recycleView_test.setAdapter(new MyAdapter<String>(dataList,this));
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList));
}
}
Adapter:
package com.cjf.animation;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter<String> extends RecyclerView.Adapter {
private List<String> dataList;
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(ArrayList<String> dataList, Context context) {
this.dataList = dataList;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.listitem_test, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myholder = (MyViewHolder) holder;
myholder.tv_test.setText((CharSequence) dataList.get(position));
}
@Override
public int getItemCount() {
return dataList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv_test;
public MyViewHolder(View itemView) {
super(itemView);
tv_test = (TextView) itemView.findViewById(R.id.tv_test);
}
}
}
重写ListView:
package com.cjf.animation;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NestedListView extends ListView implements View.OnTouchListener,
AbsListView.OnScrollListener {
private int listViewTouchAction;
private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99;
public NestedListView(Context context, AttributeSet attrs) {
super(context, attrs);
listViewTouchAction = -1;
setOnScrollListener(this);
setOnTouchListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (getAdapter() != null
&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, -1);
}
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int newHeight = 0;
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
ListAdapter listAdapter = getAdapter();
if (listAdapter != null && !listAdapter.isEmpty()) {
int listPosition = 0;
for (listPosition = 0; listPosition < listAdapter.getCount()
&& listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) {
View listItem = listAdapter.getView(listPosition, null,
this);
// now it will not throw a NPE if listItem is a ViewGroup
// instance
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
listItem.measure(widthMeasureSpec, heightMeasureSpec);
newHeight += listItem.getMeasuredHeight();
}
newHeight += getDividerHeight() * listPosition;
}
if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) {
if (newHeight > heightSize) {
newHeight = heightSize;
}
}
} else {
newHeight = getMeasuredHeight();
}
setMeasuredDimension(getMeasuredWidth(), newHeight);
}
public boolean onTouch(View v, MotionEvent event) {
if (getAdapter() != null
&& getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE) {
scrollBy(0, 1);
}
}
return false;
}
}
测试基本实现,有Bug欢迎反馈,一起讨论解决。
阅读全文
0 0
- NestedScrollView嵌套ListView,RecyclerView
- NestedScrollView 嵌套 RecyclerView
- NestedScrollView嵌套RecyclerView (官方)
- NestedScrollView嵌套RecyclerView滑动冲突
- NestedScrollView 之 ScrollView 嵌套 ListView
- RecyclerView嵌套在NestedScrollView中的注意事项
- ScrollView/NestedScrollView嵌套RecyclerView滑动不流畅
- NestedScrollview 嵌套 RecyclerView出现滑动冲突解决方法
- RecyclerView嵌套在NestedScrollView中的注意事项
- CoordinatorLayout与NestedScrollView嵌套RecyclerView滑动问题
- 解决nestedScrollview 嵌套 recyclerview出现的异常
- NestedScrollView嵌套RecyclerView滑动卡顿解决方案
- NestedScrollView 嵌套 ListView 实现滑动折叠效果
- Scrollview (NestedScrollView) 嵌套 RecyclerView 的时候RecyclerView 抢焦点
- ListView嵌套ListView 或者RecyclerView嵌套ListView
- 解决NestedScrollView 嵌套 RecyclerView出现的滑动冲突问题
- Android之NestedScrollView 嵌套 RecyclerView 滑动冲突的问题
- NestedScrollView嵌套RecyclerView时滑动不流畅问题的解决办法
- python基本数据类型——int
- sklearn:点互信息和互信息
- N!末尾0的个数
- python基本数据类型——set
- Django Tutorial Part2
- NestedScrollView嵌套ListView,RecyclerView
- 页面自动跳转实现方法
- python版本与编码的区别
- LinkCode 下一个排列、上一个排列
- Verilog中if..else和case语句分析
- C#_在VS2010下进行单元测试
- Tornado Web 框架
- LinkCode 第k个排列
- 应用机器学习(八):线性模型