我正在开发一个应用程序,它由一个只有 2 个页面的 View 分页器和每个页面中的 LinearLayout 组成。我想为这些线性布局设置点击监听器。
我使用 PagerAdapter 实现了 View 寻呼机。我试过了
view.findViewById(R.id.add_new_tut_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();
}
});
但是根本不起作用
HomePagerAdapter.java
public class HomePagerAdapter extends PagerAdapter {
Context context;
HomePagerAdapter(Context context){
this.context = context;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
final ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), container, false);
View view = inflater.inflate( modelObject.getLayoutResId(), container, false);
if ( position == 0) {
view.findViewById(R.id.add_new_tut_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();
}
});
}
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
public enum ModelObject{
HOME_PAGE("Home", R.layout.home_btns_layout),
FILES_PAGE("Files", R.layout.files_download_layout);
private int mLayoutResId;
private String mTitleResId;
ModelObject(String titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public String getTitleResId() {
return mTitleResId;
}
public int getLayoutResId()
{
return mLayoutResId;
}
}
}
home_btns_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/files_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:id="@+id/add_new_tut_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:id="@+id/upcoming_tut_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upcoming"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
files_download_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Networks"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Theories"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/st_home_btn_bg_1"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Questions"
android:textAlignment="center"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
最佳答案
我可以建议在 ViewPager 的每个屏幕上使用 Fragments 吗?然后在你的 Fragment 中你可以做任何你想做的事情,使用你需要的所有 ClickListener。
我已经为你创建了一个要点 gist ViewPagerAdapter
关于java - 我想为 viewpager 页面中的按钮设置一个 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57215975/