赞
踩
1>//一个简单的布局 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/draw_layout" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.5" android:background="@color/colorPrimary"> <ImageView android:id="@+id/iv_head" android:layout_width="80dp" android:layout_height="80dp" android:layout_margin="10dp" android:onClick="onClick" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:paddingRight="50dp" android:text="标题" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="16" android:id="@+id/fragment"> </FrameLayout> <RadioGroup android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/group" android:layout_weight="1.5" android:orientation="horizontal" android:background="#113c3c3c" > <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn1" android:button="@null" android:checked="true" android:gravity="center" android:layout_weight="1" android:text="头条" android:background="@drawable/select" /> <RadioButton android:id="@+id/btn2" android:layout_width="0dp" android:layout_height="match_parent" android:button="@null" android:gravity="center" android:text="福利" android:layout_weight="1" android:background="@drawable/select" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn3" android:button="@null" android:gravity="center" android:text="任务" android:layout_weight="1" android:background="@drawable/select" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/btn4" android:button="@null" android:gravity="center" android:text="我的" android:layout_weight="1" android:background="@drawable/select" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="400dp" android:layout_height="match_parent" android:layout_gravity="start" android:orientation="vertical" android:background="@color/colorAccent" > <ImageView android:layout_width="200dp" android:layout_height="200dp" android:src="@mipmap/ic_launcher"/> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:text="嗡嗡嗡" android:textSize="20sp" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
2>//MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private DrawerLayout drawerLayout; private ImageView imageView; private FrameLayout frameLayout; private RadioGroup group; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawerLayout = findViewById(R.id.draw_layout); imageView = findViewById(R.id.iv_head); frameLayout = findViewById(R.id.fragment); group = findViewById(R.id.group); imageView.setOnClickListener(this); drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(@NonNull View drawerView, float slideOffset) { View content = drawerLayout.getChildAt(0); float scale = 1 - slideOffset; content.setTranslationX(drawerView.getMeasuredWidth()*(1-scale)); } @Override public void onDrawerOpened(@NonNull View drawerView) { } @Override public void onDrawerClosed(@NonNull View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } }); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.btn1: getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new Fragment_One()).commit(); break; case R.id.btn2: getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new Fragment_Two()).commit(); break; case R.id.btn3: getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new Fragment_Three()).commit(); break; case R.id.btn4: getSupportFragmentManager().beginTransaction().replace(R.id.fragment,new Fragment_Four()).commit(); break; } } }); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.iv_head: if (drawerLayout.isDrawerOpen(Gravity.START)){ drawerLayout.closeDrawer(Gravity.START); }else { drawerLayout.isDrawerOpen(Gravity.START); } break; } } }
3>//Fragment_one layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v4.view.ViewPager android:id="@+id/viewpage" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3"></android.support.v4.view.ViewPager> <com.example.administrator.zhongyulong0728.xlist.XListView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="7" android:id="@+id/xlistview" > </com.example.administrator.zhongyulong0728.xlist.XListView> </LinearLayout>
4>//Fragment_One.java
public class Fragment_One extends Fragment { private ViewPager viewPager; private String urls = "http://www.xieast.com/api/banner.php"; private String psth = "http://ttpc.dftoutiao.com/jsonpc/refresh?type="; private int page = 5010; private XListView xListView; private List<UserBean.DataBean> list = new ArrayList<>(); private One_Adapter adapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one,container,false); xListView = view.findViewById(R.id.xlistview); viewPager = view.findViewById(R.id.viewpage); //访问数据 getDataWork(); //获取数据 initFromById(); return view; } private void initFromById() { //可以刷新 xListView.setPullLoadEnable(true); //可以加载 xListView.setPullRefreshEnable(true); xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { page=5010; getDataWork(); } @Override public void onLoadMore() { page++; getDataWork(); } }); } public void getDataWork() { HttpUtils httpUtils = HttpUtils.getInstance(); String url = psth+page; httpUtils.getdata(url); //接口毁掉 httpUtils.setHttpListnenr(new HttpUtils.HttpListener() { @Override public void getjsondata(String json) { Gson gson = new Gson(); json =json.replace("null(", "").replace(")", ""); UserBean userBean = gson.fromJson(json, UserBean.class); List<UserBean.DataBean> data = userBean.getData(); adapter = new One_Adapter(list,getActivity()); xListView.setAdapter(adapter); if (page==5010){ list.clear(); } //添加数据 list.addAll(data); adapter.notifyDataSetChanged(); if (page==5010){ xListView.stopRefresh(); }else { xListView.stopLoadMore(); } } }); } }
5>//fragment_one的适配器fragmenrbase.layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:text="textView"/> </LinearLayout>
6>//based的Adapter Adapter_One
public class One_Adapter extends BaseAdapter { private List<UserBean.DataBean> list; private Context context; public One_Adapter(List<UserBean.DataBean> list, Context context) { this.list = list; this.context = context; } @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) { ViewHolder holder; if (view==null){ view = View.inflate(context, R.layout.fragmentbase,null); holder = new ViewHolder(); holder.textView =view.findViewById(R.id.text); holder.imageView = view.findViewById(R.id.img); view.setTag(holder); }else { holder = (ViewHolder) view.getTag(); } ImageLoader.getInstance().displayImage(list.get(i).getMiniimg().get(0).getSrc(), holder.imageView, Myapp.getOptions()); holder.textView.setText(list.get(i).getBrief()); // ImageLoader.getInstance().displayImage(list.get(i).getMiniimg().get(0),holder.imageView, Myapp.getOptions()); return view; } class ViewHolder{ TextView textView; ImageView imageView; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。