1. 程式人生 > >RecyclerView實現簡易版的新聞應用

RecyclerView實現簡易版的新聞應用

主要時利用Fragment和RecyclerView來實現,有正在學的可以關注下交流一下!!!!

主要就是根據螢幕大小建立兩個activity_main佈局。在佈局裡設定fragment來顯示新聞列表和內容。

1.首先在app/build.grade當中新增依賴庫

dependencies {  
  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    compile 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' }
還是老樣子,版本號要對上才可以使用。

2.新建新聞實體類News

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

3.新建佈局檔案news_content_frag.xml,作為新聞內容的佈局。
<LinearLayout
    android:id="@+id/visibility_layout"
    android:orientation="vertical"
    android:visibility="invisible"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/news_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#00f"
        android:textSize="30dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000"/>
    <TextView
        android:id="@+id/news_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#000"
        android:textSize="25dp"/>
</LinearLayout>


4.新建NewsContentFragment類
public class NewsContentFragment extends Fragment {
    private View view;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.new_content_frag,container,false);
        return view;
    }
    public void refresh(String newsTitle,String newsContent)//重新整理新聞標題和內容
    {
        View visibilityLayout=view.findViewById(R.id.visibility_layout);
        visibilityLayout.setVisibility(View.VISIBLE);//顯示新聞內容佈局
        TextView newsTitleText=(TextView) view.findViewById(R.id.news_title);
        TextView newsContentText=(TextView) view.findViewById(R.id.news_content);
        newsTitleText.setText(newsTitle);
        newsContentText.setText(newsContent);
    }
}
5.新建NewsContentActivity,並將佈局名改為news_content,修改news_content.xml中的程式碼
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/news_content_frag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.example.administrator.fragmentbestpractice.NewsContentFragment"/>
    </LinearLayout>

6.修改NewsContentActivity中的程式碼
public class NewsContentActivity extends AppCompatActivity {

    public static void actionStart(Context context,String newsTitle,String newsContent)
    {
        Intent intent=new Intent(context,NewsContentActivity.class);
        intent.putExtra("news_title",newsTitle);
        intent.putExtra("news_content",newsContent);
        context.startActivity(intent);
    }使用Intent傳遞資訊,從context跳轉到NewsContentActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_content);
        String newsTitle=getIntent().getStringExtra("news_title");
        String newsContent=getIntent().getStringExtra("news_content");
        NewsContentFragment newsContentFragment=(NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_frag);
        newsContentFragment.refresh(newsTitle,newsContent);//重新整理NewsContentFragment介面
    }
}
7.新建news_title_frag.xml,用於顯示新聞列表
 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/news_title_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

8.新建news_item.xml作為RecyclerView子項的佈局
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/news_title"
            android:layout_width="1dp"
            android:layout_height="35dp"
            android:layout_weight="1"
            android:textSize="20sp" />
    </LinearLayout>
9.新建NewsTitleFragment作為新聞列表的碎片
public class NewsTitleFragment extends Fragment {

    private boolean isTwoPane;

    public View onCreateView(LayoutInflater inflater, ViewGroup contrainer, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.news_item_frag,contrainer,false);
        RecyclerView recyclerView=(RecyclerView)view.findViewById(R.id.news_title_recycler_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);//指定佈局方式
        NewsTitleAdapter newsTitleAdapter=new NewsTitleAdapter(getNews());
        recyclerView.setAdapter(newsTitleAdapter);//指定介面卡
        return view;
    }
    private List<News> getNews()//初始化訊息資料
    {
        List<News> newsList=new ArrayList<>();
        for (int i=1;i<50;i++)
        {
            News news=new News();
            news.setTitle("This is"+i+"news");
            news.setContent("This is"+i+"content");
            newsList.add(news);
        }
        return newsList;
    }
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        if(getActivity().findViewById(R.id.news_content_layout)!=null)//通過是否能找到元件,判斷是不是雙頁顯示
        {
            isTwoPane=true;
        }else{
            isTwoPane=false;
        }
    }
    class NewsTitleAdapter extends RecyclerView.Adapter<NewsTitleAdapter.ViewHolder> {//在內部編寫介面卡類,號利用isTwoPane變數
        private List<News> mNewsList;
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView newsTitleText;
            public ViewHolder(View view)
            {
                super(view);
                newsTitleText=(TextView) view.findViewById(R.id.news_title);
            }
        }
        public NewsTitleAdapter(List<News> list)
        {
            mNewsList=list;
        }
        public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
        {
            View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
            final ViewHolder viewHolder=new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    News news=mNewsList.get(viewHolder.getAdapterPosition());
                    if(isTwoPane)
                    {
                        NewsContentFragment newsContentFragment=(NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_frag);
                        newsContentFragment.refresh(news.getTitle(),news.getContent());
                    }else
                    {
                        NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
                    }
                }
            });
            return viewHolder;
        }
        public void onBindViewHolder(ViewHolder holder,int position)
        {
            News news=mNewsList.get(position);
            holder.newsTitleText.setText(news.getTitle());
        }
        public int getItemCount()
        {
            return mNewsList.size();
        }
    }

}
10.最後編寫兩個activity_main佈局
<FrameLayout
        android:id="@+id/news_title_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:name="com.example.administrator.fragmentbestpractice.NewsTitleFragment"
            android:id="@+id/news_title_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
activity_main.xml(sw600dp)
<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:id="@+id/news_title_fragment"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
        <FrameLayout
            android:id="@+id/news_content_layout"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent">
            <fragment
                android:id="@+id/news_content_fragment"
                android:name="com.example.administrator.fragmentbestpractice.NewsContentFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </FrameLayout>
    </LinearLayout>

最後執行結果如下:

介面沒設計額,很醜!!!!!!