首先在主 build.gradle
allprojects {
repositories {
google()
maven {url "https://jitpack.io"}
jcenter()
}
}
项目的build.gradle
compile 'com.github.andyoom:draggrid:v1.0.1' implementation 'com.android.support:design:26.0.0-alpha1'
package hongliang.com.channelmanagement; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; import com.andy.library.ChannelActivity; import com.andy.library.ChannelBean; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TabLayout mMytab; private ImageButton mImgBtn; private ArrayList<ChannelBean> channelBeens; String jsonStr = ""; private Gson gson; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); vpsp vpsp = new vpsp(getSupportFragmentManager()); vp.setAdapter(vpsp); mMytab.setupWithViewPager(vp); } private void initData(){ //准备栏目数据 channelBeens = new ArrayList<ChannelBean>(); channelBeens.add(new ChannelBean("热点",true)); channelBeens.add(new ChannelBean("军事",true)); channelBeens.add(new ChannelBean("八卦",true)); channelBeens.add(new ChannelBean("游戏",true)); channelBeens.add(new ChannelBean("宠物",true)); channelBeens.add(new ChannelBean("汽车",true)); channelBeens.add(new ChannelBean("热卖",true)); channelBeens.add(new ChannelBean("外卖",true)); channelBeens.add(new ChannelBean("条目1",true)); channelBeens.add(new ChannelBean("条目2",true)); channelBeens.add(new ChannelBean("条目3",false)); channelBeens.add(new ChannelBean("条目4",false)); channelBeens.add(new ChannelBean("条目5",false)); channelBeens.add(new ChannelBean("条目6",false)); channelBeens.add(new ChannelBean("条目7",false)); channelBeens.add(new ChannelBean("条目8",false)); //把选择的栏目(true)数据配置给tablayout for (int i=0;i<channelBeens.size();i++){ if(channelBeens.get(i).isSelect()){ mMytab.addTab(mMytab.newTab().setText(channelBeens.get(i).getName())); } } } private void initView() { mMytab = (TabLayout) findViewById(R.id.mytab); mImgBtn = (ImageButton) findViewById(R.id.imgBtn); mImgBtn.setOnClickListener(this); vp = (ViewPager) findViewById(R.id.vp); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imgBtn: ChannelActivity.startChannelActivity(MainActivity.this,channelBeens); break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE){//为true表示是频道管理回调回来的 jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);//得到栏目管理的结果 Toast.makeText(this,jsonStr,Toast.LENGTH_SHORT).show(); Log.i("main",jsonStr); mMytab.removeAllTabs();//清空之前的栏目 //把新选择的栏目结果更新到tablayout上 gson = new Gson(); //进行json解析 Type type= new TypeToken<ArrayList<ChannelBean>>(){}.getType(); channelBeens = gson.fromJson(jsonStr,type); //遍历结果,更新tablayout for (int i=0;i<channelBeens.size();i++){ if(channelBeens.get(i).isSelect()){ mMytab.addTab(mMytab.newTab().setText(channelBeens.get(i).getName())); } } } } class vpsp extends FragmentPagerAdapter { //有参数的构造 public vpsp(FragmentManager fm) { super(fm); } //返回选项卡的文本 ,,,添加选项卡 @Override public CharSequence getPageTitle(int position) { return channelBeens.get(position).getName(); } //创建fragment对象并返回 @Override public Fragment getItem(int position) { content content = new content(); Bundle bundle = new Bundle(); bundle.putString("name",channelBeens.get(position).getName()); content.setArguments(bundle); return content; } //返回数量 @Override public int getCount() { return channelBeens.size(); } } }
package hongliang.com.channelmanagement; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Created by 知足 on 2018/3/14. */ public class content extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = View.inflate(getActivity(), R.layout.contentlayout, null); TextView cte = inflate.findViewById(R.id.cte); Bundle arguments = getArguments(); String name = arguments.getString("name"); Log.e("chen", "onCreateView: ------"+name ); cte.setText(name); return inflate; } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="hongliang.com.channelmanagement.MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/mytab" app:tabMode="scrollable" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" ></android.support.v4.view.ViewPager> <ImageButton android:id="@+id/imgBtn" android:src="@mipmap/ic_launcher" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
<?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:gravity="center"> <TextView android:id="@+id/cte" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="dfxcvnxcv" android:textColor="@color/colorAccent"/> </LinearLayout>
到这代码就已经全部奉献了