RadioGroup+Fragment实现界面切换

来源:互联网 发布:淘宝电子面单打印机 编辑:程序博客网 时间:2024/05/16 01:28

主界面===========================================

package com.bwie.test;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends FragmentActivity implements OnClickListener{

   
    private FragmentTransaction fts;
    private Fragment1 f1;
    private Fragment2 f2;
    private Fragment3 f3;
    private Fragment4 f4;
    private Button bt1,bt2,bt3,bt4;
    private ViewPager vp;
    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
       // vp=(ViewPager) findViewById(R.id.vp);
        
        fm=getSupportFragmentManager();
       fts=fm.beginTransaction();
       
       f1=new Fragment1();
       f2=new Fragment2();
       f3=new Fragment3();
       f4=new Fragment4();
       
       fts.add(R.id.fl, f1).add(R.id.fl, f2).add(R.id.fl,f3).add(R.id.fl, f4);
       fts.commit();
        
       initView();
       
       
    }

    private void initView() {
        bt1=(Button) findViewById(R.id.bt1);
        bt2=(Button) findViewById(R.id.bt2);
        bt3=(Button) findViewById(R.id.bt3);
        bt4=(Button) findViewById(R.id.bt4);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        
        
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.bt1:
            fts=fm.beginTransaction();
            fts.hide(f2).hide(f3).hide(f4).show(f1);
            fts.commit();
            break;
        
        case R.id.bt2:
            fts=fm.beginTransaction();
            fts.hide(f1).hide(f3).hide(f4).show(f2);
            fts.commit();
            break;
            
        case R.id.bt3:
            fts=fm.beginTransaction();
            fts.hide(f1).hide(f2).hide(f4).show(f3);
            fts.commit();
            break;
            
        case R.id.bt4:
            fts=fm.beginTransaction();
            fts.hide(f1).hide(f2).hide(f3).show(f4);
            fts.commit();
            break;
        
        }
    }
}

主界面布局==============================

<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="com.bwie.test.MainActivity" >

    
    <LinearLayout
        android:id="@+id/lin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资讯"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热点"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="博客"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="推荐"
        android:layout_weight="1"
        />
    </LinearLayout>
    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/lin"
        ></FrameLayout>
    <org.com.cctest.view.XListView
        android:id="@+id/xListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/lin"
       />
 

</RelativeLayout>



Fragment界面==========================================

package com.bwie.test;

import java.io.ByteArrayInputStream;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.com.cctest.view.XListView;
import org.com.cctest.view.XListView.IXListViewListener;
import org.xmlpull.v1.XmlPullParser;

import com.bwie.bean.Zixun;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class Fragment1 extends Fragment {

    private String path = "http://www.oschina.net/action/api/news_list";
    private Zixun zixun;
    private ArrayAdapter arrayAdapter;
    private List<String> list;
    private XListView xListView;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.f1, null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        setData();

        xListView = (XListView) getActivity().findViewById(R.id.xListView);

        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);

        xListView.setXListViewListener(new IXListViewListener() {

            private int j;

            @Override
            public void onRefresh() {
                list.clear();
                for (int i = 0; i < 5; i++) {
                    list.add("新增" + (j += 1));
                }
                arrayAdapter.notifyDataSetChanged();

                xListView.setRefreshTime(setTime());

            }

            private String setTime() {
                long cur = System.currentTimeMillis();

                SimpleDateFormat date = new SimpleDateFormat(
                        "yyyy-MM-dd  hh:mm:ss");
                String format = date.format(cur);
                return format;
            }

            @Override
            public void onLoadMore() {
                for (int i = 0; i < 5; i++) {
                    list.add("下拉刷新" + (j += 1));
                }

                arrayAdapter.notifyDataSetChanged();

                xListView.stopLoadMore();
                xListView.stopRefresh();

            }
        });

        arrayAdapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, list);

        xListView.setAdapter(arrayAdapter);

        getData();

        super.onActivityCreated(savedInstanceState);
    }

    private void setData() {
        list = new ArrayList<String>();

        list.add("aaaaa");
        list.add("bbbbb");
        list.add("sssss");
        list.add("eeeee");
        list.add("gggggg");
        list.add("eeeeee");
        list.add("wwwww");
        list.add("jhjjhj");
        list.add("rrrrr");

    }

    private void getData() {

        HttpUtils utils = new HttpUtils();

        utils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                String result = arg0.result;
                ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
                        result.getBytes());
                ArrayList<Zixun> startAnalyze=startAnalyze(arrayInputStream);
                xListView.setAdapter(new MyApater(getActivity(),startAnalyze));
                
            }
        });

    }

    protected ArrayList<Zixun> startAnalyze(
            ByteArrayInputStream arrayInputStream) {
        ArrayList<Zixun> zxList = new ArrayList<Zixun>();
        try {
            XmlPullParser newPullParser = Xml.newPullParser();
            newPullParser.setInput(arrayInputStream, "utf-8");
            int eventType = newPullParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name = newPullParser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if ("news".equals(name)) {
                        zixun = new Zixun();
                    } else if ("title".equals(name)) {
                        zixun.setTitle(newPullParser.nextText());
                    } else if ("body".equals(name)) {
                        zixun.setBody(newPullParser.nextText());
                    } else if ("pubDate".equals(name)) {
                        zixun.setPubDate(newPullParser.nextText());
                    } else if ("author".equals(name)) {
                        zixun.setAuthor(newPullParser.nextText());
                    }
                    break;

                case XmlPullParser.END_TAG:
                    if ("news".equals(name)) {
                        zxList.add(zixun);
                        zixun = null;
                    }
                    break;

                default:
                    break;
                }
                eventType = newPullParser.next();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return zxList;
    }

}


Fragment的Item================================================
<?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:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:textSize="20sp"  
        android:padding="10dp"  
        android:textStyle="bold"  
        android:id="@+id/tv_title"  
        android:text="title"
        />  
    <TextView   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/tv_body"  
        android:text="body"
        />  
    <LinearLayout   
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal"  
        android:padding="10dp"  
        >  
        <TextView   
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/tv_author"  
            android:text="author"
            />  
        <TextView   
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/tv_time"  
            android:layout_marginLeft="20dp"
            android:text="titme"
            />  
    </LinearLayout>  
      
 
</LinearLayout> 


Bean包=====================================================

package com.bwie.bean;

public class Zixun {

     private String title;  
        private String body;  
        private String pubDate;  
        private String author;
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getBody() {
            return body;
        }
        public void setBody(String body) {
            this.body = body;
        }
        public String getPubDate() {
            return pubDate;
        }
        public void setPubDate(String pubDate) {
            this.pubDate = pubDate;
        }
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        @Override
        public String toString() {
            return "Zixun [title=" + title + ", body=" + body + ", pubDate="
                    + pubDate + ", author=" + author + "]";
        }  
        
        
}


0 0
原创粉丝点击