一个简易版的新闻应用

YXTS122 2017-02-13 06:06:43
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>
...全文
235 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2017-02-17
  • 打赏
  • 举报
回复
执行到MainActivity.java里的setContentView时会去执行NewsTitleFragment.java里的onAttach,onCreateView,然后又回到MainActivity.java这里一下,又去执行 onActivityCreated,然后执行getView两遍,就出来了界面,点击一下,就去执行onItemClick里的actionStart这个,然后跳转, 执行到NewsContentActivity.java里的setContentView时会去执行NewsContentFragment.java里的onCreateView
YXTS122 2017-02-13
  • 打赏
  • 举报
回复
news_content_frag.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <LinearLayout 
        android:id="@+id/visibility_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="invisible" >
        
        <TextView 
            android:id="@+id/news_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textSize="20sp" />
        
        <ImageView
            android:contentDescription="@string/app_name" 
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
        
        <TextView 
            android:id="@+id/news_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:padding="15dp"
            android:textSize="18sp" />
    </LinearLayout>
    
     <ImageView
            android:contentDescription="@string/app_name" 
            android:layout_height="match_parent"
            android:layout_width="1dp"
            android:layout_alignParentLeft="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
       
</RelativeLayout>
  
news_content.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_content_fragment"
        android:name="com.example.newsapp.NewsContentFragment" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newsapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".NewsContentActivity" >
            
        </activity>
    </application>

</manifest>
  

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧