我已经阅读了多篇关于它的帖子;但就我的具体情况而言,我似乎无法使其发挥作用。单击适配器中的视图时,片段似乎不会被替换。
相关帖子:
start Fragment from RecycleView Adapter Onclick
how to open a different fragment on recyclerview OnClick
这是我的RecyclerView
public class LocationFilterAdapter extends RecyclerView.Adapter<LocationFilterAdapter.LocationFilterViewHolder> {
public Context mActivity;
@Override
public LocationFilterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.profile_tab_become_a_host_basic_questions_location_filter_item, parent, false);
mActivity = parent.getContext();
return new LocationFilterAdapter.LocationFilterViewHolder(view);
}
@Override
public void onBindViewHolder(LocationFilterViewHolder holder, int position) {
holder.bindView(position);
}
@Override
public int getItemCount() {
return LocationFilterFragment.RESULT_SIZE;
}
public class LocationFilterViewHolder extends RecyclerView.ViewHolder{
TextView tvLocations;
public LocationFilterViewHolder(View itemView) {
super(itemView);
tvLocations = (TextView) itemView.findViewById(R.id.tvFilterLocations);
}
public void bindView(int position){
final String output = LocationFilterFragment.STREET_NAME + ", " + LocationFilterFragment.CITY_NAME
+", " + LocationFilterFragment.STATE_NAME + ", " + LocationFilterFragment.COUNTRY_NAME;
tvLocations.setText(output);
tvLocations.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText etStreet = (EditText) LocationFilterFragment.mView.findViewById(R.id.etStreetFilter);
etStreet.setText(output);
LocationFragment locationFragment = new LocationFragment();
locationFragment.setArguments(bundle);
//Fragment will not start
((AppCompatActivity) mActivity).getSupportFragmentManager().beginTransaction().replace(R.id.progressFragment, new LocationFragment()).commit();
}
});
}
这是我的LocationFragment(SupportFragment)
public class LocationFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile_tab_become_a_host_basic_questions_location_fragment, container, false);
Log.d("fragment lifecycle", "OnCreateView");
if(getArguments() != null){
TextView tvStreet = (TextView) view.findViewById(R.id.tvStreet);
tvStreet.setText(getArguments().getString("STREET_NAME"));
TextView tvState = (TextView) view.findViewById(R.id.tvState);
tvState.setText(getArguments().getString("STATE_NAME"));
TextView tvCity = (TextView) view.findViewById(R.id.tvCity);
tvCity.setText(getArguments().getString("CITY_NAME"));
}
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.layoutStreet).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new LocationFilterFragment()).addToBackStack("locationFragment").commit();
}
});
view.findViewById(R.id.bContinue).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new MapFragment()).addToBackStack(null).commit();
}
});
view.findViewById(R.id.layoutTapInfo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.progressFragment, new LocationInfoFragment()).addToBackStack("locationFragment").commit();
}
});
}
}
答案 0 :(得分:0)
这很令人尴尬,但我这样做是对的。由于某些原因。我的Android Studio表现得很奇怪;当我重建我的项目时,代码完全奏效了:)