添加依赖:
gson image_loader xutils recyleview_v7
添加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
android:name=".application.MvApplication"
布局:
1:activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="baway.com.mydemo.MainActivity"> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/manhua" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:checked="true" android:gravity="center" android:padding="10dp" android:text="漫画" android:textColor="@drawable/textcolor_select" /> <RadioButton android:id="@+id/faxian" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="10dp" android:text="发现" android:textColor="@drawable/textcolor_select" /> <RadioButton android:id="@+id/vshequ" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="10dp" android:text="V社区" android:textColor="@drawable/textcolor_select" /> <RadioButton android:id="@+id/wode" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="10dp" android:text="我的" android:textColor="@drawable/textcolor_select" /> </RadioGroup> </LinearLayout>
2:fragment1:
<?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"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="137dp"></android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/ll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:layout_centerHorizontal="true" android:orientation="horizontal"></LinearLayout> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/im" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="10dp" android:src="@drawable/paihang" /> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="96dp" android:src="@drawable/xinzuo" /> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentRight="true" android:layout_marginRight="96dp" android:src="@drawable/wanjie" /> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:src="@drawable/fenlei" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="本周推荐" /> <TextView android:id="@+id/gengduo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="10dp" android:text="更多" /> </RelativeLayout> <GridView android:id="@+id/grid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numColumns="2" android:padding="2dp"></GridView> </LinearLayout>
3:home_item:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal"> <TextView android:id="@+id/textView" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="5dp" android:textSize="18sp" android:gravity="center" /> </LinearLayout>
4:home_item2:
<?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:id="@+id/textView2" android:layout_width="100dp" android:layout_height="wrap_content" android:padding="5dp" android:textSize="18sp" android:gravity="center" /> </LinearLayout>
5: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"> <ImageView android:layout_gravity="center" android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:layout_gravity="center" android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
6:pindao_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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="我的频道" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:padding="10dp" android:text="长按拖拽替换位置" /> <Button android:id="@+id/bianji" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="编辑" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="2dp" android:background="#D6D7D7" /> <android.support.v7.widget.RecyclerView android:id="@+id/recy1" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="2dp" android:background="#D6D7D7" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" android:text="其他频道" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:padding="10dp" android:text="点击添加替换到我的频道" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="10dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="2dp" android:background="#D6D7D7" /> <android.support.v7.widget.RecyclerView android:id="@+id/recy2" android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout> <TextView android:id="@+id/chuanzhi" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
7:drawable-textcolor_select:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#FFE13C" android:state_checked="true"></item> <item android:color="#2B2D2E"></item> </selector>
MvApplication:
public class MvApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); try { File cacheDir = StorageUtils.getOwnCacheDirectory(this, Environment.getExternalStorageDirectory().getPath()); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this) .threadPoolSize(3)//配置线程数量 .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存图片 2M .diskCache(new UnlimitedDiskCache(cacheDir))//配饰sdcard缓存路径 .diskCacheSize(50 * 1024 * 1024)//sdcard上缓存50M的图片 .diskCacheFileCount(100)//缓存文件的数量 100个 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .build(); //配置缓存选项 ImageLoader.getInstance().init(configuration); } catch (Exception e) { e.printStackTrace(); } } }
Fragment1:
public class Fragment1 extends Fragment{ private ViewPager vp; private LinearLayout ll; private List<String> imagelist = new ArrayList<String>(); private List<View> dalist; private int oldpro = 0; private int count = 0; private GridView gr; private List<Bean.ResultBean.IndexProductsBean> list; private TextView gengduo; private List<Bean.ResultBean.BrandsBean> brands; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { vp.setCurrentItem(count); } }; private Bean bean; private String pic; private DisplayImageOptions options; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); gengduo = (TextView) getView().findViewById(R.id.gengduo); if (NetWorkUtils.isNetWorkConnection(getActivity())) { getShuju(); initView(); autoplay(); } else { Intent intent = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(intent); } //配置是否对图片进行缓存 options = new DisplayImageOptions.Builder() .cacheInMemory(true)//是否内存缓存 .cacheOnDisk(true)//是否sdcard缓存 .build();//构建图片缓存的选项 gengduo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<Bean.ResultBean.CategoryBean> category = bean.getResult().getCategory(); List<Bean.ResultBean.NationsBean> nations = bean.getResult().getNations(); Intent intent = new Intent(getActivity(), PinDaoGuanLi.class); intent.putExtra("yy",(Serializable)category); intent.putExtra("nn",(Serializable)nations); startActivity(intent); } }); } private void getShuju() { RequestParams params = new RequestParams(); params.setUri("http://www.babybuy100.com/API/getShopOverview.ashx"); x.http().get(params, new Callback.CommonCallback<String>() { @Override public void onSuccess(String result) { Gson gson = new Gson(); bean = gson.fromJson(result, Bean.class); brands = bean.getResult().getBrands(); for (int i = 0; i < brands.size(); i++) { pic = brands.get(i).getPic(); imagelist.add(pic); } list = bean.getResult().getIndexProducts(); Mydapter mydapter = new Mydapter(); gr.setAdapter(mydapter); setData(); setListener(); try { File file = new File(Environment.getExternalStorageDirectory(),"menuinfo.txt"); if (!file.exists()){ file.createNewFile(); } OutputStream os = new FileOutputStream(file); os.write(pic.getBytes()); //关闭流 os.close(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } private void setListener() { vp.setAdapter(new MyPagerAdapter()); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int pro) { // TODO Auto-generated method stub dalist.get(pro % imagelist.size()).setBackgroundResource( R.drawable.btn_radio_on_holo_dark); dalist.get(oldpro % imagelist.size()).setBackgroundResource( R.drawable.btn_radio_on_disabled_holo_dark); oldpro = pro; } @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() { dalist = new ArrayList<View>(); for (int i = 0; i < imagelist.size(); i++) { View view = new View(getActivity()); view.setLayoutParams(new LinearLayout.LayoutParams(50, 50)); dalist.add(view); if (i == 0) { dalist.get(i).setBackgroundResource( R.drawable.btn_radio_on_holo_dark); } else { dalist.get(i).setBackgroundResource( R.drawable.btn_radio_on_disabled_holo_dark); } ll.addView(dalist.get(i)); } } private void autoplay() { new Thread() { public void run() { while (true) { try { SystemClock.sleep(3000); count++; handler.sendEmptyMessage(0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } private void initView() { vp = (ViewPager) getView().findViewById(R.id.vp); ll = (LinearLayout) getView().findViewById(R.id.ll); gr = (GridView) getView().findViewById(R.id.grid); } public class MyPagerAdapter extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub // super.destroyItem(container, position, object); container.removeView((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub ImageView imageView = new ImageView(getActivity()); ImageLoader.getInstance().displayImage(imagelist.get(position % imagelist.size()), imageView,options); container.addView(imageView); return imageView; } } class Mydapter extends BaseAdapter { @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder; if (convertView == null) { holder = new Holder(); convertView = View.inflate(getActivity(), R.layout.item, null); holder.image = (ImageView) convertView.findViewById(R.id.imageView); holder.text = (TextView) convertView.findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (Holder) convertView.getTag(); } ImageLoader.getInstance().displayImage(list.get(position).getPic(), holder.image,options); holder.text.setText(list.get(position).getName()); return convertView; } } class Holder { ImageView image; TextView text; } }
MainActivity:
public class MainActivity extends AppCompatActivity { private List<Fragment> list; private RadioGroup rg; private ViewPager vp; private RadioButton manhua; private RadioButton faxian; private RadioButton vshequ; private RadioButton wode; private Fragment[] mFragments; private int mIndex=0; private FrameLayout fr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initFragment(); } private void initFragment() { Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); Fragment3 fragment3 = new Fragment3(); Fragment4 fragment4 = new Fragment4(); mFragments = new Fragment[]{fragment1,fragment2,fragment3,fragment4}; FragmentTransaction ft =getSupportFragmentManager().beginTransaction(); ft.add(R.id.content,fragment1).commit(); setIndexSelected(0); } private void setIndexSelected(int index) { if(mIndex==index){ return; } FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft= fragmentManager.beginTransaction(); //隐藏 ft.hide(mFragments[mIndex]); //判断是否添加 if(!mFragments[index].isAdded()){ ft.add(R.id.content,mFragments[index]).show(mFragments[index]); }else { ft.show(mFragments[index]); } ft.commit(); //再次赋值 mIndex=index; } private void initView() { rg = (RadioGroup) findViewById(R.id.rg); fr = (FrameLayout) findViewById(R.id.content); manhua = (RadioButton) findViewById(R.id.manhua); faxian = (RadioButton) findViewById(R.id.faxian); vshequ = (RadioButton) findViewById(R.id.vshequ); wode = (RadioButton) findViewById(R.id.wode); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.manhua: setIndexSelected(0); break; case R.id.faxian: setIndexSelected(1); break; case R.id.vshequ: setIndexSelected(2); break; case R.id.wode: setIndexSelected(3); break; } } }); } }
PinDaoGuanLi:
public class PinDaoGuanLi extends Activity { private SwipeRefreshLayout swipe; private RecyclerView recyclerView; private RecyleAdapter recyleAdapter; private boolean isRefresh = true; private GridLayoutManager gridLayoutManager; private GridLayoutManager gridLayoutManager2; private RecyclerView recyclerView1; private RecyleAdapter2 recyleAdapter2; private Button button; private RequestParams params; private ArrayList<String> list=new ArrayList<>(); private ArrayList<String> list2=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pindao_item); initView(); initData(); } private void initData() { recyclerView = (RecyclerView) findViewById(R.id.recy1); recyclerView1 = (RecyclerView) findViewById(R.id.recy2); recyleAdapter = new RecyleAdapter(this); recyleAdapter2 = new RecyleAdapter2(this); gridLayoutManager = new GridLayoutManager(this, 3); gridLayoutManager2 = new GridLayoutManager(this, 3); recyclerView.setLayoutManager(gridLayoutManager); recyclerView1.setLayoutManager(gridLayoutManager2); recyleAdapter.setData(list); recyclerView.setAdapter(recyleAdapter); recyleAdapter.notifyDataSetChanged(); recyleAdapter2.setDataelse(list2); recyclerView1.setAdapter(recyleAdapter2); recyleAdapter2.notifyDataSetChanged(); recyleAdapter.setOnClick(new RecyleAdapter.onClick() { @Override public void onCli(View v, int position) { myData(position); } }); recyleAdapter2.setOnClick(new RecyleAdapter.onClick() { @Override public void onCli(View v, int position) { elseData(position); } }); } private void initView() { Intent intent = getIntent(); List<Bean.ResultBean.CategoryBean> category = (List<Bean.ResultBean.CategoryBean>) intent.getSerializableExtra("yy"); for (int i = 0; i <category.size() ; i++) { list.add(category.get(i).getName()); } List<Bean.ResultBean.NationsBean> nations = (List<Bean.ResultBean.NationsBean>) intent.getSerializableExtra("nn"); for (int i = 0; i < nations.size(); i++) { list2.add(nations.get(i).getName()); } } private void elseData(int position) { list.add(list2.get(position)); recyleAdapter.setData(list); recyleAdapter.notifyDataSetChanged(); list2.remove(position); recyleAdapter2.setDataelse(list2); recyleAdapter2.notifyDataSetChanged(); } private void myData(int position) { list2.add(list.get(position)); recyleAdapter2.setDataelse(list2); recyleAdapter2.notifyDataSetChanged(); list.remove(position); recyleAdapter.setData(list); recyleAdapter.notifyDataSetChanged(); } }
RecyleAdapter:
public class RecyleAdapter extends RecyclerView.Adapter { private List<String> list=new ArrayList<>(); private Context mcontext; public RecyleAdapter(Context context){ this.mcontext=context; } public void setData(List<String> data){ this.list.clear(); this.list.addAll(0,data); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(mcontext, R.layout.home_item, null); MyHolderView myHolderView = new MyHolderView(view); return myHolderView; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { MyHolderView my= (MyHolderView) holder; my.textView.setText(list.get(position)); my.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { monClick.onCli(v,position); } }); my.textView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { monitemlong.onLong(v,position); return false; } }); } @Override public int getItemCount() { return list.size(); } class MyHolderView extends RecyclerView.ViewHolder{ private final TextView textView; public MyHolderView(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.textView); } } public interface onClick{ void onCli(View v, int position); } private onClick monClick; public void setOnClick(onClick onClick){ this.monClick=onClick; } public interface onItemlong{ void onLong(View v, int position); } private onItemlong monitemlong; public void setonItemlong(onItemlong onitemlong){ this.monitemlong=onitemlong; } }
RecyleAdapter2:
public class RecyleAdapter2 extends RecyclerView.Adapter { private List<String> list=new ArrayList<>(); private Context mcontext; public RecyleAdapter2(Context context){ this.mcontext=context; } public void setDataelse(List<String> data){ this.list.clear(); this.list.addAll(0,data); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(mcontext, R.layout.home_item2, null); RecyleAdapter2.MyHolderView myHolderView = new RecyleAdapter2.MyHolderView(view); return myHolderView; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { RecyleAdapter2.MyHolderView my= (RecyleAdapter2.MyHolderView) holder; my.textView.setText(list.get(position)); my.textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { monClick.onCli(v,position); } }); } @Override public int getItemCount() { return list.size(); } class MyHolderView extends RecyclerView.ViewHolder{ private final TextView textView; public MyHolderView(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(R.id.textView2); } } public interface onClick{ void onCli(View v, int position); } private RecyleAdapter.onClick monClick; public void setOnClick(RecyleAdapter.onClick onClick){ this.monClick=onClick; } }