美好的一天,我正在尝试在我的应用程序中使用 ListView 布局和 ActionBarSherlock 实现 jfeinstein 幻灯片菜单,但由于某种原因,我似乎无法在 SlideMenu 上正确实现。据我了解,在使用带有 ActionBarSherlock 的 SlideMenu 库时,您必须在您计划使用的 SlideMenu Activity 中扩展到 SherlockActivity。
这是我的实现:我将只在此处发布相关代码。
public class MyActivity extends SlidingFragmentActivity {
ListView slidinglist;
private SlidingAdapter SlidingAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_dashboard);
setBehindContentView(R.layout.menu_layout);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Enable home button
getSupportActionBar().setHomeButtonEnabled(true);
//Home as up display
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
/*get array from the slidemenu items in value folder */
String[] text_items = getResources().getStringArray(R.array.sliding_menus);
TypedArray imgs = getResources().obtainTypedArray(R.array.sliding_menu_images);
final Context context=this;
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.menu_layout, null, true);
slidinglist = (ListView)v.findViewById(R.id.slidemenu_listView);
SlidingAdapter = new SlidingAdapter(this, R.layout.slide_menu_item, text_items, imgs);
slidinglist.setAdapter(SlidingAdapter);
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setFadeEnabled(true);
menu.setFadeDegree(0.35f);
menu.setBehindOffset(100);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//menu.setMenu(R.layout.menu_layout);
menu.setMenu(v);
}
menu_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/slidemenu_listView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</ListView>
</LinearLayout>
适配器类:
public class SlidingAdapter extends ArrayAdapter<String> {
Context mContext;
private int ViewResourceId;
private String[] mstring;
TypedArray icons;
public SlidingAdapter(Context context, int resourceId, String[] items, TypedArray images ){
super(context, resourceId, items);
mContext = context;
ViewResourceId = resourceId;
mstring = items;
icons = images;
}
@Override
public int getCount(){
Log.d(TAG, "lenght of string is" + mstring.length);
return mstring.length;
}
@Override
public String getItem(int position){
return mstring[position];
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView (int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(ViewResourceId, null);
holder = new ViewHolder();
holder.imageview = (ImageView)convertView.findViewById(R.id.slidemenu_image_item);
holder.text_title = (TextView)convertView.findViewById(R.id.slidemenu_textview_id);
holder.arrowImageView = (ImageView)convertView.findViewById(R.id.slidemenu_arrow_id);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
//set Array text values defined in xml to textview
String title = mstring[position];
holder.text_title.setText(title);
//set Array icons values defined in xml to textview
holder.imageview.setImageDrawable(icons.getDrawable(position));
return convertView;
}
}
class ViewHolder {
TextView text_title;
ImageView imageview;
ImageView arrowImageView;
}
我在幻灯片菜单中看到一个空白屏幕。我可能做错了什么?