I have a problem with adapter and replacing a fragmet
My question is .. how to replace fragment and remove the item in the previous listview?
i try fragment_transaction.begintransation().add(); fragment_transaction.commit(); and the remove
and i also tried fragment_transaction.begintransaction().replace(); fragment_transaction.commit();
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View wew=getLayoutInflater ().inflate (R.layout.frag2,null);
GridView listView=wew.findViewById (R.id.listview);
listView.setAdapter (new adapter ());
listView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0){
faculty_fragment faculty_fragment=new faculty_fragment ();
FragmentTransaction fragmentTransaction=getChildFragmentManager ().beginTransaction ().replace (R.id.frag2,faculty_fragment);
fragmentTransaction.commit ();
}
}
});
return wew;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
}
public class adapter extends BaseAdapter {
@Override
public int getCount() {
return image.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1=getLayoutInflater ().inflate (R.layout.adapter,null);
mTextMessage=view1.findViewById (R.id.text1);
imageView=view1.findViewById (R.id.image1);
mTextMessage.setText (name[i]);
imageView.setImageResource (image[i]);
return view1;
}
}
Here is the result
Dont know if understood correctly but you are trying to replace a fragment A with fragment B? If yes - then look at this code:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().remove(oldfragment).commit();
fragmentManager.beginTransaction().add(R.id.yourcontainer, newfragment).addToBackStack(null).commit();
Here you basically remove the old fragment first and commit then you add your new fragment to your container.
If this is not the answer then maybe it is because your layout MUST have a background color/image or something. I've had an experience with 2 items overlapping or rather 2 fragments overlapping eachother and the result was adding a background to the fragment layout.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.