https://github.com/xiaohaibin/PageMenuLayout,由于这位大佬的开源的首页分页导航菜单功能写的是很好,但是让我们去集成他的依赖时,如果是使用androidx版本的话,基本是用不了,所有我就直接把他用到相关类重新引用了一些,修改成了适用Androidx的项目,我自己重新生成了一个模块,上传到了github,你们可以直接去下载使用,导入模块就行了,不用再去添加上面的github的依赖了,看一下我重新修改成androidx版本的效果
如果集成上面的开源库很难,你可以尝试一下我这个修改androidx版本的demo,可以看里面的源码即可完成。不用再去依赖PageMenuLayout项目的库了。
https://github.com/yezi10086/PageMenu_androidxdemo
1.下载好,导入module:
implementation project(':pagemenulayoutandroidx')
implementation 'androidx.recyclerview:recyclerview:1.1.0'
2.引入成功之后,调用(参考我的MainActivity):
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.androidx.pagemenulayoutandroidx.IndicatorView;
import com.androidx.pagemenulayoutandroidx.PageMenuLayout;
import com.androidx.pagemenulayoutandroidx.ScreenUtil;
import com.androidx.pagemenulayoutandroidx.holder.AbstractHolder;
import com.androidx.pagemenulayoutandroidx.holder.PageMenuViewHolderCreator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<ModelHomeEntrance> homeEntrances;
IndicatorView entranceIndicatorView;
PageMenuLayout<ModelHomeEntrance> mPageMenuLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//屏幕适配绑定,一定要写,否则加载不出来
ScreenUtil.init(MainActivity.this);
initView();
//初始化数据
initData();
init();
}
private void initView() {
entranceIndicatorView = findViewById(R.id.main_home_entrance_indicator);
mPageMenuLayout = findViewById(R.id.pagemenu);
}
//初始化数据
private void initData() {
homeEntrances = new ArrayList<>();
homeEntrances.add(new ModelHomeEntrance("美食", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("电影", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("酒店住宿", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("生活服务", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("KTV", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("旅游", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("学习培训", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("汽车服务", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("摄影写真", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("休闲娱乐", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("丽人", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("运动健身", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("大保健", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("团购", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("景点", R.mipmap.shouye1));
homeEntrances.add(new ModelHomeEntrance("全部分类", R.mipmap.shouye1));
}
private void init() {
mPageMenuLayout.setPageDatas(homeEntrances, new PageMenuViewHolderCreator() {
@Override
public AbstractHolder createHolder(View itemView) {
return new AbstractHolder<ModelHomeEntrance>(itemView) {
private TextView entranceNameTextView;
private ImageView entranceIconImageView;
@Override
protected void initView(View itemView) {
entranceIconImageView = itemView.findViewById(R.id.entrance_image);
entranceNameTextView = itemView.findViewById(R.id.entrance_name);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) ((float) ScreenUtil.getScreenWidth() / 4.0f));
itemView.setLayoutParams(layoutParams);
}
@Override
public void bindView(RecyclerView.ViewHolder holder, final ModelHomeEntrance data, int pos) {
entranceNameTextView.setText(data.getName());
entranceIconImageView.setImageResource(data.getImage());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, data.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
}
@Override
public int getLayoutId() {
return R.layout.item_home_entrance;
}
});
entranceIndicatorView.setIndicatorCount(mPageMenuLayout.getPageCount());
mPageMenuLayout.setOnPageListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
entranceIndicatorView.setCurrentIndicator(position);
}
});
}
}
当然里面还有布局,和实体类的调用,就不放上来了,缺什么就从我的github的项目去cop进去即可,如果你有什么不懂的可以评论留言,在这里还是感谢开源的作者,写的很好,但是能把开源使用方式说明白一点,我觉得更完美了,谢谢他的付出。