码迷,mamicode.com
首页 > 其他好文 > 详细

RadioGroup

时间:2016-04-05 21:27:54      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

<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" >
<RadioGroup 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    
    >
    <RadioButton 
        android:id="@+id/r1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资讯"
        android:layout_weight="1"
        
        android:button="@null"
         android:gravity="center"
      android:textSize="25dp"
        />
    <RadioButton 
        android:id="@+id/r2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="热点"
        android:layout_weight="1"
        android:button="@null"
       android:textSize="25dp"
         android:gravity="center"
        />
    <RadioButton 
        android:id="@+id/r3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="博客"
        android:layout_weight="1"
        android:button="@null"
         android:gravity="center"
        android:textSize="25dp"
        
        />
     <RadioButton 
         android:id="@+id/r4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="推荐"
         android:button="@null" 
         android:gravity="center"
         android:textSize="25dp"
         android:layout_weight="1"
         />
    
</RadioGroup>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:id="@+id/lin"  
    >    
    
</LinearLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>
package com.bawei.yuekao;

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

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private RadioButton r1;
    private RadioButton r2;
    private RadioButton r3;
    private RadioButton r4;
    private LinearLayout lin;
    private ViewPager viewPager;
    int width;
    private String[] url = new String[] {
            "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=",
            "http://www.oschina.net/action/api/news_list?catalog=4&show=week&pageIndex=",
            "http://www.oschina.net/action/api/blog_list?type=latest&pageIndex=",
            "http://www.oschina.net/action/api/blog_list?type=recommend&pageIndex=" };
    private List<Fragment> list;
    private List<TextView> l;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         //获取屏幕宽度
          width = getWindowManager().getDefaultDisplay().getWidth();
          
        r1 = (RadioButton) findViewById(R.id.r1);
        r2 = (RadioButton) findViewById(R.id.r2);
        r3 = (RadioButton) findViewById(R.id.r3);
        r4 = (RadioButton) findViewById(R.id.r4);
        r1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r3.setOnClickListener(this);
        r4.setOnClickListener(this);
        lin = (LinearLayout) findViewById(R.id.lin);
        //游标
        l = new ArrayList<TextView>();
        for(int i = 0; i < url.length; i++){
        TextView tv_line = new TextView(MainActivity.this);
            RadioButton button1=new RadioButton(MainActivity.this);
            
        LinearLayout.LayoutParams params_2 = new LayoutParams(
                width / 4, LayoutParams.WRAP_CONTENT);
        tv_line.setBackgroundColor(Color.GREEN);
        tv_line.setVisibility(View.INVISIBLE);
        l.add(tv_line);
       lin.addView(tv_line,i,params_2);
        
        }
        
        //viewpager适配
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setdata();
        FragmentManager fm = getSupportFragmentManager();
        viewPager.setAdapter(new MyViewPager(fm, list));
        //首次进入 
        viewPager.setCurrentItem(0);
        r1.setTextColor(Color.GREEN);
        l.get(0).setVisibility(View.VISIBLE);
        //viewpager监听
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                case 0:
                    r1.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(0).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 1:
                    r2.setTextColor(Color.GREEN);
                    r1.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(1).setVisibility(View.VISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 2:
                    r3.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r1.setTextColor(Color.BLACK);
                    r4.setTextColor(Color.BLACK);
                    l.get(2).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    l.get(3).setVisibility(View.INVISIBLE);
                    break;
                case 3:
                    r4.setTextColor(Color.GREEN);
                    r2.setTextColor(Color.BLACK);
                    r3.setTextColor(Color.BLACK);
                    r1.setTextColor(Color.BLACK);
                    l.get(3).setVisibility(View.VISIBLE);
                    l.get(1).setVisibility(View.INVISIBLE);
                    l.get(2).setVisibility(View.INVISIBLE);
                    l.get(0).setVisibility(View.INVISIBLE);
                    break;

                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    private void setdata() {
        list = new ArrayList<Fragment>();
        for (int i = 0; i < url.length; i++) {
            Fragment1 f1 = new Fragment1(url[i],i);
            list.add(f1);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.r1:
            viewPager.setCurrentItem(0);
            break;
        case R.id.r2:
            viewPager.setCurrentItem(1);
            break;
        case R.id.r3:
            viewPager.setCurrentItem(2);
            break;
        case R.id.r4:
            viewPager.setCurrentItem(3);
            break;

        }
    }

}
package com.bawei.yuekao;

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

import com.bawei.vo.Good;
import com.bawei.vo.Goods;
import com.bawei.vo.Myblog;
import com.bawei.vo.Myblogs;
import com.bawei.vo.Mynews;
import com.example.xlistviews.XListView;
import com.example.xlistviews.XListView.IXListViewListener;
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 com.thoughtworks.xstream.XStream;

import android.annotation.SuppressLint;
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.BaseAdapter;

public class Fragment1 extends Fragment implements IXListViewListener {
    String url;
    int m;

    public Fragment1(String url, int i) {
        // TODO Auto-generated constructor stub
        this.url = url;
        this.m = i;
    }

    int num = 0;
    int z = 6;
    int p = 6;
    private BaseAdapter adapter;
    private BaseAdapter adapters;
    private XListView my_xlist;
    List<Mynews> list = new ArrayList<Mynews>();
    List<Myblog> lists = new ArrayList<Myblog>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f1, null);
        my_xlist = (XListView) view.findViewById(R.id.my_xlist);
        my_xlist.setPullLoadEnable(true);
        my_xlist.setPullRefreshEnable(true);
        my_xlist.setXListViewListener(this);
        shuain();
        return view;
    }

    private void shuain() {
        z = z - 1;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url + z, new RequestCallBack<String>() {

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

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String result = arg0.result;
                XStream stream = new XStream();
                if (m == 0 || m == 1) {
                    stream.processAnnotations(Good.class);
                    Good good = (Good) stream.fromXML(result);
                    List<Mynews> news = good.getNewslist().getNews();
                    list = news;
                    adapter = new Adpter(getActivity(), list);
                    my_xlist.setAdapter(adapter);
                }
                if (m == 2 || m == 3) {
                    stream.processAnnotations(Goods.class);
                    Goods good = (Goods) stream.fromXML(result);
                    List<Myblog> blog = good.getBlogs().getBlog();
                    lists = blog;
                    adapters = new Adrters(getActivity(), lists);
                    my_xlist.setAdapter(adapters);
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        shuain();
        onLoad();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        p = p - 1;
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpMethod.POST, url + p, new RequestCallBack<String>() {

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

            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                String result = arg0.result;
                XStream stream = new XStream();
                if (m == 0 || m == 1) {
                    stream.processAnnotations(Good.class);
                    Good good = (Good) stream.fromXML(result);
                    List<Mynews> news = good.getNewslist().getNews();
                    list.addAll(news);
                    adapter.notifyDataSetChanged();
                }
                if (m == 2 || m == 3) {
                    stream.processAnnotations(Goods.class);
                    Goods good = (Goods) stream.fromXML(result);
                    List<Myblog> blog = good.getBlogs().getBlog();
                    lists.addAll(blog);
                    adapters.notifyDataSetChanged();
                }
            }
        });
        onLoad();
    }

    /*
     * 
     * 设置时间
     */
    @SuppressLint("SimpleDateFormat")
    private void onLoad() {
        my_xlist.stopRefresh();
        my_xlist.stopLoadMore();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间
        String nowTime = df.format(new Date(System.currentTimeMillis()));
        // 释放时提示正在刷新时的当前时间
        my_xlist.setRefreshTime(nowTime);
    }
}
package com.bawei.yuekao;

import java.util.List;

import com.bawei.vo.Mynews;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Adpter extends BaseAdapter {
    Context context;
    List<Mynews> list;
    public Adpter(Context context, List<Mynews> list) {
        // TODO Auto-generated constructor stub
    this.context=context;
    this.list=list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if(convertView==null){
        convertView=View.inflate(context, R.layout.xlistview, null);
        viewHolder=new ViewHolder();
        viewHolder.t1=(TextView) convertView.findViewById(R.id.t1);
        viewHolder.t2=(TextView) convertView.findViewById(R.id.t2);
        viewHolder.t3=(TextView) convertView.findViewById(R.id.t3);
        convertView.setTag(viewHolder);
        }else{
        viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.t1.setText(list.get(position).getTitle());
        viewHolder.t2.setText(list.get(position).getBody());
        viewHolder.t3.setText(list.get(position).getPubDate());
        
        return convertView;
    }
class ViewHolder{
    TextView t1;
    TextView t2;
    TextView t3;
    
}
}

 

RadioGroup

标签:

原文地址:http://www.cnblogs.com/1426837364qqcom/p/5356881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!