android模仿美团顶部的滑动菜单实例代码
时间: 2023-10-18 21:04:14 浏览: 106
Android模仿美团顶部的滑动菜单实例代码
以下是一个简单的实现顶部滑动菜单的示例代码:
1. 创建一个名为 `TopMenuActivity` 的 Activity,并在布局文件中添加一个 `ViewPager` 和一个 `TabLayout`:
```xml
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
2. 创建一个名为 `TopMenuFragmentPagerAdapter` 的 FragmentPagerAdapter,用于管理顶部菜单的 Fragment:
```java
public class TopMenuFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
private List<String> mTitles;
public TopMenuFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
super(fm);
mFragments = fragments;
mTitles = titles;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
```
3. 创建一个名为 `TopMenuFragment` 的 Fragment,实现顶部菜单的布局和功能:
```java
public class TopMenuFragment extends Fragment {
private RecyclerView mRecyclerView;
private TopMenuAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_top_menu, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new TopMenuAdapter();
mRecyclerView.setAdapter(mAdapter);
return view;
}
private class TopMenuAdapter extends RecyclerView.Adapter<TopMenuViewHolder> {
private List<String> mData = Arrays.asList("美食", "电影", "酒店", "KTV", "景点", "更多");
@Override
public TopMenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top_menu, parent, false);
return new TopMenuViewHolder(view);
}
@Override
public void onBindViewHolder(TopMenuViewHolder holder, int position) {
holder.mTextView.setText(mData.get(position));
}
@Override
public int getItemCount() {
return mData.size();
}
}
private class TopMenuViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public TopMenuViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.text_view);
}
}
}
```
4. 在 `TopMenuActivity` 中初始化 `ViewPager` 和 `TabLayout`,并设置 `TopMenuFragmentPagerAdapter` 为 `ViewPager` 的适配器:
```java
public class TopMenuActivity extends AppCompatActivity {
private ViewPager mViewPager;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_menu);
mViewPager = findViewById(R.id.view_pager);
mTabLayout = findViewById(R.id.tab_layout);
List<Fragment> fragments = new ArrayList<>();
List<String> titles = new ArrayList<>();
for (int i = 0; i < 5; i++) {
fragments.add(new TopMenuFragment());
titles.add("Tab " + (i + 1));
}
mViewPager.setAdapter(new TopMenuFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles));
mTabLayout.setupWithViewPager(mViewPager);
}
}
```
这样就可以实现一个简单的顶部滑动菜单了。当用户滑动 `ViewPager` 时,`TabLayout` 中的选项卡会自动切换,而 `TopMenuFragment` 中的 RecyclerView 可以显示菜单项。你可以根据需要自定义菜单项的样式和功能。
阅读全文