我有一個列表視圖,其中每個項目是textview和自定義視圖。這裏是ListView項的佈局:如何將同一個動作事件傳遞給listview中的所有項目?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<net.manualuser.calibr.TimeScale
android:id="@+id/my_scale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="40dp">
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
時間表這裏是繪製一個統治者和處理水平滾動的自定義視圖。
我想將相同的動作事件傳遞給每個項目的標尺,以便當我從列表中滾動一個時,其他人同時滾動。
在適配器中,我將偵聽器設置爲自定義視圖。這裏是getView部分:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
if(view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.time_scale_item, viewGroup, false);
}
scale = (TimeScale)v.findViewById(R.id.my_scale);
scale.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
scale.onTouchEvent(event);
return false;
}
});
TextView text = (TextView)v.findViewById(R.id.counter);
text.setText(Cities.get(i));
return v;
}
在主要活動中找到我的列表,並設置適配器給它。 比方說,我有4個項目在列表視圖。但只有2個同時移動。將相同的運動事件傳遞給列表中的所有統治者的正確方法是什麼?
它的工作原理部分。在兩個項目的列表中,當我滾動第一個時,第二個也移動,但不是相反,就好像在第二個視圖上沒有設置ontouchlistener一樣。但是,當列表中有三個項目時,它們會成對滾動:一個,三個,兩個,最後一個僅滾動自己。 –