这是布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="@+id/column"
android:layout_width="85dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="@string/column"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<com.example.demohlistview.InterceptScrollContainer
android:id="@+id/scroollContainter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/column"
android:focusable="false"
>
<com.example.demohlistview.MyHScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="@color/lineColor"
android:visibility="visible"
android:clickable="true"
android:focusableInTouchMode="true"
/>
<TextView
android:id="@+id/column1"
android:layout_width="85dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="@string/column1"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="@color/lineColor"
android:visibility="visible"/>
<TextView
android:id="@+id/column2"
android:layout_width="85dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="@string/column2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="@color/lineColor"
android:visibility="visible"/>
<TextView
android:id="@+id/column3"
android:layout_width="85dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="@string/column3"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</com.example.demohlistview.MyHScrollView>
</com.example.demohlistview.InterceptScrollContainer>
这是MyHSrollView:
public class MyHScrollView extends HorizontalScrollView {
ScrollViewObserver mScrollViewObserver = new ScrollViewObserver();
public MyHScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyHScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyHScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
Log.i("pdwy","MyHScrollView onTouchEvent");
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if (mScrollViewObserver != null /*&& (l != oldl || t != oldt)*/) {
mScrollViewObserver.NotifyOnScrollChanged(l, t, oldl, oldt);
}
super.onScrollChanged(l, t, oldl, oldt);
}
public void AddOnScrollChangedListener(OnScrollChangedListener listener) {
mScrollViewObserver.AddOnScrollChangedListener(listener);
}
public void RemoveOnScrollChangedListener(OnScrollChangedListener listener) {
mScrollViewObserver.RemoveOnScrollChangedListener(listener);
}
public static interface OnScrollChangedListener {
public void onScrollChanged(int l, int t, int oldl, int oldt);
}
public static class ScrollViewObserver {
List<OnScrollChangedListener> mList;
public ScrollViewObserver() {
super();
mList = new ArrayList<OnScrollChangedListener>();
}
public void AddOnScrollChangedListener(OnScrollChangedListener listener) {
mList.add(listener);
}
public void RemoveOnScrollChangedListener(
OnScrollChangedListener listener) {
mList.remove(listener);
}
public void NotifyOnScrollChanged(int l, int t, int oldl, int oldt) {
if (mList == null || mList.size() == 0) {
return;
}
for (int i = 0; i < mList.size(); i++) {
if (mList.get(i) != null) {
mList.get(i).onScrollChanged(l, t, oldl, oldt);
}
}
}
}
}
这是我的MainActivity
:
public class MainActivity extends Activity{
ListView mListView1;
MyAdapter myAdapter;
RelativeLayout mHead;
LinearLayout main;
TextView text1;
private static String TAG = "MainActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mHead = (RelativeLayout) findViewById(R.id.head);
mHead.setBackgroundResource(R.color.listHeadColor);
mHead.setOnTouchListener(new ListViewAndHeadViewTouchLinstener());
mListView1 = (ListView) findViewById(R.id.listView1);
myAdapter = new MyAdapter(this, R.layout.item);
mListView1.setAdapter(myAdapter);
mListView1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionevent) {
Log.e(TAG,"--------setOnTouchListener");
HorizontalScrollView headSrcrollView = (HorizontalScrollView) mHead
.findViewById(R.id.horizontalScrollView1);
headSrcrollView.onTouchEvent(motionevent);
return false;
}
});
mListView1.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view, int position,
long arg3) {
MyAdapter.ViewHolder holder = (MyAdapter.ViewHolder) view.getTag();
holder.txt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG, "--------onclick");
}
});
Log.e(TAG, "--------setOnItemClickListener");
myAdapter.setSelectedPosition(position);
myAdapter.notifyDataSetInvalidated();
}
});
}
class ListViewAndHeadViewTouchLinstener implements View.OnTouchListener {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
HorizontalScrollView headSrcrollView = (HorizontalScrollView) mHead
.findViewById(R.id.horizontalScrollView1);
headSrcrollView.onTouchEvent(arg1);
return false;
}
}
public class MyAdapter extends BaseAdapter {
public List<ViewHolder> mHolderList = new ArrayList<ViewHolder>();
int id_row_layout;
LayoutInflater mInflater;
private int selectedPosition = -1;
public MyAdapter(Context context, int id_row_layout) {
super();
this.id_row_layout = id_row_layout;
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 250;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
}
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
ViewHolder holder = null;
if (convertView == null) {
synchronized (MainActivity.this) {
convertView = mInflater.inflate(id_row_layout, null);
holder = new ViewHolder();
MyHScrollView scrollView1 = (MyHScrollView) convertView
.findViewById(R.id.horizontalScrollView1);
holder.scrollView = scrollView1;
holder.txt = (TextView) convertView
.findViewById(R.id.column);
holder.txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("jacob", "tex.onClick()");
}
});
holder.txt1 = (TextView) convertView
.findViewById(R.id.column1);
holder.txt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("jacob", "tex1111.onClick()!!!");
}
});
holder.txt2 = (TextView) convertView
.findViewById(R.id.column2);
holder.txt3 = (TextView) convertView
.findViewById(R.id.column3);
MyHScrollView headSrcrollView = (MyHScrollView) mHead
.findViewById(R.id.horizontalScrollView1);
headSrcrollView
.AddOnScrollChangedListener(new OnScrollChangedListenerImp(
scrollView1));
convertView.setTag(holder);
mHolderList.add(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt.setText(position + "" + 11);
holder.txt1.setText(position + "" + 1);
holder.txt2.setText(position + "" + 2);
holder.txt3.setText(position + "" + 3);
int intervalColor1 = getResources().getColor(R.color.intervalColor1);
int intervalColor2 = getResources().getColor(R.color.intervalColor2);
int[] colors = { intervalColor1, intervalColor2};
convertView.setBackgroundColor(colors[position % 2]);
if (selectedPosition == position) {
convertView.setBackgroundColor(Color.GREEN);
}
return convertView;
}
class OnScrollChangedListenerImp implements OnScrollChangedListener {
MyHScrollView mScrollViewArg;
public OnScrollChangedListenerImp(MyHScrollView scrollViewar) {
mScrollViewArg = scrollViewar;
}
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
mScrollViewArg.smoothScrollTo(l, t);
}
};
class ViewHolder{
TextView txt;
TextView txt1;
TextView txt2;
TextView txt3;
HorizontalScrollView scrollView;
}
}// end class my
}
InterceptScrollContainer
在这里:
public class InterceptScrollContainer extends LinearLayout {
public InterceptScrollContainer(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public InterceptScrollContainer(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.i("pdwy","ScrollContainer onInterceptTouchEvent");
return true;
}
}
令我困惑的是,scrollview外面的textview是可点击的,并在logcat中打印tex.onClick(),但holder.tet1是不可点击的,不打印“tex1111.onClick()!!!”< / p>
我的问题是如何使我的CUSTOM ScrollView中的所有TEXTVIEW可点击?我认为click事件在某处停止,而不是传递给目标textview