1, Using shards for a best practice, we write a news app
1. First, create a news class
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. Then we set up an interface to display news
<?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>
There are several new property settings that we haven't seen before. First of all, android:singLine setting to true means TextView can only display in one line. android:ellipse is used to set the text abbreviation when the text content exceeds the width of the control. Here, end means to abbreviate at the end.
3. Next, we need to create an adapter for News list, which inherits from ArrayAdapter and specifies the generic as News class. Let's create a new 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; } }
As you can see, in the getView() method, we get the News class in the corresponding location, and let the News title display in the list.
4. Code the news content
<?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>
3, Source code:
1. Project address
https://github.com/ruigege66/Android/tree/master/FragmentBestPractise
2.CSDN: https://blog.csdn.net/weixin_44630050
3. Blog Park: https://www.cnblogs.com/ruige0000/
4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.