我在适配器和更换烟盒方面有问题
我的问题是..如何替换片段并删除上一个列表视图中的项目?
我尝试fragment_transaction.begintransation()。add(); fragment_transaction.commit();和删除
,我也尝试了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;
}
}
这是结果
答案 0 :(得分:0)
不知道是否理解正确,但是您正在尝试将片段A替换为片段B?如果是,那么请看下面的代码:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().remove(oldfragment).commit();
fragmentManager.beginTransaction().add(R.id.yourcontainer, newfragment).addToBackStack(null).commit();
在这里,您基本上先删除了旧片段,然后提交,然后将新片段添加到容器中。
如果这不是答案,则可能是因为您的布局必须具有背景颜色/图像或其他东西。我有2个项目重叠或2个片段相互重叠的经验,结果是在片段布局中添加了背景。