致新手 android中ScrollView嵌套listview
来源:互联网 发布:50道js经典逻辑题 编辑:程序博客网 时间:2024/06/06 05:21
先看效果 画质有点垃圾 格式工厂转换的
布局文件
<?xml version="1.0" encoding="utf-8"?><ScrollView android:layout_height="match_parent" android:layout_width="match_parent" android:fillViewport="true" android:id="@+id/sv" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 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.scrollviewandlistview.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="嵌套个listview看看" android:textColor="@color/colorAccent" android:textSize="19sp"/> <com.example.scrollviewandlistview.ListViewForScorollView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="200dp"> </com.example.scrollviewandlistview.ListViewForScorollView> <ImageView android:layout_width="match_parent" android:layout_height="500dp" android:background="@mipmap/ic_launcher"/> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:background="@mipmap/ic_launcher"/></LinearLayout></ScrollView>activity 里的代码
package com.example.scrollviewandlistview;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.LinearLayout;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.ScrollView;import android.widget.Toast;import java.util.List;public class MainActivity extends AppCompatActivity { private static final String TAG = "cj"; private String[] arrays ={"河南","河北","山东","山西","广西","广东","北京","上海", "四川", "甘肃","内蒙古","青海","陕西","云南","天津","重庆","辽宁","吉林","黑龙江","湖南", "湖北","安徽", "新疆","江苏","浙江","江西","福建","贵州","西藏","宁夏","海南","台湾","香港","澳门"}; private ListViewForScorollView mListview1; private ScrollView scrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mListview1 = (ListViewForScorollView) findViewById(R.id.listview); mListview1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrays)); //下面这三行 得到listview里面有多少个条目 ListAdapter listadapter = mListview1.getAdapter(); int count = listadapter.getCount(); Log.e(TAG, "listview里有多少个条目: "+count ); scrollView = (ScrollView) findViewById(R.id.sv); scrollView.smoothScrollTo(0,0); mListview1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_UP) { scrollView.requestDisallowInterceptTouchEvent(false);//最重要的一句 }else { scrollView.requestDisallowInterceptTouchEvent(true); } return false; } }); mListview1.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String sheng = arrays[position]; Toast.makeText(MainActivity.this, sheng, Toast.LENGTH_SHORT).show(); } }); } //动态设置listview的高度(根据儿子的个数) // 如果在listview条目较少的情况下, // 可以使用这一种方式,将listview的条目全部显示出来 //但是如果想让listview也能滑动情况下就得重写listview了 //最重要的是lv的ontouchListener方法,得到事件的消费焦点 public void setListViewHeightBasedOnChildren(ListView listView) { if(listView == null) return; ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); }}重写的listview
package com.example.scrollviewandlistview;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * Created by Administrator on 2016/10/15. */public class ListViewForScorollView extends ListView { public ListViewForScorollView(Context context) { super(context); } public ListViewForScorollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewForScorollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int gaodu = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);//显示所有条目 int gaodu2 = MeasureSpec.makeMeasureSpec(500, MeasureSpec.AT_MOST);//指定listview的高度 super.onMeasure(widthMeasureSpec, gaodu2); }}
0 0
- 致新手 android中ScrollView嵌套listview
- Android中ScrollView嵌套ListView
- Android ScrollView中嵌套ListView
- Android ScrollView中嵌套ListView
- Android中ScrollView嵌套ListView
- android---ScrollView中嵌套ListView
- Android ScrollView中嵌套ListView
- android ScrollView中嵌套ListView显示问题
- Android: 如何在ScrollView中嵌套ListView
- android 中 ScrollView里嵌套ListView
- Android ScrollView中嵌套ListView和TextView
- Android:如何在ScrollView中嵌套ListView
- Android -- 在ScrollView中嵌套ListView
- android开发中使用scrollView嵌套Listview
- Android ScrollView中嵌套ListView或ExpandableListView
- android事件分发ScrollView中嵌套listview
- Android: 如何在ScrollView中嵌套ListView
- Android--ScrollView中嵌套使用ListView
- MP3光标
- Linux新手怎样更好地学习各种linux命令
- [C语言] leetcode 118. Pascal's Triangle
- 关于Algorithm IO3模拟赛的总结
- 总结2016.10.7
- 致新手 android中ScrollView嵌套listview
- HOG特征
- Android中AsyncTask和Handler对比
- 【Quadcopter】Content
- 剑指offer:不用加减乘除做加法(java)
- 二叉树的非递归遍历
- 渔盘 融合 无脑定投
- 给 Android 初学者的 Gradle 知识普及
- GNU Linux核心命令和工具的源代码路径