一个简易版的新闻应用
MainActivity.java
News.java
NewsAdapter.java
NewsContentActivity.java
NewsContentFragment.java
NewsTitleFragment.java
activity_mai.xml
activity_main.xml
news_title_frag.xml
news_item.xml
package com.example.newsapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
News.java
package com.example.newsapp;
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;
}
}
NewsAdapter.java
package com.example.newsapp;
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;
}
}
NewsContentActivity.java
package com.example.newsapp;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Window;
public class NewsContentActivity extends FragmentActivity
{
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);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_content);
FragmentManager fragmentManager=getSupportFragmentManager();
String newsTitle=getIntent().getStringExtra("news_title");
String newsContent=getIntent().getStringExtra("news_content");
NewsContentFragment newsContentFragment=(NewsContentFragment)fragmentManager.findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(newsTitle,newsContent);
}
}
NewsContentFragment.java
package com.example.newsapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class NewsContentFragment extends Fragment
{
private View view;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
view=inflater.inflate(R.layout.news_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);
}
}
NewsTitleFragment.java
package com.example.newsapp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class NewsTitleFragment extends Fragment implements OnItemClickListener
{
private ListView newsTitleListView;
private List<News> newsList;
private NewsAdapter adapter;
private boolean isTwoPane;
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
newsList=getNews();
adapter=new NewsAdapter(activity,R.layout.news_item,newsList);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.news_title_frag,container,false );
newsTitleListView=(ListView)view.findViewById(R.id.news_title_list_view);
newsTitleListView.setAdapter(adapter);
newsTitleListView.setOnItemClickListener(this );
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (getActivity().findViewById(R.id.news_content_layout)!=null )
isTwoPane=true;
else
isTwoPane=false;
}
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
News news=newsList.get(position);
if (isTwoPane)
{
NewsContentFragment newsContentFragment=(NewsContentFragment)getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(news.getTitle(),news.getContent());
}
else
{
NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
}
}
private List<News> getNews()
{
List<News> newsList=new ArrayList<News>();
News news1=new News();
news1.setTitle("标题");
news1.setContent("内容");
newsList.add(news1);
News news2=new News();
news2.setTitle("日记");
news2.setContent("去旅游");
newsList.add(news2);
return newsList;
}
} activity_mai.xml
<?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" >
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.newsapp.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.newsapp.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.newsapp.MainActivity" >
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.newsapp.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
news_title_frag.xml
<?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" >
<ListView
android:id="@+id/news_title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
news_item.xml
<?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>
...全文
相关推荐
Fragment的最佳实践-一个简易版的新闻应用
Fragment的最佳实践-一个简易版的新闻应用
ios-简易版新闻客户端使用SDAutolayout适配.zip
空闲时间写的一个简易版新闻客户端,由于时间紧迫该封装的没有封装,主要自己封装了一个循环滚动图片WBScrollBanner 使用起来非常简单。主要用到SDAutolayout一键适配,如有问题请联系QQ299814
一个简易新闻网站的源码
新闻网站的源码 做课程设计或毕业设计的时候用得到
ios-网易新闻.zip
网易新闻界面的简单实现,包括上下的联动 label的放大与缩小
雨楠万能ASP采集程序 简易版 v0.1
雨楠万能ASP采集程序简易版V0.1 功能1.可以采集类型、标题、内容、时间2.自行配置3.可以整合到你自己的程序里


