編輯:關於Android編程
問題背景:在做圖表展示的時候,ListView可以上下左右滑動,但最左邊一列在向右滑動時,保持不變,表頭在向下滑動時保持不變。
有用兩個ListView實現的,但測試過,好像有些問題
這個例子是通過(ListView + HorizontalScrollView)實現的
效果圖:
Activity代碼
/** * * 帶滑動表頭與固定列的ListView */ public class HListActivity extends Activity{ private ListView mListView; //方便測試,直接寫的public public HorizontalScrollView mTouchView; //裝入所有的HScrollView protected List<CHScrollView> mHScrollViews =new ArrayList<CHScrollView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scroll); initViews(); } private void initViews() { List<Map<String, String>> datas = new ArrayList<Map<String,String>>(); Map<String, String> data = null; CHScrollView headerScroll = (CHScrollView) 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("title", "Title_" + i); data.put("data_" + 1, "Date_" + 1 + "_" +i ); data.put("data_" + 2, "Date_" + 2 + "_" +i ); data.put("data_" + 3, "Date_" + 3 + "_" +i ); data.put("data_" + 4, "Date_" + 4 + "_" +i ); data.put("data_" + 5, "Date_" + 5 + "_" +i ); data.put("data_" + 6, "Date_" + 6 + "_" +i ); datas.add(data); } SimpleAdapter adapter = new ScrollAdapter(this, datas, R.layout.item , new String[] { "title", "data_1", "data_2", "data_3", "data_4", "data_5", "data_6", } , new int[] { R.id.item_title , R.id.item_data1 , R.id.item_data2 , R.id.item_data3 , R.id.item_data4 , R.id.item_data5 , R.id.item_data6 }); mListView.setAdapter(adapter); } public void addHViews(final CHScrollView hScrollView) { if(!mHScrollViews.isEmpty()) { int size = mHScrollViews.size(); CHScrollView scrollView = mHScrollViews.get(size - 1); final int scrollX = scrollView.getScrollX(); //第一次滿屏後,向下滑動,有一條數據在開始時未加入 if(scrollX != 0) { mListView.post(new Runnable() { @Override public void run() { //當listView刷新完成之後,把該條移動到最終位置 hScrollView.scrollTo(scrollX, 0); } }); } } mHScrollViews.add(hScrollView); } public void onScrollChanged(int l, int t, int oldl, int oldt){ for(CHScrollView 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; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if(v == null) { v = LayoutInflater.from(context).inflate(res, null); //第一次初始化的時候裝進來 addHViews((CHScrollView) 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++) { ((TextView)holders[i]).setText(this.datas.get(position).get(from[i]).toString()); } return v; } } //測試點擊的事件 protected View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(HListActivity.this, ((TextView)v).getText(), Toast.LENGTH_SHORT).show(); } }; }
滑動view代碼
/** * 滑動代碼 */ public class CHScrollView extends HorizontalScrollView { HListActivity activity; public CHScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); activity = (HListActivity) context; } public CHScrollView(Context context, AttributeSet attrs) { super(context, attrs); activity = (HListActivity) context; } public CHScrollView(Context context) { super(context); activity = (HListActivity) context; } @Override public boolean onTouchEvent(MotionEvent ev) { //進行觸摸賦值 activity.mTouchView = this; return super.onTouchEvent(ev); } @Override protected 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程序開發之ListView實現橫向滾動(帶表頭與固定列),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!
安裝cygwin:由於NDK編譯代碼時必須要用到make和gcc,所以你必須先搭建一個linux環境, cygwin是一個在windows平台上運行的unix模擬環境,它
前言:可能Android的事件分發對於剛學Android的童鞋來說接觸得不多,這樣不奇怪。因為剛學的時候,一般人很難注意到或是會選擇主動去了解。那麼究竟什麼是Androi
推薦閱讀:使用RecyclerView添加Header和Footer的方法
要實現網絡數據下載實現瀑布流效果;實現思路很簡單,主要是熟悉框架的運用。首先要寫一個通用的MyRequest類,用於從網上獲取json數據用Gson解析並返回bean對象