如何实现对Slidingmenu列表itens? [英] how to implement a list itens on Slidingmenu?

查看:132
本文介绍了如何实现对Slidingmenu列表itens?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置列表itens到SlidingMenu有任何教程?
我使用 https://github.com/jfeinstein10/SlidingMenu

I'd like to set a list itens to a SlidingMenu is there any tutorial? I'm using https://github.com/jfeinstein10/SlidingMenu

推荐答案

您可以在库中的ExampleListActivity找到它。

You could find it in the ExampleListActivity in the library.

要拥有一个列表项的滑动菜单首先,您需要在您的主要一个的FrameLayout。

To have a List item to a sliding menu firstly you will need a FrameLayout on your main.

MainActivity.xml(布局文件夹)

MainActivity.xml (in layout folder)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

之后,创建一个leftlist.xml(左侧菜单中的列表)

After that create a leftlist.xml ( The list on the left menu)

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/list_padding"
android:paddingRight="@dimen/list_padding" />

在这之后创建一个menu_frame.xml(保持在左侧的列表)

After that create a menu_frame.xml (to hold the list on the left)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

和一个row.xml(自定义每一行的视图)

And a row.xml (CUstomize the view for each row)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >

<TextView
    android:id="@+id/row_title1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:padding="10dp"

</LinearLayout>

然后有一个SampleListFragment.java(写列表的名称)

Then have a SampleListFragment.java (to write the name of the list)

   public class SampleListFragment extends ListFragment {

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

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
    SampleAdapter adapter = new SampleAdapter(getActivity());
    adapter.add(new SampleItem("menu left A"));
    adapter.add(new SampleItem("menu left B"));
    adapter.add(new SampleItem("menu left C"));
    adapter.add(new SampleItem("menu left D"));
    adapter.add(new SampleItem("menu left A"));
    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;
    }

}
   }

您将需要一个baseactivity显示滑动/操作栏的效果
BaseActivity.java(如何配置你的滑动菜单效果)

You will need a baseactivity to show the effects of the sliding / action bar BaseActivity.java (Configure how your sliding menu effects)

   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);



}


   }

然后在你的MainActivity.java延伸baseactivity(只需拨打你的OnCreate左侧菜单)

Then on your MainActivity.java extends baseactivity(Just call the left menu in your oncreate)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.menu_frame, new SampleListFragment())
    .commit();
    }

这是我怎么做我的应用程序,我不知道这是否是正确的或错误的方法,但随时问,如果您有任何问题。当然,你将不得不在RES /可绘制文件夹中的shadow.xml和shadowright.xml复制到应用程序

This is how I do it for my application I am not sure if it's the right or wrong method but feel free to ask if you have any questions. And of course you will have to copy the shadow.xml and shadowright.xml at res/drawable folder to your application

这篇关于如何实现对Slidingmenu列表itens?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆