• Android连载11-新闻app优秀实践


    一、使用碎片来进行一个最佳实践,即我们写一个新闻的app

    1.首先先建立一个新闻类

    package com.example.fragmentbestpractice;
    
    ​
    
    public class News {
    
     
    
      private String title;
    
     
    
      private String content;
    
    ​
    
      public String getTitle() {
    
        return title;
    
      }
    
    ​
    
      public void setTitle(String title) {
    
        this.title = title;
    
      }
    
    ​
    
      public String getContent() {
    
        return content;
    
      }
    
    ​
    
      public void setContent(String content) {
    
        this.content = content;
    
      }
    
     
    
     
    
    }

    2.然后我们设置一个界面,也就是显示新闻的界面

     
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent"
    
        android:orientation="vertical" >
    
       
    
        <TextView
    
            android:id="@+id/news_title"
    
            android:layout_width="match_parent"
    
            android:layout_height="wrap_content"
    
            android:singleLine="true"
    
            android:ellipsize="end"
    
            android:textSize="18sp"
    
            android:paddingLeft="10dp"
    
            android:paddingRight="10dp"
    
            android:paddingTop="15dp"
    
            android:paddingBottom="15dp"
    
            /></LinearLayout>

    这里面有几个新的属性设置是我们之前没有见到过的,首先来看android:singLine设置为true代表的就是TextView只能单行显示;android:ellipse用于设定当文本内容超出控件的宽度的时候,文本的缩略方式,这里指定成end表示在尾部进行缩略​。

    3.接下来需要创建一个新闻列表的适配器,让这个适配器继承自ArrayAdapter,并将泛型指定为News类,下面我们新建NewsAdapter

    package com.example.fragmentbestpractice;
    
    ​
    
    import java.util.List;
    
    ​
    
    import android.content.Context;
    
    import android.view.LayoutInflater;
    
    import android.view.View;
    
    import android.view.ViewGroup;
    
    import android.widget.ArrayAdapter;
    
    import android.widget.TextView;
    
    ​
    
    public class NewsAdapter extends ArrayAdapter<News>{
    
     
    
      private int resourceId;
    
     
    
      public NewsAdapter(Context context,int textViewResourceId,List<News> objects) {
    
        super(context,textViewResourceId,objects);
    
        resourceId = textViewResourceId;
    
      }
    
     
    
      @Override
    
      public View getView(int position,View convertView,ViewGroup parent) {
    
        News news = getItem(position);
    
        View view;
    
        if(convertView == null) {
    
          view = LayoutInflater.from(getContext()).inflate(resourceId,null);
    
        }else {
    
          view = convertView;
    
        }
    
        TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);
    
        newsTitleText.setText(news.getTitle());
    
        return view;
    
      }
    
    }

    可以看出来,在getView()方法中,我们获取到了相应位置上的News类,并且让新闻的标题在列表中​进行显示。

    4.编写新闻内容部分的代码

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="match_parent"
    
        android:layout_height="match_parent" >
    
       
    
        <LinearLayout
    
            android:id="@+id/visibility_layout"
    
            android:layout_width="match_parent"
    
            android:layout_height="match_parent"
    
            android:orientation="vertical"
    
            android:visibility="invisible">
    
           
    
            <TextView
    
                android:id="@+id/news_title"
    
                android:layout_width="match_parent"
    
                android:layout_height="wrap_content"
    
                android:gravity="center"
    
                android:padding="10dp"
    
                android:textSize="20sp" />
    
           
    
            <ImageView
    
                android:layout_width="match_parent"
    
                android:layout_height="1dp"
    
                android:scaleType="fitXY"
    
                android:src="@drawable/split_line" />
    
           
    
           <TextView
    
               android:id="@+id/news_content"
    
               android:layout_width="match_parent"
    
               android:layout_height="0dp"
    
               android:layout_weight="1"
    
               android:padding="15dp"
    
               android:textSize="18sp" />
    
     
    
        </LinearLayout>
    
       
    
        <ImageView
    
            android:layout_width="1dp"
    
            android:layout_height="match_parent"
    
            android:layout_alignParentLeft="true"
    
            android:scaleType="fitXY"
    
            android:src="@drawable/split_line_vertical" /></RelativeLayout>

    三、源码:

    1.项目地址

    https://github.com/ruigege66/Android/tree/master/FragmentBestPractise

    2.CSDN:https://blog.csdn.net/weixin_44630050

    3.博客园:https://www.cnblogs.com/ruigege0000/

    4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

     

  • 相关阅读:
    Java实现分布式锁
    Java中拦截器实、过滤器和监听器的实现原理
    Version 28 (intended for Android Pie and below) is the last version of the legacy support library
    AndroidManifest.xml文件报Activity supporting ACTION_VIEW is not set as BROWSABLE
    flowable 流程图片中文乱码
    java 常用工具类 (值得收藏)
    Oracle 创建表空间和用户脚本
    spring cloud alibaba springboot nacos 版本对应
    Tomcat 配置支持不同的域名访问各自不同程序的配置方法
    flowable 表结构说明
  • 原文地址:https://www.cnblogs.com/ruigege0000/p/12903102.html
Copyright © 2020-2023  润新知