1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.gallery3d.filtershow.category; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.database.DataSetObserver; 22 import android.graphics.Rect; 23 import android.util.AttributeSet; 24 import android.util.Log; 25 import android.view.MotionEvent; 26 import android.view.View; 27 import android.widget.LinearLayout; 28 import com.android.gallery3d.R; 29 30 public class CategoryTrack extends LinearLayout { 31 32 private CategoryAdapter mAdapter; 33 private int mElemSize; 34 private View mSelectedView; 35 private float mStartTouchY; 36 private DataSetObserver mDataSetObserver = new DataSetObserver() { 37 @Override 38 public void onChanged() { 39 super.onChanged(); 40 if (getChildCount() != mAdapter.getCount()) { 41 fillContent(); 42 } else { 43 invalidate(); 44 } 45 } 46 @Override 47 public void onInvalidated() { 48 super.onInvalidated(); 49 fillContent(); 50 } 51 }; 52 53 public CategoryTrack(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CategoryTrack); 56 mElemSize = a.getDimensionPixelSize(R.styleable.CategoryTrack_iconSize, 0); 57 } 58 59 public void setAdapter(CategoryAdapter adapter) { 60 mAdapter = adapter; 61 mAdapter.registerDataSetObserver(mDataSetObserver); 62 fillContent(); 63 } 64 65 public void fillContent() { 66 removeAllViews(); 67 mAdapter.setItemWidth(mElemSize); 68 mAdapter.setItemHeight(LayoutParams.MATCH_PARENT); 69 int n = mAdapter.getCount(); 70 for (int i = 0; i < n; i++) { 71 View view = mAdapter.getView(i, null, this); 72 addView(view, i); 73 } 74 requestLayout(); 75 } 76 77 @Override 78 public void invalidate() { 79 for (int i = 0; i < this.getChildCount(); i++) { 80 View child = getChildAt(i); 81 child.invalidate(); 82 } 83 } 84 85 } 86