Android自定义View之双表头表格(表头不动,EditView和TextView两种方式)

本文主要是介绍Android自定义View之双表头表格(表头不动,EditView和TextView两种方式),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自定义View之双表头表格(表头不动)

1.图片展示
这里写图片描述
这里写图片描述
这里写图片描述

2 .功能分析
表头除一个外,可滑动,滑动方式为左右滑动
内容部分可上下滑动(除顶部一列)

3.解决方案
自定义View,对HorizontalScrollView进行重写
根据滑动事件来判断 : 上下滑动时,上表头不动,下面内容上下滑动
;左右滑动时,左表头不动,右方内容左右滑动

4.使用方法

内容是EditView的双表头

package com.example.lihailin.activity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.example.lihailin.R;
import com.example.lihailin.view.EditViewScrollView;import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;/*** * @author Lihailin EditView双表头表格*/
public class Listview1Activity extends Activity {private ListView mListView;public HorizontalScrollView mTouchView;protected List<EditViewScrollView> mHScrollViews = new ArrayList<EditViewScrollView>();HashMap<String, EditText> mColumnControls = new HashMap<String, EditText>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_listview1);initViews();}private void initViews() {String[] cols = { "title", "上标1", "上标2", "上标3", "上标4", "上标5", "上标6", "上标7", "上标8", "上标9", };// Table TitleLinearLayout titleLinearLayout = (LinearLayout) this.findViewById(R.id.scrollLinearLayout);for (int i = 0; i < cols.length; i++) {if (i != 0) {View linearLay = newView(R.layout.title_edit_view, "左标" + i);EditText et = (EditText) linearLay.findViewById(R.id.tevEditView);et.setText("上标" + i);titleLinearLayout.addView(linearLay);}}List<Map<String, String>> datas = new ArrayList<Map<String, String>>();Map<String, String> data = null;EditViewScrollView headerScroll = (EditViewScrollView) findViewById(R.id.item_scroll_title);// 添加头滑动事件mHScrollViews.add(headerScroll);mListView = (ListView) findViewById(R.id.scroll_list);for (int i = 0; i < 100; i++) {data = new HashMap<String, String>();data.put(cols[0], "左标" + i);data.put(cols[1], "上标" + 1 + "_" + "左标" + i);data.put(cols[2], "上标" + 2 + "_" + "左标" + i);data.put(cols[3], "上标" + 3 + "_" + "左标" + i);data.put(cols[4], "上标" + 4 + "_" + "左标" + i);data.put(cols[5], "上标" + 5 + "_" + "左标" + i);data.put(cols[6], "上标" + 6 + "_" + "左标" + i);data.put(cols[7], "上标" + 7 + "_" + "左标" + i);data.put(cols[8], "上标" + 8 + "_" + "左标" + i);data.put(cols[9], "上标" + 9 + "_" + "左标" + i);datas.add(data);}mColumnControls.clear();for (int i = 0; i < cols.length; i++) {if (i != 0) {EditText etItem1 = new EditText(Listview1Activity.this);etItem1.setWidth(50);etItem1.setTextColor(Color.DKGRAY);etItem1.setGravity(Gravity.CENTER);//mColumnControls.put(cols[i], etItem1);}}BaseAdapter adapter = new ScrollAdapter2(this, datas, R.layout.row_item_edit, cols);mListView.setAdapter(adapter);}public void addHViews(final EditViewScrollView hScrollView) {if (!mHScrollViews.isEmpty()) {int size = mHScrollViews.size();EditViewScrollView scrollView = mHScrollViews.get(size - 1);final int scrollX = scrollView.getScrollX();if (scrollX != 0) {mListView.post(new Runnable() {@Overridepublic void run() {hScrollView.scrollTo(scrollX, 0);}});}}mHScrollViews.add(hScrollView);}public void onScrollChanged(int l, int t, int oldl, int oldt) {for (EditViewScrollView scrollView : mHScrollViews) {if (mTouchView != scrollView)scrollView.smoothScrollTo(l, t);}}class ScrollAdapter2 extends BaseAdapter {private List<? extends Map<String, ?>> datas;private int res;private String[] from;private Context context;public ScrollAdapter2(Context context, List<? extends Map<String, ?>> data, int resource, String[] from) {this.context = context;this.datas = data;this.res = resource;this.from = from;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn datas.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn datas.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView v = convertView;if (v == null) {v = LayoutInflater.from(context).inflate(res, null);mColumnControls.put("左标", (EditText) v.findViewById(R.id.item_title));View chsv = v.findViewById(R.id.item_scroll);LinearLayout ll = (LinearLayout) chsv.findViewById(R.id.item_scroll_layout);View[] views = new View[from.length];for (int i = 0; i < from.length; i++) {if (i == 0) {views[0] = v.findViewById(R.id.item_title);continue;}View linearLay = newView(R.layout.item_edit_view, from[i]);EditText td = (EditText) linearLay.findViewById(R.id.ievEditView);td.setOnClickListener(clickListener);ll.addView(linearLay);views[i] = td;}//v.setTag(views);addHViews((EditViewScrollView) chsv);}View[] holders = (View[]) v.getTag();int len = holders.length;for (int i = 0; i < len; i++) {((EditText) holders[i]).setText(this.datas.get(position).get(from[i]).toString());}return v;}}private View newView(int res_id, String tag_name) {View itemView = LayoutInflater.from(Listview1Activity.this).inflate(res_id, null);itemView.setTag(tag_name);return itemView;}class ScrollAdapter extends SimpleAdapter {private List<? extends Map<String, ?>> datas;private int res;private String[] from;private int[] to;private Context context;public ScrollAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) {super(context, data, resource, from, to);this.context = context;this.datas = data;this.res = resource;this.from = from;this.to = to;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = convertView;if (v == null) {v = LayoutInflater.from(context).inflate(res, null);addHViews((EditViewScrollView) v.findViewById(R.id.item_scroll));View[] views = new View[to.length];//for (int i = 0; i < to.length; i++) {View tv = v.findViewById(to[i]);tv.setOnClickListener(clickListener);views[i] = tv;}v.setTag(views);}View[] holders = (View[]) v.getTag();int len = holders.length;for (int i = 0; i < len; i++) {((EditText) holders[i]).setText(this.datas.get(position).get(from[i]).toString());}return v;}}protected View.OnClickListener clickListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(Listview1Activity.this, ((EditText) v).getText(), Toast.LENGTH_SHORT).show();}};
}

内容是TextView的双表头

package com.example.lihailin.activity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.example.lihailin.R;
import com.example.lihailin.view.TextViewScrollView;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
/*** * @author Lihailin*TextViewView双表头表格*/
public class Listview2Activity extends Activity{private ListView mListView;public HorizontalScrollView mTouchView;protected List<TextViewScrollView> mHScrollViews =new ArrayList<TextViewScrollView>();private String[] cols = new String[] { "title", "data_1", "data_2", "data_3", "data_4", "data_5", "data_6","data_7","data_8", "data_9",};private  ScrollAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_listview2);initViews();}private void initViews() {List<Map<String, String>> datas = new ArrayList<Map<String,String>>();Map<String, String> data = null;TextViewScrollView headerScroll = (TextViewScrollView) findViewById(R.id.item_scroll_title);mHScrollViews.add(headerScroll);mListView = (ListView) findViewById(R.id.hlistview_scroll_list);for(int i = 0; i < 20; i++) {data = new HashMap<String, String>();data.put("title", "左标" + i);for (int j = 1; j < cols.length; j++) {data.put("data_" + j, "上标题" + j + "_" +"左标"+i );}datas.add(data);}mAdapter = new ScrollAdapter(this, datas, R.layout.item_listview//R.layout.item, cols, new int[] { R.id.item_titlev, R.id.item_datav1, R.id.item_datav2, R.id.item_datav3, R.id.item_datav4, R.id.item_datav5, R.id.item_datav6 , R.id.item_datav7 , R.id.item_datav8 });mListView.setAdapter(mAdapter);}public void addHViews(final TextViewScrollView hScrollView) {if(!mHScrollViews.isEmpty()) {int size = mHScrollViews.size();TextViewScrollView scrollView = mHScrollViews.get(size - 1);final int scrollX = scrollView.getScrollX();if(scrollX != 0) {//线程更新mListView.post(new Runnable() {@Overridepublic void run() {hScrollView.scrollTo(scrollX, 0);}});}}mHScrollViews.add(hScrollView);}public void onScrollChanged(int l, int t, int oldl, int oldt){for(TextViewScrollView scrollView : mHScrollViews) {//防止重复滑动if(mTouchView != scrollView)scrollView.smoothScrollTo(l, t);}}class ScrollAdapter extends SimpleAdapter {private List<? extends Map<String, ?>> datas;private int res;private String[] from;private int[] to;private Context context;public ScrollAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {super(context, data, resource, from, to);this.context = context;this.datas = data;this.res = resource;this.from = from;this.to = to;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = convertView;if(v == null) {v = LayoutInflater.from(context).inflate(res, null);//第一次初始化的时候装进来addHViews((TextViewScrollView) v.findViewById(R.id.item_chscroll_scroll));View[] views = new View[to.length];//单元格点击事件for(int i = 0; i < to.length; i++) {View tv = v.findViewById(to[i]);tv.setOnClickListener(clickListener);views[i] = tv;}v.setTag(views);}View[] holders = (View[]) v.getTag();int len = holders.length;for(int i = 0 ; i < len; i++) {((TextView)holders[i]).setText(this.datas.get(position).get(from[i]).toString());}return v;}}protected View.OnClickListener clickListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {v.setBackgroundResource(R.drawable.linearlayout_green_round_selector);Toast.makeText(Listview2Activity.this, "点击了:"+((TextView)v).getText(), Toast.LENGTH_SHORT).show();}};
}

5.自定义的View
自定义EditViewScrollView

package com.example.lihailin.view;import com.example.lihailin.activity.Listview1Activity;import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
/*** * @author Lihailin*自定义EditViewScrollView*/
public class EditViewScrollView extends HorizontalScrollView {Listview1Activity activity;public EditViewScrollView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);activity = (Listview1Activity) context;}public EditViewScrollView(Context context, AttributeSet attrs) {super(context, attrs);activity = (Listview1Activity) context;}public EditViewScrollView(Context context) {super(context);activity = (Listview1Activity) context;}@Overridepublic boolean onTouchEvent(MotionEvent ev) {//进行触摸赋值activity.mTouchView = this;return super.onTouchEvent(ev);}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {//当当前的CHSCrollView被触摸时,滑动其它if(activity.mTouchView == this) {activity.onScrollChanged(l, t, oldl, oldt);}else{super.onScrollChanged(l, t, oldl, oldt);}}
}

自定义的TextViewScrollView

package com.example.lihailin.view;import com.example.lihailin.activity.Listview2Activity;import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
/*** * @author Lihailin*自定义TextViewScrollView*/
public class TextViewScrollView extends HorizontalScrollView {Listview2Activity activity;public TextViewScrollView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);activity = (Listview2Activity) context;}public TextViewScrollView(Context context, AttributeSet attrs) {super(context, attrs);activity = (Listview2Activity) context;}public TextViewScrollView(Context context) {super(context);activity = (Listview2Activity) context;}@Overridepublic boolean onTouchEvent(MotionEvent ev) {//进行触摸赋值activity.mTouchView = this;return super.onTouchEvent(ev);}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {//当当前的CHSCrollView被触摸时,滑动其它if(activity.mTouchView == this) {activity.onScrollChanged(l, t, oldl, oldt);}else{super.onScrollChanged(l, t, oldl, oldt);}}
}

源码下载

这篇关于Android自定义View之双表头表格(表头不动,EditView和TextView两种方式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/522619

相关文章

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

内核启动时减少log的方式

内核引导选项 内核引导选项大体上可以分为两类:一类与设备无关、另一类与设备有关。与设备有关的引导选项多如牛毛,需要你自己阅读内核中的相应驱动程序源码以获取其能够接受的引导选项。比如,如果你想知道可以向 AHA1542 SCSI 驱动程序传递哪些引导选项,那么就查看 drivers/scsi/aha1542.c 文件,一般在前面 100 行注释里就可以找到所接受的引导选项说明。大多数选项是通过"_

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

用命令行的方式启动.netcore webapi

用命令行的方式启动.netcore web项目 进入指定的项目文件夹,比如我发布后的代码放在下面文件夹中 在此地址栏中输入“cmd”,打开命令提示符,进入到发布代码目录 命令行启动.netcore项目的命令为:  dotnet 项目启动文件.dll --urls="http://*:对外端口" --ip="本机ip" --port=项目内部端口 例: dotnet Imagine.M

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

深入理解RxJava:响应式编程的现代方式

在当今的软件开发世界中,异步编程和事件驱动的架构变得越来越重要。RxJava,作为响应式编程(Reactive Programming)的一个流行库,为Java和Android开发者提供了一种强大的方式来处理异步任务和事件流。本文将深入探讨RxJava的核心概念、优势以及如何在实际项目中应用它。 文章目录 💯 什么是RxJava?💯 响应式编程的优势💯 RxJava的核心概念

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

【即时通讯】轮询方式实现

技术栈 LayUI、jQuery实现前端效果。django4.2、django-ninja实现后端接口。 代码仓 - 后端 代码仓 - 前端 实现功能 首次访问页面并发送消息时需要设置昵称发送内容为空时要提示用户不能发送空消息前端定时获取消息,然后展示在页面上。 效果展示 首次发送需要设置昵称 发送消息与消息展示 提示用户不能发送空消息 后端接口 发送消息 DB = []@ro