I have a problem here. I can't make the LeftAndRight activity as my Main activity on Ecplise by using the ExampleListActivity in Jeremy library. https://github.com/jfeinstein10/SlidingMenu
When i run the application it gives me an error. But i wanted to start the LeftAndRightActivity.java at the first page how do i do that?
I have designed the action bar and the sliding just like him with a baseactivity, leftandrightactivity and samplelistfragment.java files. Then in my 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;
}
}
}
or did I do anything wrong? Appreciate with any help . Thanks
This is the changes i have made but it still won't launch at first page
on my 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();
}
}
analyse the sample example properly. Here is short description, simply create 2 fragments with different layouts. then use the API like below to set them as left and right menu
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
use setcontentview
to set your main view
But make sure you extend with BaseActivity
else it wont work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.