场景
提示:基于期末作业开发+(自增轮播图)
自评:效果蛮丑的,功能都在,仅供参考!
内容:
一,引导页
1,设计引导页LogoActivity,添加引导页所需图片和引导圆点设计
//引导页面至少三张图片
public class LogoActivity extends AppCompatActivity {
private List<ImageView> image_points;
private LogoAdapter logoAdapter;
private List<Integer> imageIds;
private ViewPager vp_logo;
private boolean isLastPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// @SuppressLint("ResourceType") View login = findViewById(R.layout.activity_login);
setContentView(R.layout.activity_logo);
vp_logo = findViewById(R.id.vp_logo);
imageIds = new ArrayList<>();
imageIds.add(R.drawable.logo1);
imageIds.add(R.drawable.logo2);
imageIds.add(R.drawable.logo3);
imageIds.add(R.drawable.logo4);
logoAdapter = new LogoAdapter(imageIds, this);
vp_logo.setAdapter(logoAdapter);
image_points = new ArrayList<>();
LinearLayout ll_points = findViewById(R.id.ll_points);
for (int i = 0; i < imageIds.size(); i++) {
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
imageView.setImageResource(R.drawable.point_blue);
final int finalI = i;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vp_logo.setCurrentItem(finalI);
}
});
image_points.add(imageView);
ll_points.addView(imageView);
}
private void setPoint(int position) {
for (int i = 0; i < image_points.size(); i++) {
if (i == position) {
image_points.get(i).setImageResource(R.drawable.point_blue);
} else {
image_points.get(i).setImageResource(R.drawable.point_pink);
}
}
}
}
2,同时设计引导页面图片适配容器,item_logo布局
public class LogoAdapter extends PagerAdapter {
List<Integer> imageIds ;
Context ctx;
public LogoAdapter(List<Integer> imageIds, Context ctx){
this.imageIds = imageIds;
this.ctx = ctx;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position){
ImageView imageView = (ImageView) LayoutInflater.from(ctx).inflate(R.layout.item_logo,
container, false);
imageView.setImageResource(imageIds.get(position));
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container,int position,@NonNull Object object){
container.removeView((View) object);
}
@Override
public int getCount() {
return imageIds.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
}
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
3,在LogoActivity设计结束跳转登入页面
vp_logo.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
setPoint(position);
}
@Override
public void onPageSelected(int position) {
setPoint(position);
isLastPage = position == imageIds.size() - 1;
}
@Override
public void onPageScrollStateChanged(int state) {
if (isLastPage) {
goLoginActivity();
}
}
});
setPoint(0);
}
public void goLoginActivity() {
startActivity(new Intent(this, LoginActivity.class));
finish();
}
效果图:
二,登入界面
1,生成登入,LoginDataSource修改登入信息和样式
public class LoginDataSource {
public Result<LoggedInUser> login(String username, String password) {
try {
// TODO: handle loggedInUser authentication
if(username.equals("郭淑瑛")&&password.equals("123")){
LoggedInUser fakeUser =
new LoggedInUser(
java.util.UUID.randomUUID().toString(),
username+"Doe");
return new Result.Success<>(fakeUser);
}else {
return new Result.Error(new IOException("账号密码错误!"));
}
} catch (Exception e) {
return new Result.Error(new IOException("Error logging in", e));
}
}
public void logout() {
// TODO: revoke authentication
}
}
2,在LoginActivity加登入成功跳转事件,跳转到主页面
startActivity(new Intent(LoginActivity.this, MainActivity.class));
效果图:
三,主页面显示新闻列表
1,item_news_normal.xml规范列表样式部署item_news_tv_title,item_news_tv_time,item_news_tv_img
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/palegoldenrod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/item_news_linearlayout"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_news_height"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:visibility="visible">
<TextView
android:id="@+id/item_news_tv_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="4"
android:text="@string/default_text"
android:textColor="@color/black"/>
<ImageView
android:id="@+id/item_news_tv_img"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_weight="2"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/item_news_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="10 minutes ago"
android:textColor="@color/darkgray"
android:textSize="12sp"/>
<TextView
android:id="@+id/item_news_tv_arrow"
android:layout_width="3dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@color/darkgray" />
<TextView
android:id="@+id/item_news_tv_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/item_news_tv_arrow"
android:text="@string/source_news_text"
android:textColor="@color/darkgray"
android:textSize="12sp"/>
</RelativeLayout>
</LinearLayout>
2,创建新闻容器NewsBean,和NewsHttp get json数据和解析所需数据标题等
private List<NewsBean> jsonNews(String json) {
List<NewsBean> newsBeans = null;
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("T1348647853363");
newsBeans = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String imgSrc = object.getString("imgsrc");
String time = object.getString("mtime");
NewsBean newsBean = new NewsBean(title, imgSrc, time);
newsBeans.add(newsBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
return newsBeans;
3,在MainActivity调用使用
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_news_normal, viewGroup, false);
}
TextView tv_title = view.findViewById(R.id.item_news_tv_title);
TextView tv_time = view.findViewById(R.id.item_news_tv_time);
ImageView iv_img = view.findViewById(R.id.item_news_tv_img);
tv_title.setText(getItem(i).getTitle());
tv_time.setText(getItem(i).getTime());
Glide.with(MainActivity.this).load(getItem(i).imgSrc).into(iv_img);
return view;
}
public void add(List<NewsBean> newsBeans) {
MainActivity.this.newsBeans.clear();
MainActivity.this.newsBeans.addAll(newsBeans);
notifyDataSetChanged();
}
效果图:
四,轮播图
查看参考,是直接采用的方法
效果
不好意思,不会做动图
完整代码链接
完整讲解加代码和效果视频:查看资源
参考
(87条消息) Android开发-Android应用中启动时引导页的实现__彼岸雨敲窗_的博客-CSDN博客_android 引导页实现
(117条消息) Android Studio 出现“Cannot resolve symbol” 解决办法_极客神殿-CSDN博客_cannot resolve symbol
(144条消息) Android使用banner实现自动手动轮播图_李小白的博客的博客-CSDN博客_android banner自动轮播