android的新闻显示。

ListVIew+json+Fragment+RadioGroup。

MainActivity.java

package com.newsjsontest;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.newsjsontest.fragment.HeadLine_Fragment;

public class MainActivity extends Activity implements OnCheckedChangeListener{
	/*private static final String TAG="MainActivity";
	private List<News> list;
	private MyBaseAdapter adapter;*/
	private HeadLine_Fragment headLine_Fragment;
	private FragmentManager fragmentManager;
	private HeadLine_Fragment society_Fragment ;
	private HeadLine_Fragment inland_Fragment;
	private HeadLine_Fragment international_Fragment;
	private Bundle args;
	private FragmentTransaction beginTransaction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		/*list = new ArrayList<News>();
		ListView listView=(ListView) findViewById(R.id.headLine_news);
		
		//新建异步任务
		MyAsyncTask myAsyncTask= new MyAsyncTask();
		//开启异步任务
//		myAsyncTask.execute(NewsUrlPath.getUrlHeadlinePath());
//		myAsyncTask.execute(NewsUrlPath.getUrlInlandPath());
//		myAsyncTask.execute(NewsUrlPath.getUrlInternationalPath());
		myAsyncTask.execute(NewsUrlPath.getUrlSocietyPath());
		
		myAsyncTask.SetOnDownLoadCompleteLister(new OnDownLoadCompleteLister() {
			
			@Override
			public void SetOnDownLoadCompleteLister(List<News> myList) {
				// TODO Auto-generated method stub
				list.addAll(myList);
				adapter.notifyDataSetChanged();
//				Log.i(TAG, list.toString()+"a");
			}
		});
		
		adapter = new MyBaseAdapter(this,list);
		
		listView.setAdapter(adapter);*/
		//获取fragment的管理对象
		fragmentManager = getFragmentManager();
		headLine_Fragment = new HeadLine_Fragment();
		society_Fragment = new HeadLine_Fragment();
		inland_Fragment = new HeadLine_Fragment();
		international_Fragment = new HeadLine_Fragment();
		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.my_radiogroup);
		radioGroup.setOnCheckedChangeListener(this);
		RadioButton headLine_radioButton=(RadioButton) findViewById(R.id.headLine_radiobutton);
		headLine_radioButton.setChecked(true);
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		/*RadioButton headLine_radioButton=(RadioButton) findViewById(R.id.headLine_radiobutton);
		headLine_radioButton.setChecked(true);*/
		if(checkedId==R.id.headLine_radiobutton){
			beginTransaction = fragmentManager.beginTransaction();
			args = new Bundle();
			//共用一个key来区分不同的参数
			args.putString("headLine_Fragment", "headLine_Fragment");
			headLine_Fragment.setArguments(args);
			beginTransaction.replace(R.id.fragment_container, headLine_Fragment);
			beginTransaction.commit();
		}else if(checkedId==R.id.social_radiobutton){
			beginTransaction=	fragmentManager.beginTransaction();
			args = new Bundle();
			args.putString("headLine_Fragment", "society_Fragment");
			society_Fragment.setArguments(args);
			beginTransaction.replace(R.id.fragment_container, society_Fragment );
			beginTransaction.commit();
		}
		else if(checkedId==R.id.inner_radiobutton){
			beginTransaction=	fragmentManager.beginTransaction();
			args = new Bundle();
			args.putString("headLine_Fragment", "inland_Fragment");
			inland_Fragment.setArguments(args);
			beginTransaction.replace(R.id.fragment_container, inland_Fragment);
			beginTransaction.commit();
		}else if(checkedId==R.id.world_radiobutton){
			beginTransaction=	fragmentManager.beginTransaction();
			args = new Bundle();
			args.putString("headLine_Fragment", "international_Fragment");
			international_Fragment.setArguments(args);
			beginTransaction.replace(R.id.fragment_container, international_Fragment);
			beginTransaction.commit();
		}
	}

	
}

HeadLine_Fragment.java

package com.newsjsontest.fragment;

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

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

import com.newsjsontest.NewsUrlPath;
import com.newsjsontest.R;
import com.newsjsontest.adapter.MyBaseAdapter;
import com.newsjsontest.async.MyAsyncTask;
import com.newsjsontest.async.MyAsyncTask.OnDownLoadCompleteLister;
import com.newsjsontest.bean.News;

public class HeadLine_Fragment extends Fragment {
	private List<News> list;
	private MyBaseAdapter adapter;
	private ListView listView;
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		list = new ArrayList<News>();
		MyAsyncTask myAsyncTask= new MyAsyncTask();
		Bundle args=this.getArguments();
		String headLine_Fragment=args.getString("headLine_Fragment");
		/*String society_Fragment=args.getString("society_Fragment");
		String inland_Fragment=args.getString("inland_Fragment");
		String international_Fragment=args.getString("international_Fragment");*/
		if(headLine_Fragment.equals("headLine_Fragment")){
			myAsyncTask.execute(NewsUrlPath.getUrlHeadlinePath());
		}else if(headLine_Fragment.equals("society_Fragment")){
			myAsyncTask.execute(NewsUrlPath.getUrlSocietyPath());
		}else if(headLine_Fragment.equals("inland_Fragment")){
			myAsyncTask.execute(NewsUrlPath.getUrlInlandPath());
		}else if(headLine_Fragment.equals("international_Fragment")){
			myAsyncTask.execute(NewsUrlPath.getUrlInternationalPath());
		}
//		myAsyncTask.execute(NewsUrlPath.getUrlHeadlinePath());
//		myAsyncTask.execute(NewsUrlPath.getUrlInlandPath());
//		myAsyncTask.execute(NewsUrlPath.getUrlInternationalPath());
		
		myAsyncTask.SetOnDownLoadCompleteLister(new OnDownLoadCompleteLister() {
			
			@Override
			public void SetOnDownLoadCompleteLister(List<News> myList) {
				// TODO Auto-generated method stub
				list.addAll(myList);
				adapter.notifyDataSetChanged();
//				Log.i(TAG, list.toString()+"a");
			}
		});
		adapter = new MyBaseAdapter(getActivity(),list);
		super.onAttach(activity);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.myheadline_news, container, false);
		listView = (ListView)view.findViewById(R.id.headLine_news);
		return view;
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		listView.setAdapter(adapter);
		super.onActivityCreated(savedInstanceState);
	}
}
activity_mail.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:orientation="vertical"
    tools:context="com.newsjsontest.MainActivity" >

    <!--
         <ListView 
        android:id="@+id/headLine_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>
    -->

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>
    <!--
             <fragment
            android:name="com.newsjsontest.fragment.HeadLine_Fragment"
            android:id="@+id/head_line_fragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            ></fragment>
    -->

    <RadioGroup
        android:id="@+id/my_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="0dp" >

        <RadioButton
            android:id="@+id/headLine_radiobutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/context_selector"
            android:gravity="center"
            android:text="头条"
            android:textColor="@drawable/radio_btn_color_selector" />

        <RadioButton
            android:id="@+id/social_radiobutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:checked="true"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/find_selector"
            android:gravity="center"
            android:text="社会"
            android:textColor="@drawable/radio_btn_color_selector" />

        <RadioButton
            android:id="@+id/inner_radiobutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/we_selector"
            android:gravity="center"
            android:text="国内"
            android:textColor="@drawable/radio_btn_color_selector" />

        <RadioButton
            android:id="@+id/world_radiobutton"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/me_selector"
            android:gravity="center"
            android:text="国际"
            android:textColor="@drawable/radio_btn_color_selector" />
    </RadioGroup>

</LinearLayout>


文件分享一个zip包。下载链接:             http://download.csdn.net/download/qq_17524035/9398200

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值