Android 使用RecyclerView 实现 tag 展示

效果图如下:

正常使用 RecyclerView  , 在setManger的 时候,注意:

1. 使用 

implementation 'com.google.android:flexbox:2.0.1'

gitHub 地址:https://github.com/google/flexbox-layout

2. activity 中使用:

FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(getContext());
flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);//主轴为水平方向,起点在左端。
flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
//justifyContent 属性定义了项目在主轴上的对齐方式。
flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);//交叉轴的起点对齐。
tag_view.setLayoutManager(flexboxLayoutManager);

其他的就是正常的 RecyclerView 的 操作。 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现该功能的示例代码: ```java import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.github.promeg.pinyinhelper.Pinyin; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private RecyclerView mRecyclerView; private AppListAdapter mAdapter; private TextView mTvIndexHint; private List<AppInfo> mAppList = new ArrayList<>(); private HashMap<String, Integer> mIndexMap = new HashMap<>(); private Handler mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: mAdapter.notifyDataSetChanged(); break; case 1: mTvIndexHint.setVisibility(View.INVISIBLE); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.recycler_view); mTvIndexHint = findViewById(R.id.tv_index_hint); mAdapter = new AppListAdapter(); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mAdapter); new Thread(new Runnable() { @Override public void run() { loadAppList(); mHandler.sendEmptyMessage(0); } }).start(); mRecyclerView.addItemDecoration(new IndexBarDecoration(new IndexBarDecoration.IndexBarDataProvider() { @Override public List<String> getSectionList() { List<String> indexList = new ArrayList<>(mIndexMap.keySet()); Collections.sort(indexList); return indexList; } @Override public int getPositionForSection(String section) { if (mIndexMap.containsKey(section)) { return mIndexMap.get(section); } return -1; } }, new IndexBarDecoration.IndexBarHintProvider() { @Override public void onIndexChanged(String index) { mTvIndexHint.setVisibility(View.VISIBLE); mTvIndexHint.setText(index); mHandler.removeMessages(1); mHandler.sendEmptyMessageDelayed(1, 1000); } })); } private void loadAppList() { mAppList.clear(); mIndexMap.clear(); PackageManager pm = getPackageManager(); List<ApplicationInfo> appInfoList = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo appInfo : appInfoList) { String packageName = appInfo.packageName; if (TextUtils.isEmpty(packageName)) { continue; } String appName = pm.getApplicationLabel(appInfo).toString(); if (TextUtils.isEmpty(appName)) { continue; } String pinyin = Pinyin.toPinyin(appName, ""); AppInfo info = new AppInfo(appName, packageName, pinyin); mAppList.add(info); } Collections.sort(mAppList, new Comparator<AppInfo>() { @Override public int compare(AppInfo o1, AppInfo o2) { return o1.pinyin.compareToIgnoreCase(o2.pinyin); } }); char lastChar = 0; for (int i = 0; i < mAppList.size(); i++) { char firstChar = mAppList.get(i).pinyin.charAt(0); if (Character.isLetter(firstChar)) { if (lastChar != firstChar) { String index = String.valueOf(firstChar).toUpperCase(); mIndexMap.put(index, i); lastChar = firstChar; } } } } private static class AppInfo { String name; String packageName; String pinyin; AppInfo(String name, String packageName, String pinyin) { this.name = name; this.packageName = packageName; this.pinyin = pinyin; } } private class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ViewHolder> { @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false); return new ViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { final AppInfo appInfo = mAppList.get(position); holder.tvName.setText(appInfo.name); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { Intent intent = getPackageManager().getLaunchIntentForPackage(appInfo.packageName); if (intent != null) { startActivity(intent); } } catch (Exception e) { Log.e(TAG, "Launch app error: " + e.getMessage()); } } }); } @Override public int getItemCount() { return mAppList.size(); } class ViewHolder extends RecyclerView.ViewHolder { TextView tvName; ViewHolder(View itemView) { super(itemView); tvName = itemView.findViewById(R.id.tv_app_name); } } } } ``` 其中, `loadAppList()` 方法用于加载应用列表,并按照拼音排序和生成索引映射表;`IndexBarDecoration` 类用于实现字幕条索引和提示。需要在布局文件中添加 `RecyclerView` 和 `TextView` 控件,并分别为它们设置 ID。注意在 `AndroidManifest.xml` 文件中添加读取应用列表的权限: ```xml <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值