您的位置:首页 > 移动开发 > Android开发

第一行代码Android学习(五)

2016-08-15 19:09 369 查看
第一行代码Android学习:第五部分主要实现了一个简易版的新闻应用,其中主要涉及到fragment的操作

1.layout文件

activity_main.xml

<RelativeLayout 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"
tools:context=".MainActivity" >

<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.dyham_04_01fragmentbasepractice.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>


item_news.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:ellipsize="end"
android:padding="10dp"
android:singleLine="true"
android:textSize="18sp" />

</LinearLayout>


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" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" >
</View>

<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>

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#000000" >
</View>

</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"
android:orientation="vertical" >

<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.dyham_04_01fragmentbasepractice.NewsContentFragment"
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/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

</LinearLayout>


2.layout-sw320dp文件夹

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"
tools:context=".MainActivity" >

<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.dyham_04_01fragmentbasepractice.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.dyham_04_01fragmentbasepractice.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

</LinearLayout>


3.src

MainActivity.java

package com.example.dyham_04_01fragmentbasepractice;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}

}


News.java

/*
* @Title:  News.java
* @Description:  TODO
* @author:  张志安
* @date:  2016-8-15 下午5:08:26
*
*/
package com.example.dyham_04_01fragmentbasepractice;

/**
* TODO 新闻实体类
*
* @author 张志安
* @date: 2016-8-15 下午5:08:26
*/
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

/*
* @Title:  NewsAdapter.java
* @Description:  TODO
* @author:  张志安
* @date:  2016-8-15 下午5:16:38
*
*/
package com.example.dyham_04_01fragmentbasepractice;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
* TODO
*
* @author 张志安
* @date: 2016-8-15 下午5:16:38
*/
public class NewsAdapter extends ArrayAdapter<News> {

private int resourceId;

/**
* <默认构造函数>
*/
public NewsAdapter(Context context, int textViewResourceId, List 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 newsTextView = (TextView) view.findViewById(R.id.news_title);
newsTextView.setText(news.getTitle());
return view;
}

}


NewsContentActivity.java

/*
* @Title:  NewscContentActivity.java
* @Description:  TODO
* @author:  张志安
* @date:  2016-8-15 下午5:38:41
*
*/
package com.example.dyham_04_01fragmentbasepractice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

/**
* TODO
*
* @author 张志安
* @date: 2016-8-15 下午5:38:41
*/
public class NewsContentActivity extends Activity {
/**
* TODO Activity跳转方法
*
* @throw
* @return void
*/
public static void actionStart(Context context, String newsTitle,
String newsContent) {
Intent intent = new Intent(context, NewsContentActivity.class);
intent.putExtra("newsTitle", newsTitle);
intent.putExtra("newsContent", newsContent);
context.startActivity(intent);
}

/**
* 重载方法
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_content);
String newsTitle = getIntent().getStringExtra("newsTitle");
String newsContent = getIntent().getStringExtra("newsContent");
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager()
.findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(newsTitle, newsContent);
}
}


NewsContentFragment.java

/*
* @Title:  NewsContentFragment.java
* @Description:  TODO
* @author:  张志安
* @date:  2016-8-15 下午5:28:41
*
*/
package com.example.dyham_04_01fragmentbasepractice;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* TODO 新闻内容的fragment
* @author  张志安
* @date:  2016-8-15 下午5:28:41
*/
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;
}

/**
* TODO 刷新页面
* @throw
* @return void
*/
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

/*
* @Title:  NewsTitleFragment.java
* @Description:  TODO
* @author:  张志安
* @date:  2016-8-15 下午5:49:41
*
*/
package com.example.dyham_04_01fragmentbasepractice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
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;

/**
* TODO
*
* @author 张志安
* @date: 2016-8-15 下午5:49:41
*/
public class NewsTitleFragment extends Fragment implements OnItemClickListener {

private ListView lv;

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.item_news, newsList);
}

/**
* 重载方法
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater
.inflate(R.layout.news_title_frag, container, false);
lv = (ListView) view.findViewById(R.id.lv);
lv.setAdapter(adapter);
lv.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;
}

}

/**
* TODO 获取新闻数据
*
* @throw
* @return List<News>
*/
private List<News> getNews() {
List<News> newsList = new ArrayList<News>();
News news1 = new News();
news1.setTitle("123123sdassssssssssssss");
news1.setContent("sdfajkkkkkkkkkkkkkkkkkkkkkkkhnjfhasdkfhsiudhfsdijhfajksdhlfsahdfhsdjkhfkljsadhfkljadklfahsjlk");
newsList.add(news1);
News news2 = new News();
news2.setTitle("123123sdassssssssssssss");
news2.setContent("sdfajkkkkkkkkkkkkkkkkkkkkkkkhnjfhasdkfhsiudhfsdijhfajksdhlfsahdasddadadadfhsdjkhfkljsadhfkljadklfahsjlk");
newsList.add(news2);
return newsList;
}

/**
* 重载方法
*/
@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());
}
}

}


代码下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android fragment