片段内的按钮没有在滚动视图中第一次触发?
Buttons inside fragments not firing first time in scroll view?
在我的应用程序中,主页有 5 个片段..
我正在使用相对布局显示五个片段。我厌倦了线性布局也不起作用..
这是我的布局代码
<android.support.v4.widget.NestedScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:http="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/background"
android:layout_width="match_parent"
android:id="@+id/scrollView"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
<fragment android:name="com.example.Fragment1"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="20dp"
tools:layout="@layout/fragment_need_card" />
<fragment android:name="com.example.Fragment2"
android:id="@+id/fragment2"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp"
android:layout_below="@+id/fragment1"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_discussion" />
<fragment android:name="com.example.Fragment3"
android:id="@+id/fragment3"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp"
android:layout_below="@+id/fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_askapro" />
<fragment android:name="com.example.Fragment4"
android:id="@+id/fragment4"
android:layout_width="match_parent"
android:focusable="true"
android:layout_below="@+id/askapro"
android:layout_height="wrap_content"
tools:layout="@layout/fragment3" />
<fragment android:name="com.example.Fragment5"
android:id="@+id/fragment5"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_below="@+id/fragment4"
android:layout_marginBottom="20dp"
android:layout_marginRight="8dp"
android:layout_height="fill_parent"
tools:layout="@layout/recent_articles" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
默认情况下 2 个片段在屏幕上可见,对于接下来的 3 个片段,用户必须垂直滚动..
public class Fragment3 扩展了 BaseFragment {
public Fragment3() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment3, container, false);
CardView b= (CardView)v.findViewById(R.id.cardview);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(getActivity(),DummyEvent.class);
startActivity(intent);
}
});
return v;
}
这个 onclick 监听器从来没有第一次触发,我必须点击两次..
我认为第三个片段没有得到 focused.may 是我在布局中绑定了片段吗?有人有解决问题的想法吗?
此问题是由于 android 支持库 NestedScrollView 中的错误所致。在这里能找到它 :
https://code.google.com/p/android/issues/detail?id=178041
这个问题很可能会在下一个版本中得到修复。到那时你可以从这个 post 看到解决方法:
如果您使用的是 androidX,请使用此更新 'androidx.appcompat:appcompat:1.1.0-alpha04'.
或更高版本。
在我的应用程序中,主页有 5 个片段..
我正在使用相对布局显示五个片段。我厌倦了线性布局也不起作用..
这是我的布局代码
<android.support.v4.widget.NestedScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:http="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@color/background"
android:layout_width="match_parent"
android:id="@+id/scrollView"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
>
<fragment android:name="com.example.Fragment1"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="20dp"
tools:layout="@layout/fragment_need_card" />
<fragment android:name="com.example.Fragment2"
android:id="@+id/fragment2"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp"
android:layout_below="@+id/fragment1"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_discussion" />
<fragment android:name="com.example.Fragment3"
android:id="@+id/fragment3"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="20dp"
android:layout_below="@+id/fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_askapro" />
<fragment android:name="com.example.Fragment4"
android:id="@+id/fragment4"
android:layout_width="match_parent"
android:focusable="true"
android:layout_below="@+id/askapro"
android:layout_height="wrap_content"
tools:layout="@layout/fragment3" />
<fragment android:name="com.example.Fragment5"
android:id="@+id/fragment5"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_below="@+id/fragment4"
android:layout_marginBottom="20dp"
android:layout_marginRight="8dp"
android:layout_height="fill_parent"
tools:layout="@layout/recent_articles" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
默认情况下 2 个片段在屏幕上可见,对于接下来的 3 个片段,用户必须垂直滚动..
public class Fragment3 扩展了 BaseFragment {
public Fragment3() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment3, container, false);
CardView b= (CardView)v.findViewById(R.id.cardview);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(getActivity(),DummyEvent.class);
startActivity(intent);
}
});
return v;
}
这个 onclick 监听器从来没有第一次触发,我必须点击两次..
我认为第三个片段没有得到 focused.may 是我在布局中绑定了片段吗?有人有解决问题的想法吗?
此问题是由于 android 支持库 NestedScrollView 中的错误所致。在这里能找到它 : https://code.google.com/p/android/issues/detail?id=178041
这个问题很可能会在下一个版本中得到修复。到那时你可以从这个 post 看到解决方法:
如果您使用的是 androidX,请使用此更新 'androidx.appcompat:appcompat:1.1.0-alpha04'.
或更高版本。