1. 项目模块功能思维导图
2. 项目涉及到的技术点
- 数据来源:聚合数据API
- 使用okhttp网络请求框架获取api数据
- 使用gson库解析json数据
- 使用RecyclerView+adapter实现新闻列表
- 使用SQLite数据库实现用户登录,注册,浏览历史记录
- 使用SharedPreferences 实现记住密码登录
- 使用TabLayout+ViewPager2实现新闻分类滑动
- 使用DrawerLayout+NavigationView实现抽屉
- 使用WebView实现新闻详情数据加载
- 使用Glide实现新闻图片加载
3. 项目截图
3. 部分代码实现
- 欢迎页实现
public class WelcomeActivity extends AppCompatActivity {private TextView tvCountdown;private CountDownTimer countDownTimer;private long timeLeftInMillis = 3000; // 设置倒计时时长,单位为毫秒@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);//初始化控件tvCountdown =findViewById(R.id.tv_countdown);// 启动倒计时startCountdown();}private void startCountdown() {countDownTimer =new CountDownTimer(timeLeftInMillis,1000) {@Overridepublic void onTick(long millisUntilFinished) {timeLeftInMillis = millisUntilFinished;int secondsRemaining = (int) (millisUntilFinished / 1000);tvCountdown.setText(secondsRemaining +" s | 跳转");}@Overridepublic void onFinish() {//跳转到登录页面(看自己逻辑想跳转哪个页面)startActivity(new Intent(WelcomeActivity.this, MainActivity.class));// 倒计时结束后的操作,例如跳转到主页面finish();}}.start();}@Overrideprotected void onDestroy() {super.onDestroy();if (countDownTimer != null) {countDownTimer.cancel();}}
}
- 首页实现
public class MainActivity extends AppCompatActivity {// private String[] titles = {"娱乐", "军事", "教育", "文化", "将康", "财经", "体育", "汽车", "科技"};private List<TitleInfo> titles = new ArrayList<>();private TabLayout tab_layout;private ViewPager2 viewPager;private NavigationView nav_view;private TextView tv_username;private TextView tv_nickname;private ImageView btn_open_drawerLayout;private DrawerLayout drawer_layout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化title数据titles.add(new TitleInfo("推荐", "top"));titles.add(new TitleInfo("国内", "guonei"));titles.add(new TitleInfo("国际", "guoji"));titles.add(new TitleInfo("娱乐", "yule"));titles.add(new TitleInfo("体育", "tiyu"));titles.add(new TitleInfo("军事", "junshi"));titles.add(new TitleInfo("科技", "keji"));titles.add(new TitleInfo("财经", "caijing"));titles.add(new TitleInfo("游戏", "youxi"));titles.add(new TitleInfo("汽车", "qiche"));titles.add(new TitleInfo("健康", "jiankang"));//初始化控件tab_layout = findViewById(R.id.tab_layout);viewPager = findViewById(R.id.viewPager);nav_view = findViewById(R.id.nav_view);btn_open_drawerLayout = findViewById(R.id.btn_open_drawerLayout);drawer_layout = findViewById(R.id.drawer_layout);//注意事项, 不能直接findViewById// tv_username=findViewById(R.id.nav_view);// tv_username = nav_view.getHeaderView(0).findViewById(R.id.tv_username);tv_username = nav_view.getHeaderView(0).findViewById(R.id.tv_username);tv_nickname = nav_view.getHeaderView(0).findViewById(R.id.tv_nickname);//btn_open_drawerLayout打开抽屉btn_open_drawerLayout.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {drawer_layout.open();}});//nav_view点击事件nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {if (item.getItemId() == R.id.nav_history) {//跳转到历史记录Intent intent = new Intent(MainActivity.this, HistoryListActivity.class);startActivity(intent);} else if (item.getItemId() == R.id.nav_update_pwd) {//判断是否登录UserInfo userInfo = UserInfo.getUserInfo();if (null != userInfo) {startActivity(new Intent(MainActivity.this, UpdatePwdActivity.class));} else {Toast.makeText(MainActivity.this, "请先登录~~", Toast.LENGTH_SHORT).show();}} else if (item.getItemId() == R.id.nav_about) {startActivity(new Intent(MainActivity.this, AboutActivity.class));} else if (item.getItemId() == R.id.nav_exit) {UserInfo userInfo = UserInfo.getUserInfo();if (null!=userInfo){new AlertDialog.Builder(MainActivity.this).setTitle("温馨提示").setMessage("确认要退出登录吗?").setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {startActivity(new Intent(MainActivity.this, LoginActivity.class));UserInfo.setUserInfo(null);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}else {Toast.makeText(MainActivity.this, "请先登录~~", Toast.LENGTH_SHORT).show();}}return true;}});//viewPager 需要设置一个adapterviewPager.setAdapter(new FragmentStateAdapter(this) {@NonNull@Overridepublic Fragment createFragment(int position) {String title = titles.get(position).getPy_title();TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);return tabNewsFragment;}@Overridepublic int getItemCount() {return titles.size();}});//tab_layout点击事件tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {@Overridepublic void onTabSelected(TabLayout.Tab tab) {//设置viewPager选中当前页viewPager.setCurrentItem(tab.getPosition(), false);}@Overridepublic void onTabUnselected(TabLayout.Tab tab) {}@Overridepublic void onTabReselected(TabLayout.Tab tab) {}});//tab_layout和viewPager关联在一起TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {@Overridepublic void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {tab.setText(titles.get(position).getTitle());}});//这句话一定不能少tabLayoutMediator.attach();}@Overrideprotected void onResume() {super.onResume();UserInfo userInfo = UserInfo.getUserInfo();if (null != userInfo) {tv_username.setText(userInfo.getUsername());tv_nickname.setText(userInfo.getNickname());} else {tv_username.setText("请登录");tv_nickname.setText("");//登录点击事件tv_username.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, LoginActivity.class);startActivity(intent);}});}}
}
- 新闻详情页
public class NewsDetailsActivity extends AppCompatActivity {private NewsInfo.ResultDTO.DataDTO dataDTO;private Toolbar toolbar;private WebView mWebView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_news_details);//初始化控件toolbar = findViewById(R.id.toolbar);mWebView = findViewById(R.id.webView);//获取传递的数据dataDTO = (NewsInfo.ResultDTO.DataDTO) getIntent().getSerializableExtra("dataDTO");//设置数据if (null != dataDTO) {toolbar.setTitle(dataDTO.getTitle());mWebView.loadUrl(dataDTO.getUrl());//添加历史记录String dataDTOJson = new Gson().toJson(dataDTO);UserInfo userInfo = UserInfo.getUserInfo();if (userInfo!=null){HistoryDbHelper.getInstance(NewsDetailsActivity.this).addHistory(userInfo.getUsername(),dataDTO.getUniquekey(),dataDTOJson);}else {HistoryDbHelper.getInstance(NewsDetailsActivity.this).addHistory(null,dataDTO.getUniquekey(),dataDTOJson);}}//返回toolbar.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}
}