gpt4 book ai didi

android - 在 RecyclerView Adapter 上启动 fragment

转载 作者:行者123 更新时间:2023-11-29 02:39:41 26 4
gpt4 key购买 nike

我已经阅读了多篇关于它的文章;但对于我的具体情况,我似乎无法让它发挥作用。单击适配器中的 View 时, fragment 似乎没有被替换。

相关帖子:

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();
}
});
}


}

最佳答案

这很尴尬,但我的做法是正确的。因为某些原因。我的 Android Studio 表现得很奇怪;当我重建我的项目时,代码完全有效:)

关于android - 在 RecyclerView Adapter 上启动 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45069478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com