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>

最后运行结果如下:

界面没设计额,很丑!!!!!!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值