1. 程式人生 > >android_NestedScrollView與listView、recyclerView滑動衝突

android_NestedScrollView與listView、recyclerView滑動衝突

效果圖

與recyclerView

gradle:

compile 'com.android.support:design:26.0.0-alpha1'//材料設計語言
compile 'com.jakewharton:butterknife:8.5.1'//butterknife可以實現繫結控制元件
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'//butterknife可以實現繫結控制元件
compile 'com.zhy:base-rvadapter:3.0.3'//recyclerView萬能介面卡

item

<?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="wrap_content">
    <TextView
        android:id="@+id/text"
        android:text="文字"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp" />
    <View
        android:background="#F5F5F5"
        android:layout_width="match_parent"
        android:layout_height="2dp"></View>
</LinearLayout>

layout

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.test.MainActivity">
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:descendantFocusability="blocksDescendants"//注意這句話
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:background="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="120dp" /> <android.support.v7.widget.RecyclerView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> </android.support.v4.widget.NestedScrollView> </LinearLayout>

java

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.listView)
    RecyclerView recyclerView;
    List<String> strings= Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

       recyclerView.setAdapter(new com.zhy.adapter.recyclerview.CommonAdapter(MainActivity.this,R.layout.item,strings) {
           @Override
           protected void convert(com.zhy.adapter.recyclerview.base.ViewHolder holder, Object o, final int position) {
               holder.setText(R.id.text,strings.get(position));
               holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                   }
               });
           }
       });
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);





    }
}

//NestedScrollView與recyclerView就直接用就行了

//android:descendantFocusability="blocksDescendants"//注意這句話

與listView

gradle:

compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
compile 'com.zhy:base-adapter:3.0.3'

item

<?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="wrap_content">
    <TextView
        android:id="@+id/text"
        android:text="文字"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp" />
    <View
        android:background="#F5F5F5"
        android:layout_width="match_parent"
        android:layout_height="2dp"></View>
</LinearLayout>

 

layout:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.iamchan.test.MainActivity">
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:descendantFocusability="blocksDescendants"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:background="@mipmap/ic_launcher"
                android:layout_width="match_parent"
                android:layout_height="120dp" />
            <com.example.iamchan.test.MyListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </com.example.iamchan.test.MyListView>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

java

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.listView)
    ListView listView;
    List<String> strings= Arrays.asList("a","b","c","d","e","f","g","h","i","j","k","l","m","n");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

       listView.setAdapter(new CommonAdapter(MainActivity.this,R.layout.item,strings) {
           @Override
           protected void convert(ViewHolder viewHolder, Object item, int position) {
               viewHolder.setText(R.id.text,strings.get(position));
           }
        });
    }
}

自定義view

public class MyListView extends ListView {
    public MyListView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, mExpandSpec);
    }
}