我在活动中有两个片段。基于categoryFragment.java中选择的类别,子类别将显示在另一个片段中(SubcategoryFragment.java)
截至目前,当在categoryfragment中单击textview时,我正在显示子类别。而不是被替换,文本视图附加在点击上。
我甚至尝试过复制片段。但是无法两次提交片段事务。请告诉我哪里出错了。
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
itemsList.add(new Items(1,"Drinks","Coca Cola",10,1,"image.jpg"));
itemsList.add(new Items(2,"Drinks","Pepsi",30,1,"image.jpg"));
itemsList.add(new Items(3,"Food","Briyani",30,1,"image.jpg"));
itemsList.add(new Items(4,"Food","meals",30,1,"image.jpg"));
bundle.putParcelableArrayList("itemsList", itemsList);
bundle.putString("category","none");
frg.setArguments(bundle);
frg1.setArguments(bundle);
transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
transaction.commit();
}
@Override
public void onFragmentInteraction(Integer id) {
String category = "";
itemsList.add(new Items(1,"Drinks","Coca Cola",10,1,"image.jpg"));
itemsList.add(new Items(2,"Drinks","Pepsi",30,1,"image.jpg"));
itemsList.add(new Items(3,"Food","Briyani",30,1,"image.jpg"));
itemsList.add(new Items(4,"Food","meals",30,1,"image.jpg"));
for(int i=0; i<itemsList.size();i++)
{
if( i == itemsList.get(id).getItemId() - 1)
category = itemsList.get(i).getCategory();
}
((SubCategoryFragment)getSupportFragmentManager().findFragmentById(R.id.My_Container_2_ID)).getSelectedCategory(category);
}
CategoryFragmnet.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.fragment_category, container, false);
Bundle bundle = this.getArguments();
ArrayList<Items> myValue;
myValue = bundle.getParcelableArrayList("itemsList");
TextView textView = (TextView) view.findViewById(hello);
textView.setOnClickListener(this);
String cat="";
for(int i=0; i<myValue.size();i++)
{
cat = cat + myValue.get(i).getCategory() +" ";
}
textView.setText(cat);
return view;
}
OnFragmentInteractionListener dataPasser;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
dataPasser = (OnFragmentInteractionListener) context;
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.hello:
mListener.onFragmentInteraction(1);
break;
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Integer id);
}
SubCategoryFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
view = inflater.inflate(R.layout.fragment_sub_category, container, false);
return view;
}
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
Bundle bundle = this.getArguments();
myValue = bundle.getParcelableArrayList("itemsList");
String category = bundle.getString("category");
textView = (TextView) v.findViewById(R.id.subh);
}
@Override
public void onResume(){
super.onResume();
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.OnSubFragmentInteractionListener(uri);
}
}
OnSubFragmentInteractionListener dataPasser;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnSubFragmentInteractionListener) {
mListener = (OnSubFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
dataPasser = (OnSubFragmentInteractionListener) context;
}
public interface OnSubFragmentInteractionListener {
void OnSubFragmentInteractionListener(Uri uri);
}
public void getSelectedCategory(String str)
{
textView = (TextView) getView().findViewById(R.id.subh);
textView.setText(" ");
String cat="";
for(int i=0; i<myValue.size();i++)
{
if(str.equals(myValue.get(i).getCategory()))
cat = cat + myValue.get(i).getSubCategory() +" ";
}
textView.setText(cat);
}
答案 0 :(得分:0)
Mayb您可以尝试使用接口在片段之间传递数据 在第一个片段中创建一个接口MainActivity实现该接口和相应的方法,然后调用第二个fragent中定义的方法 第二个片段通过该方法接收数据。 由于2个片段不能直接进行,所以必须使用这种简单的方法 希望这有帮助。