单击 recyclerView 项目时未打开新片段
new Fragment is not opening when recyclerView item is clicked
我一直在尝试在点击时启动一个新的片段,我在互联网上发现是这样的,当点击方法被激怒但没有任何反应
public class PlacesRecyclerAdapter extends
RecyclerView.Adapter<PlacesRecyclerAdapter.ViewHolder> implements View.OnClickListener{
//some code
@Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) context;
TextView t = v.findViewById(R.id.placeName);
fragment = new DetailFragment(getCurrentPlace((String) t.getText()));
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.tabItem, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
}
//some code
}
对于 getCurrentPLace(),它是一个 return 放置对象的方法,我已经用调试器检查过它,它按预期工作
这里是DetailFragment.class代码
public class DetailFragment extends Fragment {
private MainActivity.Place place;
TextView name;
public DetailFragment(MainActivity.Place place) {
this.place = place;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_place_fragment, container, false);
name.setText(place.getTitle());
return view;
}
}
这是 ID 为 R.id.tabItem
的 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/placesRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
PS: 我尝试了类似问题的解决方案,但没有任何效果。
一开始最好的方法是使用接口。
Interface onClick{
void click();
}
并在 class 的构造函数中实现它:
class PlacesRecyclerAdapter(onClick click)...
并像
一样使用它
t.setOnClickListener{
click.click();
}
并且当您在 activity 中定义适配器时,此方法(单击)实现并且您可以在 activity 而不是适配器
中定义它
fragment = new DetailFragment(getCurrentPlace((String) t.getText()));
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.tabItem, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
但是避免这些问题的最好方法是使用导航组件。
我一直在尝试在点击时启动一个新的片段,我在互联网上发现是这样的,当点击方法被激怒但没有任何反应
public class PlacesRecyclerAdapter extends
RecyclerView.Adapter<PlacesRecyclerAdapter.ViewHolder> implements View.OnClickListener{
//some code
@Override
public void onClick(View v) {
AppCompatActivity activity = (AppCompatActivity) context;
TextView t = v.findViewById(R.id.placeName);
fragment = new DetailFragment(getCurrentPlace((String) t.getText()));
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.tabItem, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
}
//some code
}
对于 getCurrentPLace(),它是一个 return 放置对象的方法,我已经用调试器检查过它,它按预期工作
这里是DetailFragment.class代码
public class DetailFragment extends Fragment {
private MainActivity.Place place;
TextView name;
public DetailFragment(MainActivity.Place place) {
this.place = place;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_place_fragment, container, false);
name.setText(place.getTitle());
return view;
}
}
这是 ID 为 R.id.tabItem
的 XML<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tabItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/placesRecycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
PS: 我尝试了类似问题的解决方案,但没有任何效果。
一开始最好的方法是使用接口。
Interface onClick{
void click();
}
并在 class 的构造函数中实现它:
class PlacesRecyclerAdapter(onClick click)...
并像
一样使用它t.setOnClickListener{
click.click();
}
并且当您在 activity 中定义适配器时,此方法(单击)实现并且您可以在 activity 而不是适配器
中定义它fragment = new DetailFragment(getCurrentPlace((String) t.getText()));
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.tabItem, fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(null);
transaction.commit();
但是避免这些问题的最好方法是使用导航组件。