主要时利用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;
}
}
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中的代码
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,用于显示新闻列表
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 getNews()//初始化消息数据
{
List 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 {//在内部编写适配器类,号利用isTwoPane变量
private List 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 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布局
activity_main.xml(sw600dp)