问题
i have 4-5 fragments on MainActivity and on host fragment i have a recyclerview. on each item of recycler i have to open a fragment out of 4 more and kill the current fragment on which recycler is placed. i couldnt achieve this.
here is my MainActivity where all fragments are placed and host fragment is homefragment where recycler is placed and i want to move from homefragment to another fragment when 0th item clicked
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_house_business, R.id.nav_committees,
R.id.nav_senators, R.id.nav_about_senate, R.id.nav_publications, R.id.nav_media_center
, R.id.nav_get_involved)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
here is my host fragment
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
TextView ticker ;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
final View root = inflater.inflate(R.layout.fragment_home, container, false);
MyListData[] myListData = new MyListData[] {
new MyListData("Order of the Day", R.drawable.orders),
new MyListData("Bills", R.drawable.bills),
new MyListData("Delete", R.drawable.orders),
new MyListData("Dialer", R.drawable.bills),
new MyListData("Alert", R.drawable.orders),
new MyListData("Map", R.drawable.bills),
new MyListData("Email", R.drawable.orders),
new MyListData("Info", R.drawable.bills),
new MyListData("Delete", android.R.drawable.ic_delete),
new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
new MyListData("Alert", android.R.drawable.ic_dialog_alert),
new MyListData("Map", android.R.drawable.ic_dialog_map),
};
RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
MyListAdapter adapter = new MyListAdapter(myListData);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
//recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
return root;
}
}
here is my listadapter
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{
private MyListData[] listdata;
// RecyclerView recyclerView;
public MyListAdapter(MyListData[] listdata) {
this.listdata = listdata;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final MyListData myListData = listdata[position];
holder.textView.setText(listdata[position].getDescription());
holder.imageView.setImageResource(listdata[position].getImgId());
switch(position)
{
case 0 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment someFragment = new Orderofday();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
}
});
}
case 1 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
});
}
case 2 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
});
}
case 3 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
});
}
case 4 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
});
}
case 5 :
{
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+myListData.getDescription(),Toast.LENGTH_LONG).show();
}
});
}
default:
{
break;
}
}
}
@Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public RelativeLayout relativeLayout;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (ImageView) itemView.findViewById(R.id.imageView);
this.textView = (TextView) itemView.findViewById(R.id.textView);
relativeLayout = (RelativeLayout)itemView.findViewById(R.id.relativeLayout);
}
}
when case 0 is clicked i need to kill the current homefragment and move to another fragment but couldnt do it kindly somebody help me to achieve this
回答1:
In this solution i'm not using Navigation Library
replace fragments on MainActivity
. you should create interface in your adapter class and home fragment:
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder>{
private OnItemClickListener onItemClickListener;
//...
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(position, onItemClickListener);
}
//...
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView;
public TextView textView;
public RelativeLayout relativeLayout;
private OnItemClickListener listener;
public ViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
}
public void bind(int position, OnItemClickListener listener) {
this.listener = listener;
final MyListData myListData = listdata[position];
textView.setText(listdata[position].getDescription());
imageView.setImageResource(listdata[position].getImgId());
relativeLayout.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (listener != null) {
listener.onClick(getAdapterPosition());
}
}
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.onItemClickListener = listener;
}
public interface OnItemClickListener{
void onClick(int position);
}
}
in HomeFragment
:
public class HomeFragment extends Fragment implements
MyListAdapter.OnItemClickListener {
private OnItemClickListener onItemClickListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
//...
adapter.setOnItemClickListener(this);
//...
}
@Override
public void onClick(int position) {
if (onItemClickListener != null)
onItemClickListener.onClick(position);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onClick(int position);
}
}
in MainActivity
:
public class MainActivity extends AppCompatActivity implements
HomeFragment.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
HomeFragment homeFragment = new HomeFragment();
homeFragment.setOnItemClickListener(this);
getSupportFragmentManager().beginTransaction()
.replace(R.id.your_frame_layout, homeFragment).commit();
}
//...
@Override
public void onClick(int position) {
switch (position){
case 0:
//replace fragment
break;
//...
}
}
}
来源:https://stackoverflow.com/questions/58521136/recyclerview-item-onclick-to-open-a-fragment-from-main-activity-fragment