一个简易版的新闻应用

YXTS122 YXTS122 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
MainActivity.java
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>
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
YXTS122
Android 80,489 成员 91,375 内容