如何在Jeremy滑动菜单中创建LeftAndRight活动示例成为我的主页面?

时间:2013-10-24 05:29:31

标签: java android eclipse

我这里有问题。我不能通过使用Jeremy库中的ExampleListActivity将LeftAndRight活动作为我在Ecplise上的主要活动。 https://github.com/jfeinstein10/SlidingMenu

当我运行应用程序时,它给了我一个错误。但是我想在第一页启动LeftAndRightActivity.java我该怎么做?

我设计了动作栏和像他一样的滑动,带有基本活动,leftandrightactivity和samplelistfragment.java文件。然后在我的main.java

     public class MainActivity extends SherlockPreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.app_name);

    Class<?> cls = null;
    cls = LeftAndRightActivity.class;
Intent intent = new Intent(this, cls);
startActivity(intent);

}

BaseActivity.java

    public class BaseActivity extends SlidingFragmentActivity {
SlidingMenu menu;
private int mTitleRes;
protected ListFragment mFrag;

public BaseActivity(int titleRes) {
    mTitleRes = titleRes;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle(mTitleRes);

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);
    if (savedInstanceState == null) {
        FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
        mFrag = new SampleListFragment();
        t.replace(R.id.menu_frame, mFrag);
        t.commit();
    } else {
        mFrag = (ListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
    }

    // customize the SlidingMenu
    SlidingMenu sm = getSlidingMenu();
    sm.setShadowWidthRes(R.dimen.shadow_width);
    sm.setShadowDrawable(R.drawable.shadow);
    sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    sm.setFadeDegree(0.35f);
    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setIcon(android.R.color.transparent);
    getSupportActionBar().setTitle("");
       BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.background);
       getSupportActionBar().setBackgroundDrawable(bg);


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        toggle();
        return true;
    case R.id.github:
        showSecondaryMenu();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}
  }

LeftAndRightAcitivity.java

  public class LeftAndRightActivity extends BaseActivity {

public LeftAndRightActivity() {
    super(R.string.left_and_right);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    setContentView(R.layout.content_frame);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, new SampleListFragment())
    .commit();

    getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
    getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame_two, new SampleListFragment())
    .commit();
}

  }

SampleListFragment.java

public class SampleListFragment extends ListFragment {

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

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity());
    for (int i = 0; i < 20; i++) {
        adapter.add(new SampleItem("Sample List", android.R.drawable.ic_menu_search));
    }
    setListAdapter(adapter);
}

private class SampleItem {
    public String tag;
    public int iconRes;
    public SampleItem(String tag, int iconRes) {
        this.tag = tag; 
        this.iconRes = iconRes;
    }
}

public class SampleAdapter extends ArrayAdapter<SampleItem> {

    public SampleAdapter(Context context) {
        super(context, 0);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
        }
        ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
        icon.setImageResource(getItem(position).iconRes);
        TextView title = (TextView) convertView.findViewById(R.id.row_title);
        title.setText(getItem(position).tag);

        return convertView;
    }

}
 }

还是我做错了什么?感谢任何帮助。感谢

这是我所做的更改,但仍然无法在首页发布

在我的mainactivity.java

public class MainActivity extends BaseActivity {

public MainActivity(int titleRes) {
    super(titleRes);
    // TODO Auto-generated constructor stub
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    setContentView(R.layout.activity_main);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame, new LeftListFragment())
    .commit();

    getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
    getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame_two, new RightListFragment())
    .commit();
}

}

1 个答案:

答案 0 :(得分:0)

正确分析示例示例。这是简短描述,只需创建具有不同布局的2个片段。然后使用下面的API将它们设置为左右菜单

    getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
    getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

    setBehindContentView(R.layout.menu_frame);

    FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
                mFrag = new SampleListFragment();
                t.replace(R.id.menu_frame, mFrag);
                t.commit(); //left menu

getSlidingMenu().setSecondaryMenu(R.layout.right_menu_layout);
        getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
        getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.right_list, new RightListFragment())
        .commit(); //right menu

使用setcontentview设置主视图

但请确保您使用BaseActivity延伸,否则它无效。