In this tutorial we’ll override the ListView class to suit it according to our requirements in the android application.

在本教程中,我们将根据Android应用程序中的要求覆盖ListView类以使其适合您。

Android非滚动ListView要求 (Android Non Scrollable ListView Requirement)

A ListView comes up with its own default scrolling methods. Now if we wish to create a layout which contains a ListView along with some other views inside a parent ScrollView then you’ll notice that the scrolling gestures of the ListView don’t work as desired.

ListView带有其自己的默认滚动方法。 现在,如果我们希望创建一个包含ListView以及父ScrollView内其他视图的布局,那么您会注意到ListView的滚动手势无法正常工作。

The reason for this is that scrolling gestures received by the layout, they are all handled by the parent layout only. One workaround is to add the other views as the headers and footers of the ListView and avoid using ListView and ScrollView. But a more robust option is to create custom ListView class to suit it to our needs by making it non scrollable.

这样做的原因是,布局接收到的滚动手势都仅由父布局处理。 一种解决方法是将其他视图添加为ListView的页眉和页脚,并避免使用ListView和ScrollView。 但是,更可靠的选择是创建自定义ListView类 ,使其成为不可滚动的,从而使其满足我们的需求。

In this tutorial we’ll develop a custom ListView class and use it in a ScrollView with other child views. We’ll use Butterknife to bind the views.

在本教程中,我们将开发一个自定义ListView类,并将其在ScrollView中与其他子视图一起使用。 我们将使用Butterknife绑定视图。

项目结构 (Project Structure)

The project consists of a MainActivity and a subclass of ListView named NonScrollListView.

该项目包含一个MainActivity和一个名为NonScrollListView的ListView子类。

码 (Code)

The NonScrollListView class is given below:

下面给出了NonScrollListView类:

public class NonScrollListView extends ListView {public NonScrollListView(Context context) {super(context);}public NonScrollListView(Context context, AttributeSet attrs) {super(context, attrs);}public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);ViewGroup.LayoutParams params = getLayoutParams();params.height = getMeasuredHeight();}
}

onMeasure() allows us to specify how big you want your custom view to be with respect to the layout constraints of the parent class. AT_MOST typically means the layout_width or layout_height value was set to match_parent or wrap_content where a maximum size is needed (this is layout dependent in the framework), and the size of the parent dimension is the value.

onMeasure()允许我们相对于父类的布局约束指定您希望自定义视图的大小。 AT_MOST通常表示layout_widthlayout_heigh吨值设定为match_parentwrap_content其中需要的最大尺寸(这是布局依赖于框架),并且父维度的大小的值。

The layout of the MainActivity is given below:
activity_main.xml

MainActivity的布局如下:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:fillViewport="true"><RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/header_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_alignParentStart="true"android:layout_alignParentTop="true"android:background="#36D2AA"android:padding="@dimen/activity_horizontal_margin"android:text="Header Button" /><com.journaldev.nonscrollablelistview.NonScrollListViewandroid:id="@+id/listView"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_below="@+id/header_button"></com.journaldev.nonscrollablelistview.NonScrollListView><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_below="@+id/button2"android:padding="@dimen/activity_horizontal_margin"android:text="Footer Button 3" /><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_below="@+id/button1"android:padding="@dimen/activity_horizontal_margin"android:text="Footer Button 2" /><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_below="@+id/listView"android:text="Footer Button 1" /></RelativeLayout>
</ScrollView>

The package name of the custom ListView class is set as the tag. The layout consists of four buttons inside a RelativeLayout that’s the child of the a ScrollView.

自定义ListView类的包名称设置为标记。 布局由RelativeLayout内的四个按钮组成,它是ScrollView的子级。

The MainActivity.java is given below:

MainActivity.java如下所示:

package com.journaldev.nonscrollablelistview;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnItemSelected;public class MainActivity extends AppCompatActivity {@InjectView(R.id.listView)NonScrollListView nonScrollListView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.inject(this);final List<String[]> values = new LinkedList<String[]>();values.add(new String[]{"Title 1", "Subtitle 1"});values.add(new String[]{"Title 2", "Subtitle 2"});values.add(new String[]{"Title 3", "Subtitle 3"});values.add(new String[]{"Title 4", "Subtitle 4"});values.add(new String[]{"Title 5", "Subtitle 5"});values.add(new String[]{"Title 6", "Subtitle 6"});values.add(new String[]{"Title 7", "Subtitle 7"});values.add(new String[]{"Title 8", "Subtitle 8"});nonScrollListView.setAdapter(new ArrayAdapter<String[]>(MainActivity.this, android.R.layout.simple_expandable_list_item_2, android.R.id.text1, values) {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = super.getView(position, convertView, parent);String[] entry = values.get(position);TextView text1 = (TextView) view.findViewById(android.R.id.text1);TextView text2 = (TextView) view.findViewById(android.R.id.text2);text1.setText(entry[0]);text2.setText(entry[1]);return view;}});}@Nullable@OnClick({ R.id.header_button, R.id.button1,R.id.button2 , R.id.button3})public void commonMethod() {Toast.makeText(getApplicationContext(),"Button is clicked",Toast.LENGTH_SHORT).show();}@OnItemClick(R.id.listView)void onItemClick(int position) {Toast.makeText(getApplicationContext(),"Position "+position+" is clicked",Toast.LENGTH_SHORT).show();}}

We’ve used a new default list layout android.R.layout.simple_expandable_list_item_2. It consists of two textViews that can be only invoked using ids : android.R.id.text1 and android.R.id.text2. The ListView is populated using a LinkedList of String arrays.

我们使用了新的默认列表布局android.R.layout.simple_expandable_list_item_2 。 它由两个只能使用id调用的textView组成: android.R.id.text1android.R.id.text2 。 使用String数组的LinkedList填充ListView。

The output of the application in action is given below:

实际应用程序的输出如下:

Note: Replace the NonScrollListView with a normal ListView in the xml and the code. The following output is returned.

注意 :将xml和代码中的NonScrollListView替换为普通的ListView。 返回以下输出。

As you can see the ListView default scrolls conflict with the ScrollView’s hence the gestures are not handled properly.

如您所见,ListView默认滚动与ScrollView冲突,因此手势处理不正确。

替代方式 (Alternate Way)

A workaround to the above problem without creating a custom widget is given below:

下面给出了不创建自定义窗口小部件的上述问题的解决方法:

listView.setOnTouchListener(new ListView.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:// Disallow ScrollView to intercept touch events.v.getParent().requestDisallowInterceptTouchEvent(true);break;case MotionEvent.ACTION_UP:// Allow ScrollView to intercept touch events.v.getParent().requestDisallowInterceptTouchEvent(false);break;}// Handle ListView touch events.v.onTouchEvent(event);return true;}});

This brings an end to this tutorial. You can download the final Android Custom Non Scrollable ListView Project from the link below.

本教程到此结束。 您可以从下面的链接下载最终的Android自定义非滚动ListView项目

Download Android Custom ListView Project下载Android自定义ListView项目

翻译自: https://www.journaldev.com/10444/android-custom-listview-non-scrollable

Android自定义ListView示例,以创建不可滚动的ListView相关推荐

  1. android 自定义心电图,手把手教你打造一个心电图效果View Android自定义View(示例代码)...

    大家好,看我像不像蘑菇-因为我在学校呆的发霉了. 思而不学则殆 丽丽说得对,我有奇怪的疑问,大都是思而不学造成的,在我书读不够的情况下想太多,大多等于白想 ,所以革命没成功,同志仍需努力. 好了废话不 ...

  2. Android自定义View——仿1号店垂直滚动广告条实现

    效果图 原理分析 整个过程都是基于坐标Y的增加和交换进行处理的,Y值都会一直增加到endY,然后进行交换逻辑 实现步骤 1.初始化变量 由于1号店是两句话的滚动,所以我们也是使用两句话来实现的 pub ...

  3. android自定义dialog不显示,有关问题解决之Android自定义Dialog无法dismiss

    问题解决之Android自定义Dialog无法dismiss 场景: 点击ListView的一个Item,弹出自定义Dialog.在初始化Dialog时,将一个OnClickListener作为参数传 ...

  4. Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表

    本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...

  5. Android:自定义滚动边缘(EdgeEffect)效果

    Android可滚动控件(GridView.ListView.ScrollView等)当用户滚动到头的时候会有个边缘反馈效果,在4.0上默认为Holo蓝色效果. 如果您的App自定义了主题颜色,比如G ...

  6. Android ListView示例教程

    We will learn how to create a simple Android ListView and launch a new activity on selecting a singl ...

  7. Android自定义view原理及自定义View示例

    自定义view如何分类 自定义View:只需要重写onMeasure()和onDraw(),在没有现成的View,需要自己实现的时候,就使用自定义View,一般继承自View,SurfaceView或 ...

  8. Android自定义Adapter的ListView的思路及代码

    Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下 width="650" height="200" align=&quo ...

  9. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

    1.概述 群里的一个哥们有个需求是这样的:问题:主要功能就是:1.循环的一个滑动:2.每次滑动结束,保持每个Item的完整.然后我当时给他写了个Demo,所有代码都在Activity里面,后期看来其太 ...

最新文章

  1. 百度人脸检测与识别项目资源
  2. 【动态规划】爱与愁的心痛
  3. linux shell字符串及字符串长度获取方式
  4. httpservletresponse 一次导出多个word_报表工具能实现怎么的导出效果?
  5. jquery如何获取元素的滚动高度
  6. IntelliJ IDEA: 无法创建Java Class文件
  7. php下拉选项登录_php下拉选项的批量操作的实现代码
  8. 该文件夹包含名称过长且无法放入回收站的项目_微软复活20年前生产力工具PowerToys,填补Win10缺失功能,开源且免费...
  9. SQL中的函数 •Aggregate 函数 •Scalar 函数
  10. 全参考视频质量评价方法(PSNR,SSIM)以及相关数据库
  11. spring mvc 提供的几个常用的扩展点
  12. [华为] 华为交换机接口配置报错指南
  13. Java学习 day7 (常用API)Scanner类.Random类.Arraylist类
  14. 定制材料 Pd基聚多巴胺包裹碳纳米管/Fe或Cr单原子链填充Cu纳米管/Fe@CuNT和Cr@CuNT复合结构/氧化钼包裹碳纳米管纳米复合纤维
  15. python搭建邮件服务器_手把手教你使用Python轻松搞定发邮件
  16. 给 32 位系统装 8g 内存条能用吗?为什么?
  17. Hive常用函数介绍(窗口函数)
  18. 招沿实业学生怎样才能做好投资理财工作
  19. 物联网(IOT)之常见物联网通信技术概览-无线篇②
  20. 用python抓取津房置换的房屋挂牌交易数据

热门文章

  1. [转载] python list中append()与extend()用法
  2. [转载] python输入一个年份、输出是否为闰年_Python程序检查给定年份是否为闰年
  3. [转载] python中try Except抛出异常使用方法
  4. `ll/sc` 指令在`linux`中的软件实现
  5. 从今天开始 好好规划自己
  6. 【例题 7-2 UVA - 11059】Maximum Product
  7. 作业1-3 求1+2!+3!+...+20!的和
  8. WPF Demo20 模板
  9. Betsy Ross Problem
  10. 数据结构上机实践第四周项目6- 循环双链表应用