gpt4 book ai didi

android - 如何在滑动菜单中实现项目列表?

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:01 27 4
gpt4 key购买 nike

我想将项目列表设置为 SlidingMenu 有没有教程?我正在使用 https://github.com/jfeinstein10/SlidingMenu

最佳答案

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

要首先将列表项添加到滑动菜单中,您需要在主菜单上使用 FrameLayout。

MainActivity.xml(在布局文件夹中)

<?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(左侧菜单的列表)

<?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(用于保存左侧的列表)

<?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(自定义每一行的 View )

<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(写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来显示滑动/ Action 条的效果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);



}


}

然后在您的 MainActivity.java 上扩展 baseactivity(只需在您的 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/drawable 文件夹中的 shadow.xml 和 shadowright.xml 复制到您的应用程序

关于android - 如何在滑动菜单中实现项目列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com