我有一个Activity Main
班级和XML
,以及ana_sayfa
(Fragment
)班级和XML
。
当我滑动屏幕并显示ana_sayfa
时,我想将项目添加到可展开列表视图中,但我无法访问ana_sayfa
Fragment
到主要活动类
Main_activity类:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button b1;
private ResideMenu resideMenu;
private Context mContext;
private ResideMenuItem itemAnasayfa;
private ResideMenuItem itemRastgele;
private ResideMenuItem itemEncok;
private ResideMenuItem itemTarifyaz;
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;
private View view2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
setUpMenu();
if (savedInstanceState == null) {
changeFragment(new F_ana_sayfa());
}
/** listView=(ExpandableListView)findViewById(R.id.exp2);
initData();
listAdapter=new com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);
*/
}
private void initData() {
listDataHeader=new ArrayList<>();
listHash=new HashMap<>();
listDataHeader.add("et");
listDataHeader.add("tavuk");
listDataHeader.add("sebze");
listDataHeader.add("diger");
List<String> l_et=new ArrayList<>();
l_et.add("kemikli");
l_et.add("kuşbaşı");
List<String> l_tavuk=new ArrayList<>();
l_tavuk.add("bonfile");
l_tavuk.add("but");
l_tavuk.add("bütün tavuk");
List<String> l_sebze=new ArrayList<>();
l_sebze.add("patates");
l_sebze.add("biber");
l_sebze.add("kabak");
l_sebze.add("patlıcan");
List<String> l_diger=new ArrayList<>();
l_diger.add("tuz");
l_diger.add("yağ");
l_diger.add("kekik");
listHash.put(listDataHeader.get(0),l_et);
listHash.put(listDataHeader.get(1),l_tavuk);
listHash.put(listDataHeader.get(2),l_sebze);
listHash.put(listDataHeader.get(3),l_diger);
}
private void setUpMenu() {
resideMenu = new ResideMenu(this);
resideMenu.setBackground(R.drawable.menu_background);
resideMenu.attachToActivity(this);
resideMenu.setMenuListener(menuListener);
resideMenu.setScaleValue(0.6f);
//create menu items;
itemAnasayfa = new ResideMenuItem(this, R.drawable.icon_home, "Ana Sayfa");
itemRastgele = new ResideMenuItem(this, R.drawable.icon_profile, "Random tarif bul");
itemEncok = new ResideMenuItem(this, R.drawable.icon_calendar, "En cok begenilenler");
itemTarifyaz = new ResideMenuItem(this, R.drawable.icon_settings, "tarif Yaz");
itemAnasayfa.setOnClickListener(this);
itemRastgele.setOnClickListener(this);
itemEncok.setOnClickListener(this);
itemTarifyaz.setOnClickListener(this);
resideMenu.addMenuItem(itemAnasayfa, ResideMenu.DIRECTION_LEFT);
resideMenu.addMenuItem(itemRastgele, ResideMenu.DIRECTION_LEFT);
resideMenu.addMenuItem(itemEncok, ResideMenu.DIRECTION_RIGHT);
resideMenu.addMenuItem(itemTarifyaz, ResideMenu.DIRECTION_RIGHT);
findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
}
});
findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return resideMenu.dispatchTouchEvent(ev);
}
@Override
public void onClick(View view) {
if (view == itemAnasayfa) {
changeFragment(new F_ana_sayfa());
listView=(ExpandableListView)findViewById(R.id.exp2);
initData();
listAdapter=new com.yeni.ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);
} else if (view == itemRastgele) {
changeFragment(new F_rastgele_tarif());
} else if (view == itemEncok) {
changeFragment(new F_encok_beg());
} else if (view == itemTarifyaz) {
changeFragment(new F_tarif_yaz());
}
resideMenu.closeMenu();
}
private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
@Override
public void openMenu() {
Toast.makeText(mContext, "Menu is aÇILDIIII!", Toast.LENGTH_SHORT).show();
}
@Override
public void closeMenu() {
Toast.makeText(mContext, "Menu is KAPANDI!", Toast.LENGTH_SHORT).show();
}
};
private void changeFragment(Fragment targetFragment) {
resideMenu.clearIgnoredViewList();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main_fragment, targetFragment, "fragment")
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
public ResideMenu getResideMenu(){
return resideMenu;
}
}
ana_sayfa类:
public class F_ana_sayfa extends Fragment {
private View parentView;
private ResideMenu resideMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.ana_sayfa, container, false);
//setUpViews();
return parentView;
}
答案 0 :(得分:0)
使用setArguments(Bundle)
将数据传递到片段,并使用onCreateView()
在getArguments()
中检索数据。然后Fragment可以处理在列表视图中呈现数据的所有逻辑。
答案 1 :(得分:0)
您可以使用interface
来进行活动和片段之间的通信。例如,在您的片段类中,您可以添加:
public class F_ana_sayfa extends Fragment {
...
SomeInterface myInterface;
// Container activity must implement this interface
public interface SomeInterface {
// You can define your interface functions here
void doSomething();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
myInterface = (SomeInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement SomeInterface");
}
}
public void someFunction(boolean someBoolean) {
if (someBoolean) {
// Calling the interface function
myInterface.doSomething();
}
}
}
现在您的活动必须实现片段类中定义的接口。 例如:
public class MainActivity extends AppCompatActivity implements F_ana_sayfa.SomeInterface {
...
public void doSomething() {
// Your code here
}
}
有关详细信息,请访问https://developer.android.com/training/basics/fragments/communicating.html