gpt4 book ai didi

android - 带有滑动菜单和操作栏的动态 UI

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:32 25 4
gpt4 key购买 nike

尝试使用 facebook like sliding menu 实现动态 UI和 actionbarsherlock.首先,我查看了引入 fragment 来处理动态按钮的android文档。但是没有运气和一个星期的时间,我仍然无法让它工作,我想是我对 android 概念的误解。slidingbar 和 actionbarsherlock 工作没有任何问题。

我有一个 HomeScreen.java,其中包含我所有的菜单和预设阶段到目前为止,我已经创建了一个扩展 FragmentPagerAdapter 的 pagerAdapter1.java,以及处理我的工作的三个示例 fragment 类,它们是 task1.java、task2.java,task3.java 足够简单

这是我的部分代码主屏幕.java

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;
public class HomeScreen extends SlidingFragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
setBehindContentView(R.layout.menu_frame);
}

PagerAdapter1.java

public class PagerAdapter1 extends FragmentPagerAdapter  {

private List<Fragment> fragments;
public PagerAdapter1(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}

public Fragment getItem(int position) {
return this.fragments.get(position);
}

public int getCount() {
return this.fragments.size();
}

}

和三个task1.java,2,3

    import android.support.v4.app.Fragment;
public class Tab1Fragment extends Fragment{

onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
}

我觉得最好用图片来解释我的问题

主屏幕是一个预设阶段,每当用户点击菜单时,这个页面就会切换到他想要的页面

homescreen

这是我的菜单

menu_frame

我的问题是如何将这 3 个 fragment 添加到我的主屏幕中?我已经尝试了很多教程,但它在我的情况下不起作用。大多数教程都是用代码创建 fragment ,我只想将我的 3 任务包含在其中

最佳答案

我会尝试解释这个示例代码,您可以根据需要使用。

这是您的 BehindContent (SlidingMenu) 的 ListFragment:

public class ColorMenuFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] colors = getResources().getStringArray(R.array.color_names);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, colors);
setListAdapter(colorAdapter);
//This array is only to fill SlidingMenu with a Simple String Color.
//I used MergeAdapter from Commonsware to create a very nice SlidingMenu.
}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
//This switch case is a listener to select wish item user have been selected, so it Call
//ColorFragment, you can change to Task1Fragment, Task2Fragment, Task3Fragment.
Fragment newContent = null;
switch (position) {
case 0:
newContent = new ColorFragment(R.color.red);
break;
case 1:
newContent = new ColorFragment(R.color.green);
break;
case 2:
newContent = new ColorFragment(R.color.blue);
break;
case 3:
newContent = new ColorFragment(android.R.color.white);
break;
case 4:
newContent = new ColorFragment(android.R.color.black);
break;
}
if (newContent != null)
switchFragment(newContent);
}

// the meat of switching the above fragment
private void switchFragment(Fragment fragment) {
if (getActivity() == null)
return;

if (getActivity() instanceof FragmentChangeActivity) {
FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
fca.switchContent(fragment);
} else if (getActivity() instanceof ResponsiveUIActivity) {
ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
ra.switchContent(fragment);
}
}


}

这是您的 BaseActivity 类:

它没有滑动,据我所知,你不需要这个。

public class FragmentChangeActivity extends BaseActivity {

private Fragment mContent;

public FragmentChangeActivity() {
super(R.string.changing_fragments);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Above View
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (mContent == null)
mContent = new ColorFragment(R.color.red);

// set the Above View
//This will be the first AboveView
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, mContent)
.commit();

// set the Behind View
//This is the SlidingMenu
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new ColorMenuFragment())
.commit();

// customize the SlidingMenu
//This is opcional
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}

public void switchContent(Fragment fragment) {
// the meat of switching fragment
mContent = fragment;
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
getSlidingMenu().showContent();
}

}

好的,如果您想将 ColorFragment 更改为其他任何内容,请执行以下操作:

首先,选择您要使用的项目:

case 0:
newContent = new ColorFragment(R.color.red);
break;

到:

case 0:
newContent = new ArrayListFragment();
break;

我只做了一个arraylist,这只是一个简单的例子,你可以做很多事情,然后你可以阅读Fragment学习如何做不同的事情。

    public class ArrayListFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Listnames.TITLES));
//Listnames is a class with String[] TITLES;

}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList2", "Item clicked: " + id);

String item = (String) getListAdapter().getItem(position);
Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show();

}

}

好吧,如果你误解了什么,请告诉我。

关于android - 带有滑动菜单和操作栏的动态 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274676/

25 4 0