效果图
与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); } }