我有一個片段,它由一個覆蓋在FrameLayout上的Button組成。第二個ListView片段被插入到FrameLayout中。除非觸摸事件在Button的頂部開始,否則ListView將正確滾動。 Button的onClick監聽器似乎攔截了ListView下方的滾動。我怎樣才能讓Button進程點擊事件,但不能滾動事件。Android按鈕onClick攔截滾動
我有一個基本的解決方案,使用Button上的GestureDetector並將onScroll事件傳遞給ListView的smoothScrollBy函數。儘管這對於投擲事件來說並不容易。
This question似乎相似,儘管情況相反。我不認爲接受的答案對我有幫助。
實施例:
main_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/sub_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#999"
android:text="Button" />
</RelativeLayout>
通過MainFragment充氣:
public class MainFragment extends Fragment {
private static final String TAG = MainFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "click");
}
});
FragmentTransaction fragmentTransaction = getActivity()
.getSupportFragmentManager().beginTransaction();
ListFragment f = new ListFragment();
fragmentTransaction.replace(R.id.sub_content, f);
fragmentTransaction.commit();
return view;
}
}
按鈕具有一個onClickListener和第二片段代替的膨脹FrameLayout:
fragment_item_list.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
通過ListFragment充氣:
public class ListFragment extends Fragment {
private static final String TAG = ListFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list,
container, false);
ListView listView = (ListView) view.findViewById(R.id.list);
List<String> list = new ArrayList<String>();
for (int i=0; i<30; i++) {
list.add("Item: " + i);
}
MyAdapter adapter = new MyAdapter(getActivity(), list);
listView.setAdapter(adapter);
return view;
}
private class MyAdapter extends BaseAdapter {
private List<String> items;
private Context context;
public MyAdapter(Context context, List<String> items) {
this.items = items;
this.context = context;
}
@Override
public int getCount() {
return items.size();
}
@Override
public String getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.list_item, parent, false);
}
TextView textView = (TextView) convertView.findViewById(
R.id.list_item_text);
textView.setText(getItem(i));
return convertView;
}
}
}