ViewPager

来源:互联网 发布:centos 6.5 ftp客户端 编辑:程序博客网 时间:2024/05/18 16:39
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.bwie.myapplication.MainActivity"    >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >    <RadioGroup        android:id="@+id/RadioGroup"        android:layout_gravity="center"        android:padding="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/TextView1"            android:text="页面二"            android:textColor="@drawable/select"            android:checked="true"            android:textSize="20sp"            android:button="@null"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="页面二"            android:layout_marginRight="5dp"            android:button="@null"            android:textSize="20sp"            android:textColor="@drawable/select"            android:id="@+id/TextView2"            />    </RadioGroup>        <android.support.v4.view.ViewPager            android:layout_width="match_parent"            android:layout_height="match_parent"            android:id="@+id/vp"            ></android.support.v4.view.ViewPager>    </LinearLayout></LinearLayout>
f1<?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">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello"            android:textSize="20dp" />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1" />        <TextView            android:id="@+id/TextView3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello"            android:textSize="20dp" />    </LinearLayout></LinearLayout>
itemview<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"><TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/item_view"    android:textSize="20sp"    /></LinearLayout>

zhuche

<?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:background="#00CCCC"     >    <ListView        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/ListView"        android:text="注册"        /></LinearLayout>

activity

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {    private RadioGroup rp;    private RadioButton view1;    private RadioButton view2;    private ArrayList<Fragment> listfrag;    private ViewPager vp;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initview();        setListener();    }    private void setListener() {        listfrag = new ArrayList<Fragment>();        F1 f1 = new F1();        F2 f2 = new F2();        listfrag.add(f1);        listfrag.add(f2);        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {            @Override            public Fragment getItem(int position) {                return listfrag.get(position);            }            @Override            public int getCount() {                return listfrag.size();            }        });    }    private void initview() {        vp = (ViewPager) findViewById(R.id.vp);        rp = (RadioGroup) findViewById(R.id.RadioGroup);        view1 = (RadioButton) findViewById(R.id.TextView1);        view2 = (RadioButton) findViewById(R.id.TextView2);        rp.setOnCheckedChangeListener(this);        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {                  switch (position) {                            case 0:                              {                                view1.setChecked(true);                               }                                break;                              case 1:                              {                                  view2.setChecked(true);                              }                              break;                            }            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }    @Override    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {        int count=0;          switch (checkedId) {                    case R.id.TextView1:                      {                        count=0;                    //      vp.setCurrentItem(0,false);                       }                        break;                      case R.id.TextView2:                      {                          count=1;                     //     vp.setCurrentItem(1,false);                      }                      break;                    }        if (vp.getCurrentItem()!=count) {            vp.setCurrentItem(count, false);        }    }}

adapter

package com.bwie.myapplication;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import java.util.ArrayList;/** * 王 :王万鹏 * 2017/6/30 9:15 .: * & 作用  : * & 思路  : */public class MyAdapter extends BaseAdapter {    Context context;    ArrayList<String> list;    private ViewHodler hodler;    public MyAdapter(Context context, ArrayList<String> list) {        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return list.size();    }    @Override    public Object getItem(int i) {        return list.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        if (view==null){            hodler = new ViewHodler();            view=View.inflate(context,R.layout.itemview,null);            hodler.textView= (TextView) view.findViewById(R.id.item_view);            view.setTag(hodler);        }else {            hodler= (ViewHodler) view.getTag();        }        hodler.textView.setText(list.get(i));        return view;    }    static class ViewHodler {        TextView textView;    }}

F1

public class F1 extends Fragment implements View.OnClickListener {    private TextView mTextView;    private  ListView mListView;    private static ArrayList<String> list;    private Button mButton;    private PopupWindow popup;    private View view;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.f1,container,false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        get();        initview();    }    public void initview(){        mTextView = (TextView) getView().findViewById(R.id.TextView3);        mTextView.setOnClickListener(this);        view = View.inflate(getActivity(), R.layout.zhuche, null);        popup = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        popup.setOutsideTouchable(true);        popup.setBackgroundDrawable(new BitmapDrawable());        popup.setFocusable(true);        mListView = (ListView) view.findViewById(R.id.ListView);        list=new ArrayList<>();     //   Log.e("======",list.size()+"      "+list.toString());        MyAdapter adapter = new MyAdapter(getActivity(),list);        mListView.setAdapter(adapter);        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                Toast.makeText(getActivity()," 注册 ",Toast.LENGTH_LONG).show();            }        });    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.TextView3:                popup.showAsDropDown(mTextView);// 在                break;            default:                break;        }    }    private static OkHttpClient client = new OkHttpClient();    public static void get(){        //创建一个Request        final Request request = new Request.Builder()                .url("http://h5test.newaircloud.com/api/getLayouts?sid=xkycs&cid=10024")                .get()                .build();        //发起异步请求,并加入回调        client.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {            //    String s = response.body().toString();              //  Log.e("======",response.body().toString());                Gson gson = new Gson();                Bean bean = gson.fromJson(response.body().string(), Bean.class);                List<Bean.LayoutsBean> layouts = bean.getLayouts();              //  list.add(bean.getLayouts())                for (int i = 0; i < layouts.size(); i++) {                    String name = layouts.get(i).getName();                    list.add(name);                }            }        });    }}

F2

public class F2 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.f2,container,false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);    }}
原创粉丝点击