本文主要是介绍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两种方式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!